 |
RGB to HSV
Conversion
Colors
in RGB space can be converted to HSV (Hue, Saturation and
Value) space. In HSV space color density and light color is more
understandable. It is easy to use by humans.
1. Hue value is shown
in Figure 1. It is represented with degrees from 0° to 360°.
Figure 1.
Hue value of HSV.
2. Saturation
is the amount of white light mixed with Hue and the degree
of purity. It is takes values between 0 and 1.
3. Value
determines the lightness and the darkness of the color. Value 255 is the
lightest and 0 is the darkest color. Table 1. Shows some RGB and HSV values of well known colors.
Table 1. Some HSV converted colors
|

Red
R: 255 H: 240.00º
G: 0 S: 1.00
B: 0 V: 255
|

Blue
R: 0 H: 0.0º
G: 0 S:
1.00
B: 255 V: 255
|

Green
R: 0 H: 120.0º
G: 255 S: 1.00
B: 0 V: 255
|

Yellow
R: 0 H:
180.0º
G: 255 S: 1.00
B: 255 V: 255
|
|

Pink
R: 255 H: 300.0º
G: 0 S: 1.00
B: 255 V: 255
|

????
R: 0 H:
60.0º
G: 255 S: 1.00
B:
255 V: 255
|

Sea Blue
R: 225 H: 32.57º
G: 145 S: 1.00
B: 50 V: 225
|

Orange
R: 0 H:
209.88º
G: 128 S: 1.00
B:
255 V: 255
|
C Source Code
Here
is a procedure calculating HSV values of a number of colors presented in BMP 24-bit
formatted images.
int Batch_Analyse()
{
long i, width, height, R, G, B;
long Value, tmp;
double Saturation, Hue, Cr, Cg, Cb;
long **img_data;
BMPHEADER BmpHdr;
FILE *f, *f_text;
char FileNM[20];
//
//
CheckBmpComp("i000.bmp", &BmpHdr);
f_text = fopen("compare_result.txt","w");
fprintf(f_text, "Here are the RGB ---> HSV convertion results\n");
fprintf(f_text, "----------------------------------------------\n");
for(i=0;i<8;i++)
{
Generate_File_Name(i, "i", FileNM, 2);
f = fopen(FileNM,"rb");
fseek(f, BmpHdr.BmpOffset, SEEK_SET);
R = (unsigned char)fgetc(f);
G = (unsigned char)fgetc(f);
B = (unsigned char)fgetc(f);
/// VALUE
// find maximum
if ( (R >= G) && (R >= B))
Value = R;
else if ( (G >= R) && (G >= B))
Value = G;
else if ( (B >= G) && (B >= R))
Value = B;
/// SATURATION
// find minimum
if ( (R <= G) && (R <= B))
tmp = R;
else if ( (G <= R) && (G <= B))
tmp = G;
else if ( (B <= G) && (B <= R))
tmp = B;
if (Value == 0)
Saturation = 0;
else
Saturation = ((double)Value - (double)tmp)/(double)Value;
/// HUE
if (Saturation == 0)
Hue = -1;
else
{
Cr = ((double)Value-(double)R)/((double)Value-(double)tmp);
Cg = ((double)Value-(double)G)/((double)Value-(double)tmp);
Cb = ((double)Value-(double)B)/((double)Value-(double)tmp);
if (R == Value)
Hue = Cb - Cg;
if (G == Value)
Hue = 2 + Cr - Cb;
if (B == Value)
Hue = 4 + Cg - Cr;
Hue *= 60;
if (Hue < 0)
Hue +=360;
}
fprintf(f_text, "R:%3d G:%3d B:%3d ----> H:%3.2f S:%3.2f V:%3d\n", R, G, B, Hue, Saturation, Value);
fclose(f);
}
fclose(f_text);
return 0;
}
|
 |