You are on page 1of 5

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

HSV TO RGB COLOR MODELS


#include "stdio.h"
#include "conio.h"
#include "math.h"
void hsvtorgb(float h,float s,float v,float *r,float *g,float *b)
{
int i;
float aa,bb,cc,f;
if(s==0)
*r=*g=*b=v;
else
{
if(h==1.0)h=0;
h*=6.0;
i=floor(h);
f=h-i;
aa=v*(1-s);
bb=v*(1-(s*f));
cc=v*(1-(s*(1-f)));
switch(i)
{
case 0:
*r=v;
*g=cc;
*b=aa;
printf("R= %f G=%f B=%f",*r,*g,*b);
break;
case 1:
*r=bb;
*g=v;
*b=aa;
printf("R= %f G=%f B=%f",*r,*g,*b);
break;
case 2:
*r=aa;
*g=v;
*b=cc;
printf("R= %f G=%f B=%f",*r,*g,*b);
break;

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

You might also like