Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!sgi!paul@manray.sgi.com From: paul@manray.sgi.com (Paul Haeberli) Newsgroups: comp.graphics Subject: conversion from RGB to CMYK Message-ID: <50957@sgi.sgi.com> Date: 16 Feb 90 10:02:17 GMT Sender: news@sgi.sgi.com Organization: Silicon Graphics, Inc., Mountain View, CA Lines: 90 Here is a little code fragment that converts from rgb to CMYK. We have used this transformation for several years as part of the process of creating digital color separations for various publications. Paul Haeberli paul@sgi.com /* * rgb_to_cmyk - * Convert from rgb to cmyk. This implements 100% 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. * * Paul Haeberli - 1988 * */ #define ULTRAC 160 #define ULTRAM 160 #define ULTRAY 0 #define ULTRAK 255 #define BLACKLERP 4 rgb_to_cmyk(r,g,b,c,m,y,k) register int r, g, b; int *c, *m, *y, *k; { register int i; float param; /* 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 k plus some m and c */ if(i == 0) { *c = ULTRAC; *m = ULTRAM; *y = ULTRAY; *k = ULTRAK; 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; /* if i is less than blacklerp start lerp towards ultra-black */ if(i