Professional Documents
Culture Documents
RGB To HSV Color Models
RGB To HSV Color Models
#include "math.h"
#define MIN(a,b) (a<b?a:b)
#define MAX(a,b) (a>b?a:b)
#define NO_HUE -1
void rgbtohsv(float r,float g,float b,float *h,float *s,float *v)
{
float max=MAX(r,MAX(g,b)),min=MIN(r,MIN(g,b));
float delta=max-min;
*v=max;
if(max!=0.0)
*s=delta/max;
else
*s=0.0;
if(*s==0.0) *h=NO_HUE;
else
{
if(r==max)
*h=(g-b)/delta;
else if(g==max)
*h=2+(b-r)/delta;
else if(b==max)
*h=4+(r-g)/delta;
*h*=60.0;
if(*h<0)*h+=360.0;
*h/=360.0;
}
printf("H=%f S= %f V=%f",*h,*s,*v);
}
void main()
{
float a,b,c,d,e,f;
clrscr();
printf("Enter the RGB and HSV values :");
scanf("%f%f%f%f%f%f",&a,&b,&c,&d,&e,&f);
rgbtohsv(a,b,c,&d,&e,&f);
getch();
}
OUTPUT
Enter RGB and HSV Values:
0.1
0.2
0.3
0.4
0.5
0.6
H= 0.5833
S= 0.6667
V= 0.3000
case 3:
*r=aa;
*g=bb;
*b=v;
printf("R= %f G=%f B=%f",*r,*g,*b);
break;
case 4:
*r=cc;
*g=aa;
*b=v;
printf("R= %f G=%f B=%f",*r,*g,*b);
break;
case 5:
*r=v;
*g=aa;
*b=bb;
printf("R= %f G=%f B=%f",*r,*g,*b);
break;
}
}
}
void main()
{
float a,b,c,d,e,f;
clrscr();
printf("\n Enter HSV and RGB Values :");
scanf("%f%f%f%f%f%f",&a,&b,&c,&d,&e,&f);
hsvtorgb(a,b,c,&d,&e,&f);
getch();
}
OUTPUT
Enter HSV and RGB Values:
0.1
0.2
0.3
0.4
0.5
0.6
R= 0.3000
G= 0.2760
B= 0.2400