Skip to content

Instantly share code, notes, and snippets.

@justjkk
Created April 14, 2010 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save justjkk/365867 to your computer and use it in GitHub Desktop.
Save justjkk/365867 to your computer and use it in GitHub Desktop.
Conversion between Color Models(buggy)
// Conversion between color models(buggy)
/* Following is one of the best examples of how worse college given programs
can be. Proceed at your OWN RISK */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm,n;
float r=0,g=0,b=0;
float y=0,i=0,q=0;
float c=0,m=0,y1=0;
initgraph(&gd,&gm,"");
printf("(r,g,b)keys for incrementing R,G,B values respectively\n");
printf("(shift+(r,g,b))keys for decrementing R,G,B values respectively\n");
printf("press esc to exit\n");
setcolor(15);
while(1)
{
gotoxy(18,10);
printf("R G B");
c=1.0-r;
m=1.0-g;
y1=1.0-y;
gotoxy(47,10);
printf("C M Y");
y=0.299*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;
gotoxy(18,23);
printf("Y I Q");
switch(getche())
{
case 'r':
r++;
break;
case 'g':
g++;
break;
case 'b':
b++;
break;
case 'R':
r--;
break;
case 'G':
g--;
break;
case 'B':
b--;
break;
case 27:
closegraph();
exit(0);
}
if(r>255)
r=0;
if(g>255)
g=0;
if(b>255)
b=0;
setrgbpalette(1,r,g,b);
setfillstyle(1,1);
bar(50,50,270,250);
rectangle(50,50,270,250);
setrgbpalette(2,c,m,y1);
setfillstyle(1,2);
bar(275,50,495,250);
rectangle(275,50,495,250);
setrgbpalette(3,y,i,q);
setfillstyle(1,3);
bar(50,255,270,455);
rectangle(50,255,270,455);
}
}
/****************************************************************************\
* Ex.No : 6a *
* Title : Conversion between Color Models(RGB,CMY,YIQ) *
* Author : jkk *
* Date : 2009-12-29 *
* Dependancy : This program uses Turbo C BGI Library(graphics.h) *
\****************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
enum {RGB_MODE,CMY_MODE,YIQ_MODE} mode;
typedef struct RGB
{
int r; //Red [0..255]
int g; //Green [0..255]
int b; //Blue [0..255]
}RGB;
typedef struct CMY
{
int c; //Cyan [0..255]
int m; //Magenta [0..255]
int y; //Yellow [0..255]
}CMY;
typedef struct YIQ
{
float y; //Luminance[0..1]
float i; //Cyan-Orange Hue information [-0.596..0.596]
float q; //Green-Magenta Hue information [-0.523..0.523]
}YIQ;
CMY RGBtoCMY(RGB color);
YIQ RGBtoYIQ(RGB color);
RGB CMYtoRGB(CMY color);
RGB YIQtoRGB(YIQ color);
void main()
{
int gd=DETECT,gm;
char ch;
RGB rgb;
CMY cmy;
YIQ yiq;
initgraph(&gd,&gm,"C:/TC/BGI");
setfillstyle(SOLID_FILL,1);
rgb.r=0,rgb.g=0,rgb.b=0;
mode=RGB_MODE;
do
{
cleardevice();
gotoxy(1,1);
if(mode==RGB_MODE)
{
cmy=RGBtoCMY(rgb);
yiq=RGBtoYIQ(rgb);
}
else if(mode==CMY_MODE)
{
rgb=CMYtoRGB(cmy);
yiq=RGBtoYIQ(rgb);
}
else if(mode==YIQ_MODE)
{
rgb=YIQtoRGB(yiq);
cmy=RGBtoCMY(rgb);
}
printf("Press r,g,b,R,G,B to increment/decrement r,g,b value\n");
printf("Press c,m,y,C,M,Y to increment/decrement c,m,y value\n");
printf("Press u,i,q,U,I,Q to increment/decrement y,i,q value\n");
printf("Press Escape to exit...");
printf("\n(r=%d,g=%d,b=%d)",rgb.r,rgb.g,rgb.b);
printf("\n(c=%d,m=%d,y=%d)",cmy.c,cmy.m,cmy.y);
printf("\n(y=%.2f,i=%.2f,q=%.2f)",yiq.y,yiq.i,yiq.q);
setrgbpalette(1,rgb.r>>2,rgb.g>>2,rgb.b>>2);
setfillstyle(SOLID_FILL,1);
bar(150,150,350,350);
ch=getch();
switch(ch)
{
case 'r': rgb.r=(rgb.r+1)%256; mode=RGB_MODE; break;
case 'g': rgb.g=(rgb.g+1)%256; mode=RGB_MODE; break;
case 'b': rgb.b=(rgb.b+1)%256; mode=RGB_MODE; break;
case 'R': rgb.r=(rgb.r+255)%256; mode=RGB_MODE; break;
case 'G': rgb.g=(rgb.g+255)%256; mode=RGB_MODE; break;
case 'B': rgb.b=(rgb.b+255)%256; mode=RGB_MODE; break;
case 'c': cmy.c=(cmy.c+1)%256; mode=CMY_MODE; break;
case 'm': cmy.m=(cmy.m+1)%256; mode=CMY_MODE; break;
case 'y': cmy.y=(cmy.y+1)%256; mode=CMY_MODE; break;
case 'C': cmy.c=(cmy.c+255)%256; mode=CMY_MODE; break;
case 'M': cmy.m=(cmy.m+255)%256; mode=CMY_MODE; break;
case 'Y': cmy.y=(cmy.y+255)%256; mode=CMY_MODE; break;
case 'u': yiq.y+=0.01; if(yiq.y>1) yiq.y=0; mode=YIQ_MODE; break;
case 'i': yiq.i+=0.01; if(yiq.i>0.596) yiq.i=-0.596; mode=YIQ_MODE; break;
case 'q': yiq.q+=0.01; if(yiq.q>0.532) yiq.q=-0.532; mode=YIQ_MODE; break;
case 'U': yiq.y-=0.01; if(yiq.y<0) yiq.y=1; mode=YIQ_MODE; break;
case 'I': yiq.i-=0.01; if(yiq.i<-0.596) yiq.i=0.596; mode=YIQ_MODE; break;
case 'Q': yiq.q-=0.01; if(yiq.q<-0.532) yiq.q=0.532; mode=YIQ_MODE; break;
case 27: closegraph(); exit(0);
}
}while(1);
/*
printf("Enter RGB value: ");
scanf("%d %d %d",&(rgb.r),&(rgb.g),&(rgb.b));
printf("Converted CMY value is: ");
cmy = RGBtoCMY(rgb);
printf("%d %d %d",cmy.c,cmy.m,cmy.y);
printf("\nConverted YIQ value is: ");
yiq = RGBtoYIQ(rgb);
printf("%.2f %.2f %.2f",yiq.y,yiq.i,yiq.q);
getch();
*/
}
/* RGBtoCMY Transformation Matrix
/ \ / \ / \
| c | | -1 0 0 255 | | r |
| m | = | 0 -1 0 255 |*| g |
| y | | 0 0 -1 255 | | b |
| t1 | | 0 0 0 1 | | t |
\ / \ / \ /
*/
CMY RGBtoCMY(RGB rgb)
{
CMY cmy;
cmy.c = 255 - rgb.r;
cmy.m = 255 - rgb.g;
cmy.y = 255 - rgb.b;
return cmy;
}
/* RGBtoYIQ Transformation Matrix
/ \ / \ / \
| y | | 0.299 0.587 0.114 0 | | r |
| i | = | 0.596 -0.275 -0.321 0 |*| g |
| q | | 0.212 -0.523 0.311 0 | | b |
| t1 | | 0 0 0 255 | | t |
\ / \ / \ /
*/
YIQ RGBtoYIQ(RGB rgb)
{
YIQ yiq;
float r = rgb.r/255.0; // Converting to be in the range [0..1]
float g = rgb.g/255.0;
float b = rgb.b/255.0;
yiq.y = 0.299*r+0.587*g+0.114*b;
yiq.i = 0.596*r-0.275*g-0.321*b;
yiq.q = 0.212*r-0.523*g+0.311*b;
return yiq;
}
/* CMYtoRGB Transformation Matrix
/ \ / \ / \
| r | | -1 0 0 255 | | c |
| g | = | 0 -1 0 255 |*| m |
| b | | 0 0 -1 255 | | y |
| t1 | | 0 0 0 1 | | t |
\ / \ / \ /
*/
RGB CMYtoRGB(CMY cmy)
{
RGB rgb;
rgb.r = 255 - cmy.c;
rgb.g = 255 - cmy.m;
rgb.b = 255 - cmy.y;
return rgb;
}
/* YIQtoRGB Transformation Matrix
/ \ / \ / \
| r | | 1.000 0.956 0.620 0 | | y |
| g | = | 1.000 -0.272 -0.647 0 |*| i |
| b | | 1.000 -1.108 1.705 0 | | q |
| t1 | | 0 0 0 1/255 | | t |
\ / \ / \ /
*/
RGB YIQtoRGB(YIQ yiq)
{
RGB rgb;
float y=yiq.y;
float i=yiq.i;
float q=yiq.q;
rgb.r = (y+0.956*i+0.620*q)*255;
rgb.g = (y-0.272*i-0.647*q)*255;
rgb.b = (y-1.108*i+1.705*q)*255;
return rgb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment