Sunday, June 24, 2012

39. CONVERSION OF COLOR MODELS


//CONVERSION OF COLOR MODELS
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float r,g,b,y,i,c,m,h,s,v;
int ch;
clrscr();
do
{

printf(“\n\t ** color conversion**”);
printf(“\n 1: RGB->YIG \n 2.YIQ->RGB\n 3: RGB->CMY \n 4:CMY->RGB\n 5:RGB->HSV\n 6:HSV->RGB \n 7: EXIT”);
printf(“\n enter the choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
{
int p,d,l;
float w[3][1],z[3][1];
float x[3][3]={.299,.587,0.144,.596,-0.275,-.321,.122,-.528,.311};
printf(“enter the RGB values (0-1) in matrix w:”);
for(p=0;p<3;p++)
scanf(“%f”,&w[p][0]);
for(p=0;p<3;p++)
{
for(d=0;d<1;d++)
{
z[p][d]=0;
for(l=0;l<3;l++)
z[p][d]=z[p][d]+x[p][l]*w[l][d];
}
}
printf(“\n converted Y value for R value %f is %f “,w[0][0],z[0][0]);
printf(“\n converted I value for G value %f is %f “,w[1][0],z[1][0]);
printf(“\n converted Q value for B value %f is %f “,w[2][0],z[2][0]);
}
break;
case 2:
{
int p,d,l;
float w[3][1],z[3][1];
float x[3][3]={1.000,.956,.620,1.000,-.272,-.647,1.000,-1.108,1.705};
printf(“\n convert YIQ values (0-1) in matrix w:”);
for(p=0;p<3;p++)
scanf(“%f”,&w[p][0]);
for(p=0;p<3;p++)
{
for(d=0;d<1;d++)
{
z[p][d]=0;
for(l=0;l<3;l++)
z[p][d]=z[p][d]+x[p][l]*w[l][d];
}
}
printf(“\n converted R value for Y value %f is %f “,w[0][0],z[0][0]);
printf(“\n converted Y value for I value %f is %f “,w[1][0],z[1][0]);
printf(“\n converted B value for Q value %f is %f “,w[2][0],z[2][0]);
}
break;
case 3:
{
int p,d,l;
float w[3][1],z[3][1];
float x[3][1]={1,1,1,};
printf(“\n enter the RGB value (0-1) in matrix W:”);
for(p=0;p<3;p++)
scanf(“%f”,&w[p][0]);
for(p=0;p<3;p++)
z[p][0]=x[p][0]-w[p][0];
printf(“\n converted C value for R value %f is %f “,w[0][0],z[0][0]);
printf(“\n converted M value for G value %f is %f “,w[1][0],z[1][0]);
printf(“\n converted Y value for B value %f is %f “,w[2][0],z[2][0]);
}
break;
case 4:
{
int p,d,l;
float w[3][1],z[3][1];
float x[3][1]={1,1,1,};
printf(“\n enter the CMY value (0-1) in matrix W:”);
for(p=0;p<3;p++)
scanf(“%f”,&w[p][0]);
for(p=0;p<3;p++)
z[p][0]=x[p][0]-w[p][0];
printf(“\n converted R value for C value %f is %f “,w[0][0],z[0][0]);
printf(“\n converted G value for M value %f is %f “,w[1][0],z[1][0]);
printf(“\n converted B value for Y value %f is %f “,w[2][0],z[2][0]);
}
break;
case 5:
{
float r,g,b,*h,*s,*v;
float max,min,fmax,fmin;
float delta;
printf(“enter the RGB value (0-1)”);
scanf(“%f%f%f”,&r,&g,&b);
if(g>b)
{
fmax=g;
fmin=b;
}
else
{
fmax=r;
fmin=g;
}
if(r>fmax)
{
max=r;
min=fmax;
}
else
{
max=fmax;
min=r;
}
*v=max;
if(max!=0.0)
*s=delta/max;
else
*s=0.0;
if(*s==0.0)
*h=-1;
else
{
if(*s==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(” converted value of h is %f”,*h);
printf(“converted value of s is %f”,*s);
printf(“converted value of v is %f”,*v);
}
break;
case 6:
{
int k;
float h,s,v,*r,*g,*b;
float aa,bb,cc,f;
printf(“enter the HSB value (0-1):”);
scanf(“%f%f%f”,&h,&s,&v);
if(s==0)
*r=*g=*b=v;
else
{
if(h==1.0)
h=0;
h*=6.0;
k=floor(h);
f=h-k;
aa=v*(1-s);
bb=v*(1-(s*f));
cc=v*(1-(s*(1-f)));
switch(k)
{
case 0:
*r=v;*g=cc;*b=aa;
break;
case 1:
*r=bb;*g=v;*b=aa;
break;
case 2:
*r=aa;*g=v;*b=cc;
break;
case 3:
*r=aa;*g=bb;*b=v;
break;
case 4:
*r=cc;*g=aa;*b=v;
break;
case 5:
*r=v;*g=aa;*b=bb;
break;
}
}
printf(“converted r value is %f”,*r);
printf(“converted g value is %f”,*g);
printf(“converted b value is %f “,*b);
}
break;
}
}while(ch!=7);
getch();
}

SAMPLE OUTPUT:
Color conversion
1)RGB->YIQ
2)YIQ->RGB
3)RGB->CMY
4)CMY->RGB
5)RGB->HSV
6)HSV->RGB
7)EXIT
ENTER YOUR CHOICE:1
Enter RGB values(0-1) in matrix w: 1     0       0
Converted Y value for R value  :1.000000 is 0.299000
Converted I value for G value    :0.000000 is 0.596000
Converted Q value for B value   :0.000000 is 0.122000

ENTER YOUR CHOICE: 2
Enter YIQ values(0-1) in matrix w: 1     1        1
Converted R value for Y value  :1.000000 is 2.576000
Converted G value for I value    :1.000000 is 0.081000
Converted B value for Q value   :1.000000 is 1.597000

ENTER YOUR CHOICE :3
Enter RGB values(0-1) in matrix w: 1     1        1
Converted C value for R value     :1.000000 is 0.000000
Converted M value for G value    :0.000000 is 0.000000
Converted Y value for B value      :0.000000 is 0.000000

ENTER YOUR CHOICE : 4
Enter CMY values(0-1) in matrix w: 0    1    0
Converted R value for C value     :0.000000 is 1.000000
Converted G value for M value    :1.000000 is 0.000000
Converted B value for Y value      :0.000000 is 1.000000

ENTER YOUR CHOICE :5
ENTER RGB VALUE (0-1) IN w:  1       1        1
Converted value of h  is  0.333333
Converted value of s is   -0.000000
Converted value of v is    1.000000

ENTER YOUR CHOICE :6
ENTER HSV VALUE (0-10 IN W  : 1            1          1
Converted value of r is  1.000000
Converted value of g is   0.000000
Converted value of b is   0.000000