Path: utzoo!utgpu!water!watmath!clyde!rutgers!ames!sgi!paul From: paul@sgi.SGI.COM (Paul Haeberli) Newsgroups: comp.graphics Subject: RGB to printer CMYK conversion Message-ID: <10258@sgi.SGI.COM> Date: 1 Feb 88 19:45:55 GMT Organization: Silicon Graphics Inc, Mountain View, CA Lines: 123 Someone asked about converting from RGB to prinyer CMYK. Here is a simple conversion technique. Paul Haeberli /* * RGBtoCMYK - * This program demonstrates an easy way to convert from * RGB space to printer CMY and K. This method does grey component * replacement, so achromatic parts of the image will be printed with * only black ink. * * R is red. C is cyan. * G is geen. M is magenta. * B is blue. Y is yellow. * K is black. * * Paul Haeberli / Silicon Graphics - 1987 */ main() { /* print header */ printf("r\tg\tb\t|\tc\tm\ty\tk\n"); /* transform the corners of the color cube */ printf("-----------------------------------------------------------\n"); printvals(0,0,0); printvals(0,0,255); printvals(0,255,0); printvals(0,255,255); printvals(255,0,0); printvals(255,0,255); printvals(255,255,0); printvals(255,255,255); /* up one edge of the color cube */ printf("-----------------------------------------------------------\n"); printvals(0,0,0); printvals(0,0,32); printvals(0,0,64); printvals(0,0,128); printvals(0,0,192); printvals(0,0,255); /* transform a few a chromatic colors */ printf("-----------------------------------------------------------\n"); printvals(0,0,0); printvals(1,1,1); printvals(16,16,16); printvals(32,32,32); printvals(64,64,64); printvals(128,128,128); printvals(192,192,192); printvals(255,255,255); } printvals(r,g,b) int r, g, b; { int c,m,y,k; rgb_to_cmyk(r,g,b,&c,&m,&y,&k); printf("%d\t%d\t%d\t|\t%d\t%d\t%d\t%d\n",r,g,b,c,m,y,k); } /* * rgb_to_cmyk - * Convert from rgb to cmyk. This implements grey component * replacement. * * Inputs: * r intensity 0 to 255 * g intensity 0 to 255 * b intensity 0 to 255 * * Outputs: * c coverage 0 to 255 * m coverage 0 to 255 * y coverage 0 to 255 * k coverage 0 to 255 * * * An input value of rgb=[255,255,255] represents white. When this is * transformed to cmyk, we get cmyk=[0,0,0,0] which represents zero * coverage. * * An input value of rgb=[128,128,128] represents 50 percent grey. When * this is transformed to cmyk, we get cmyk=[0,0,0,127] which represents * zero coverage by cmy, and 50 percent coverage by black. * */ rgb_to_cmyk(r,g,b,c,m,y,k) int r, g, b; int *c, *m, *y, *k; { int i; /* i is the max of r, g, and b */ i = 0; if(r>i) i = r; if(g>i) i = g; if(b>i) i = b; /* if r, g and b are all zero then print full black */ if(i == 0) { *c = 0; *m = 0; *y = 0; *k = 255; return; } r = (255*r)/i; g = (255*g)/i; b = (255*b)/i; *c = 255-r; *m = 255-g; *y = 255-b; *k = 255-i; }