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
)
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!