When developing bitmap programs, you often need to convert the color of bitmap between the RGB and HSV color spaces. The color conversion is implemented by C ++:
RGB color space to HSV color value:
Void Rgb2Hsv (float R, float G, float B, float & H, float & S, float & V)
{
// R, g, B values are from 0 to 1
// H = [0,360], s = [], v = []
// If s = 0, then h =-1 (undefined)
Float min, max, delta, tmp;
Tmp = min (R, G );
Min = min (tmp, B );
Tmp = max (R, G );
Max = max (tmp, B );
V = max; // v
Delta = max-min;
If (max! = 0)
S = delta/max; // s
Else
{
// R = g = B = 0 // s = 0, v is undefined
S = 0;
H = UNDEFINEDCOLOR;
Return;
}
If (R = max)
H = (G-B)/delta; // between yellow & magenta
Else if (G = max)
H = 2 + (B-R)/delta; // between cyan & yellow
Else
H = 4 + (R-G)/delta; // between magenta & cyan
H * = 60; // degrees
If (H <0)
H ++ = 360;
}
Convert the HSV color space to the RGB color value:
Void HSV 2rgb (float H, float S, float V, float & R, float & G, float & B)
{
Int I;
Float f, p, q, t;
If (S = 0)
{
// Achromatic (gray)
R = G = B = V;
Return;
}
H/= 60; // sector 0 to 5
I = floor (H );
F = H-I; // factorial part of h
P = V * (1-S );
Q = V * (1-S * f );
T = V * (1-S * (1-f ));
Switch (I)
{
Case 0:
R = V;
G = t;
B = p;
Break;
Case 1:
R = q;
G = V;
B = p;
Break;
Case 2:
R = p;
G = V;
B = t;
Break;
Case 3:
R = p;
G = q;
B = V;
Break;
Case 4:
R = t;
G = p;
B = V;
Break;
Default: // case 5:
R = V;
G = p;
B = q;
Break;
}
}