We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › CMYK Conversion Class
Page Index Toggle Pages: 1
CMYK Conversion Class (Read 784 times)
CMYK Conversion Class
Dec 2nd, 2008, 7:42pm
 
Wasn't sure where to post this.  Moderators, feel free to move.

I needed to output some generated colours to the user in all the usual formats (HSV, RGB, CMYK, Hex), and ran into the problem that there isn't a colorMode(CMYK) option.  I found this post from the alpha board: http://processing.org/discourse/yabb/YaBB.cgi?board=Tools;action=display;num=1082055374, but it seems to have been abandoned.  So, I used amoeba's pseudo-code from there to make a simple CMYK_Colour class (yes, Canadian spelling Wink )  

The conversion is crude, but works for my purposes.  For printing to CMYK, use SimplePostScript: http://processing.org/hacks/hacks:ps.  Currently, this expects color objects to be in RGB mode.  However, it would be easy to adapt this to convert from HSV (HSB) colour objects.

Code:

class CMYK_Colour {

//fields
float cyan, magenta, yellow, black;

//constructor - requires colorMode(RGB,255) is set
CMYK_Colour(color c) {
//convert to CMY
cyan = 1 - (red(c) / 255);
magenta = 1 - (green(c) / 255);
yellow = 1 - (blue(c) / 255);
//convert to CMYK
black = 1;
if (cyan < black) { black = cyan; }
if (magenta < black) { black = magenta; }
if (yellow < black) { black = yellow; }
cyan = ( cyan - black ) / ( 1 - black );
magenta = ( magenta - black ) / ( 1 - black );
yellow = ( yellow - black ) / ( 1 - black );
//convert to value between 0 and 100
cyan = cyan * 100;
magenta = magenta * 100;
yellow = yellow * 100;
black = black * 100;
}  

}      


Usage:

Code:

//set to colorMode(RGB) - this is the default
colorMode(RGB, 255);
//create colour object (pacific blue)
color swatch = color(0,128,255);
//create CMYK colour object
CMYK_Colour cmyk_swatch = new CMYK_Colour(swatch);
//output CMYK values (these are floats)
println("CYAN: " + cmyk_swatch.cyan);
println("MAGENTA: " + cmyk_swatch.magenta);
println("YELLOW: " + cmyk_swatch.yellow);
println("BLACK: " + cmyk_swatch.black);


Hope someone else finds this useful.  If there are serious flaws, or minor improvements, please post back here!
Re: CMYK Conversion Class
Reply #1 - Dec 4th, 2008, 1:17am
 
johnny wrote on Dec 2nd, 2008, 7:42pm:
[i]
Hope someone else finds this useful.  If there are serious flaws, or minor improvements, please post back here!


I'm glad you did post as this was something I hadn't thought about and now realize I'll encounter as I move to print some of this stuff out in CYMK on my plotter.

One thing I had thought of was using java.awt.color.ColorSpace (just yanking it's guts or including), because it supports ICC Color profiles, which if you're doing any fancy-pants printing you might like as well.

It'd look something like..

java.awt.color.ColorSpace

ColorSpace cmyk = new ColorSpace(ColorSpace.TYPE_CMYK, 4);
float[] values = cmyk.fromRGB(rgbFloatArray);

Regardless, your example will definitely will provide a quick and easy solution for me to get things dialed in.

-n
Page Index Toggle Pages: 1