Search code examples
crgbcolor-pickerhsv

Drawing HSV Gradient failed


For the drawing process i have these 3 functions:

void setpen(R, G, B) - Sets the current brush's color

void putpixel(X, Y) - puts pixel at X Y using the brush

I am trying to draw the HSV gradient:

enter image description here

using this pair of code:

int S, H;

for(S = 0; S<255; S++)
{
    for(H = 0; H<255; H++)
    {
        setpen(S, H, H);
        putpixel(S, H);
    }
}

But the gradient drawn from this code is:

enter image description here

Not like it looks bad, but its not the result i really want. Also as i see there is something correct at the top-right side.. but at the bottom-left it goes in cyan. Please, whats wrong with my code?


Solution

  • From what you have shown, you want a gradient with fixed hue (red), saturation going up and value going right.

    When you convert a color from HSV to RGB with H = 0 (red hue) :
    - R = V
    - G = B = V * (1 - S)

    In your case, for some x and y in [0; 255] you want the following :
    - V = x, hence R = x ;
    - S = 255-y, hence G = B = x * y.

    So you can change your call to setpen like this :
    setpen(S, (H*S) / 255, (H*S) / 255);