[Rd] postscript(colormodel = "cmyk") conversion is wrong

Jeffrey J. Hallman jhallman at frb.gov
Thu May 22 22:34:41 CEST 2008


The conversion of RGB to CMYK takes place in PostScriptSetCol() starting at
line 2900 of R-2.7.0/src/library/grDevices/src/devPS.c

	if(strcmp(mm, "cmyk") == 0) {
	    double c = 1.0-r, m=1.0-g, y=1.0-b, k=c;
	    k = fmin2(k, m);
	    k = fmin2(k, y);
	    if(k == 1.0) c = m = y = 0.0;
	    else {c /= (1.-k); m /= (1.-k); y /= (1.-k);}

r, g, and b have already been normalized to the range [0,1] before the
function was called, so this is almost right.  The last line should actually
be something like

else { c = (c - k)/(1 - k); m = (m - k)/(1 - k); y = (y - k)/(1 - k);}

Here is some R code I wrote that does the conversion, so you can see what it's
supposed to be doing:

rgb2cmyk <- function(rgb){
  ## rgb is a vector of 3 numbers in the 0 to 255 range
  ## returns cmyk vector of 4 numbers in [0,1]
  cmy <- 1 - rgb/255
  names(cmy) <- c("c", "m", "y")
  k <- min(min(min(1, cmy[1]), cmy[2]), cmy[3])
  if(k == 1) c(cmy, k = 1)
  else c(c(cmy - k)/(1-k), k = k)
}

-- 
Jeff



More information about the R-devel mailing list