Task

Write a function to draw a HSV color wheel completely with code.

This is strictly for learning purposes only. It's highly recommended that you use an image in an actual application to actually draw the color wheel   (as procedurally drawing is super slow). This does help you understand how color wheels work and this can easily be used to determine a color value based on a position within a circle.



def  Radius = 480/2;
real Hue, Sat, Dist, I, F, P, Q, T;
real XX, YY, RR, GG, BB;
int  X, Y, R, G, B;
def  Pi = 3.141592654;
def  V = 1.;            \Value
[SetVid($112);          \640x480x24 graphics
for Y:= -Radius to Radius do
    for X:= -Radius to Radius do
        [XX:= float(X);
         YY:= float(Y);
         Dist:= sqrt(XX*XX + YY*YY);
         if Dist <= float(Radius) then
                [Sat:= Dist/float(Radius);      \0 >= Sat <= 1
                Hue:= ATan2(YY, XX);            \-Pi >= Hue <= Pi
                if Hue < 0. then Hue:= Hue + 2.*Pi;
                Hue:= Hue * 180./Pi;            \radians to degrees
                Hue:= Hue / 60.;                \0 >= Hue < 6
                I:= Floor(Hue);                 \integer part of Hue
                F:= Hue - I;                    \fractional part of Hue
                P:= 1. - Sat;
                Q:= 1. - Sat*F;
                T:= 1. - Sat*(1.-F);
                case fix(I) of
                  0:    [RR:= V;  GG:= T;  BB:= P];
                  1:    [RR:= Q;  GG:= V;  BB:= P];
                  2:    [RR:= P;  GG:= V;  BB:= T];
                  3:    [RR:= P;  GG:= Q;  BB:= V];
                  4:    [RR:= T;  GG:= P;  BB:= V];
                  5:    [RR:= V;  GG:= P;  BB:= Q]
                other   [exit 1];
                R:= fix(RR*255.);
                G:= fix(GG*255.);
                B:= fix(BB*255.);
                Point(X+Radius, Radius-Y, R<<16+G<<8+B);
                ];
        ];
]