# Write Source Code in C Conversion of Color Models

To write a C program to perform conversion of different color models.
Step by Step Algorithm Color Model
1. Include the graphics header file
2. Get the values of RGB and display then convert into CMY then display it.
3. Get the values of CMY and display then convert into RGB then display it.
4. Get the values of RGB and display then convert into YIQ then display it.
5. Get the values of YIQ and display then convert into RGB then display it.
Source Code Programming Algorithm
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,ch,ch1;
float r=0,g=0,b=0,c=0,m=0,v=0,y=0,i=0,q=0;
clrscr();
initgraph(&gd,&gm,"D:\\TC\\BGI");
while(1)
{
printf("Color conversion\n 1.RGB <-> YIQ\n 2.RGB <-> CMV\n 3.Exit\n Enter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n ****RGB <-> YIQ****\n 1.RGB <-> YIQ\n 2.YIQ <-> RGB\n Enter your choice:\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
initgraph(&gd,&gm,"D:\\TC\\BGI");
printf("Enter the value of R,G,B:\n");
scanf("%f%f%f",&r,&g,&b);
y=0.2899*r+0.587*g+0.144*b;
i=0.596*r-0.275*g-0.3218*b;
q=0.212*r-0.528*g+0.311*b;
setrgbpalette(1,r,g,b);
setfillstyle(1,1);
bar(50,50,270,250);
rectangle(50,50,270,250);
setrgbpalette(1,y,i,q);
setfillstyle(1,3);
bar(50,280,270,470);
rectangle(50,280,270,470);
break;
case 2:
initgraph(&gd,&gm,"D:\\TC\\BGI ");
printf("Enter the value of Y,I,Q:\n");
scanf("%f%f%f",&y,&i,&q);
r=1.000*y+0.956*i+0.620*q;
g=1.000*y-0.272*i-0.647*q;
b=1.000*y-1.108*i+1.705*q;
setrgbpalette(1,y,i,q);
setfillstyle(1,1);
bar(50,50,270,250);
rectangle(50,50,270,250);
setrgbpalette(1,r,g,b);
setfillstyle(1,3);
bar(50,280,270,470);
rectangle(50,280,270,470);
break;
}
break;
case 2:
printf("\n ****RGB <-> CMV****\n 1.RGB <-> CMV\n 2.CMV <-> RGB\n Enter your choice:\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
initgraph(&gd,&gm," D:\\TC\\BGI");
printf("Enter the value of R,G,B:\n");
scanf("%f%f%f",&r,&g,&b);
c=1.0-r;
m=1.0-g;
v=1.0-b;
setrgbpalette(1,r,g,b);
setfillstyle(1,1);
bar(50,50,270,250);
rectangle(50,50,270,250);
setrgbpalette(1,c,m,v);
setfillstyle(1,3);
bar(50,280,270,470);
rectangle(50,280,270,470);
break;
case 2:
initgraph(&gd,&gm," D:\\TC\\BGI");
printf("Enter the value of C,M,V:\n");
scanf("%f%f%f",&c,&m,&v);
r=1.0-c;
g=1.0-m;
b=1.0-v;
setrgbpalette(1,c,m,v);
setfillstyle(1,1);
bar(50,50,270,250);
rectangle(50,50,270,250);
setrgbpalette(1,r,g,b);
setfillstyle(1,3);
bar(50,280,270,470);
rectangle(50,280,270,470);
break;
}
break;

case 3:
exit(0);
}
}
getch();
}
Color Conversion Model output
Color conversion

1.RGB <-> YIQ
2.RGB <-> CMV
3.Exit

1

****RGB <-> YIQ****

1.RGB <-> YIQ
2.YIQ <-> RGB