# CMY colour function in R

Is there a CMY colour function in any R package equivalent to rgb()? There seems to be nothing in {base} or e.g. {colourSpace}. I have a custom function which I can post here, but a native would be preferable.

## Answers

this should work

```#' assumes integer input for CMYK
cmyk <- function(C,M,Y,K) {

C <- C / 100.0
M <- M / 100.0
Y <- Y / 100.0
K <- K / 100.0

n.c <- (C * (1-K) + K)
n.m <- (M * (1-K) + K)
n.y <- (Y * (1-K) + K)

r.col <- ceiling(255 * (1-n.c))
g.col <- ceiling(255 * (1-n.m))
b.col <- ceiling(255 * (1-n.y))

return(col2rgb(sprintf("#%02s%02s%02s",
as.hexmode(r.col),
as.hexmode(g.col),
as.hexmode(b.col))))

}

> cmyk(0,50,85,0)
[,1]
red    255
green  128
blue    39

> cmyk(0,0,100,0)
[,1]
red    255
green  255
blue     0
```

Well in addition to @hrbrmstr's function here is mine for what its worth. I haven't factored in black as I understand that's primarily for printing but I'd see this primarily as a dataviz tool. It should work with vectors / matrices / alphas.

```cmy = function(c, m, y, alpha, maxColorValue=1){
if(maxColorValue != 1) { c <- c/maxColorValue; m <- m/maxColorValue; y <- y/maxColorValue }
c <- 1-c; m <- 1-m; y <- 1-y
hex <- function(v) substring(rgb(v,0,0),2,3)
if(!missing(alpha)) alpha <- hex(alpha) else alpha <- ''
paste0('#',hex(c), hex(m), hex(y), alpha)
}
```

If you're into code porting, I found a couple MatLab Central possibilities:

http://www.mathworks.com/matlabcentral/fileexchange/25350-fit-multi-dimensional-polynomial (despite the name, the description says 'CMYK to Lab'

http://www.mathworks.com/matlabcentral/fileexchange/45723-convert-rgb-image-to-cmy-image-and-extract-each-component-cyan-c-magenta-m-and-yellow-y

EDIT: if you'd like to invert hrbmstr's code, here's some javascript which can trivially be converted to R Courtesy of http://www.javascripter.net/faq/rgb2cmyk.htm ):

``` if (r==0 && g==0 && b==0) {
computedK = 1;
return [0,0,0,1];
}

computedC = 1 - (r/255);
computedM = 1 - (g/255);
computedY = 1 - (b/255);

var minCMY = Math.min(computedC,
Math.min(computedM,computedY));
computedC = (computedC - minCMY) / (1 - minCMY) ;
computedM = (computedM - minCMY) / (1 - minCMY) ;
computedY = (computedY - minCMY) / (1 - minCMY) ;
computedK = minCMY;
```

Yes there is a way of using CMYK colors instead of RGB for printing purpose. If you set the colormodel to CMYK, you can use CMYK colors.

Read the details on this link and an example here

### how to parse xml in C#.net

I have a xml file e.g. like this:

### ZF2 form collections and doctrine 2

I have a ZF2 form with form collection element. And I have doctrine 2 entity. I bind this entity to the form.