Jump to content
Developer Wiki and Function Reference Links ×

CMYK to RGB


Petri

Recommended Posts

I will shortly get a text file of paint colours as CMYK values. I was wondering if anyone can interpret the following - or knows how otherwise the conversion to RGB values is done.

Wikipedia tells me this:

Converting CMYK to RGB

To convert, we first convert CMYK to CMY, then convert the CMY value to RGB

Converting now

CMYK 	= {C, M, Y, K }

CMY 	= {C',M',Y'}	= {C(1-K)+K, M(1-K)+K, Y(1-K)+K }

RGB 	= {R,G,B}	= {1-C', 1-M', 1-Y'}

RGB 	= {1 - (C(1 - K) + K), 1 - (M(1 - K) + K), 1 - (Y(1 - K) + K)}

= {(1-C)(1-K), (1-M)(1-K), (1-Y)(1-K)}

...but I' afraid I don't get it.

The purpose is to create gradients in bulk; for this I already have a program that accepts RGB values. The conversion could be done elsewhere (Excel, FileMaker) or in VectorScript.

Link to comment

Here's the function lists for future reference:

// ColourMod v1.8 - DHTML Dynamic Color Picker/Selector

// ? 2005 ColourMod.com

// Produced By The Noah Institute (www.noahinstitute.org)

// Design/Programming By Stephen Hallgren (www.teevio.net)

/***************************/

//Color Conversion Functions

/***************************/

function HSVToRGB(H, S, V) {

H = H/360;

S = S/100;

V = V/100;

if (S <= 0) {

V = Math.round(V*255);

rgb['red'] = V;

rgb['green'] = V;

rgb['blue'] = V;

return rgb;

} else {

if (H >= 1.0) {

H = 0;

}

H = 6 * H;

F = H - Math.floor(H);

P = Math.round(255 * V * (1.0 - S));

Q = Math.round(255 * V * (1.0 - (S * F)));

T = Math.round(255 * V * (1.0 - (S * (1.0 - F))));

V = Math.round(255 * V);

switch (Math.floor(H)) {

case 0:

R = V;

G = T;

B = P;

break;

case 1:

R = Q;

G = V;

B = P;

break;

case 2:

R = P;

G = V;

B = T;

break;

case 3:

R = P;

G = Q;

B = V;

break;

case 4:

R = T;

G = P;

B = V;

break;

case 5:

R = V;

G = P;

B = Q;

break;

}

rgb['red'] = R;

rgb['green'] = G;

rgb['blue'] = B;

return rgb;

}

}

function RGBToHex(R,G,B) {

return (toHex®+toHex(G)+toHex(B));

}

function toHex(N) {

if (N==null)

return "00";

N=parseInt(N);

if (N==0 || isNaN(N))

return "00";

N=Math.max(0,N);

N=Math.min(N,255);

N=Math.round(N);

return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);

}

function HexToRGB(H) {

hexR = H.substr(0,2);

rgb['red'] = parseInt((hexR).substring(0,2),16);

hexG = H.substr(2,2);

rgb['green'] = parseInt((hexG).substring(0,2),16);

hexB = H.substr(4,2);

rgb['blue'] = parseInt((hexB).substring(0,2),16);

return rgb;

}

function RGBToHSV (R,G,B) {

var max = Math.max(R,G,B);

var min = Math.min(R,G,B);

var delta = max-min;

V = Math.round((max / 255) * 100);

if(max != 0){

S = Math.round(delta/max * 100);

}else{

S = 0;

}

if(S == 0){

H = 0;

}else{

if(R == max){

H = (G - B)/delta;

}else if(G == max){

H = 2 + (B - R)/delta;

}else if(B == max){

H = 4 + (R - G)/delta;

}

H = Math.round(H * 60);

if(H > 360){

H = 360;

}

if(H < 0){

H += 360;

}

}

hsv['hue'] = H;

hsv['sat'] = S;

hsv['val'] = V;

return hsv;

}

function RGBToCMYK (R,G,B) {

//RGB values = 0 ? 255

//CMY values = 0 ? 1

cmyk['C'] = 1 - ( R / 255 );

cmyk['M'] = 1 - ( G / 255 );

cmyk['Y'] = 1 - ( B / 255 );

cmyk['K'] = 1

if ( cmyk['C'] < cmyk['K'] )

cmyk['K'] = cmyk['C'];

if ( cmyk['M'] < cmyk['K'] )

cmyk['K'] = cmyk['M'];

if ( cmyk['Y'] < cmyk['K'] )

cmyk['K'] = cmyk['Y'];

cmyk['C'] = ( cmyk['C'] - cmyk['K'] ) / ( 1 - cmyk['K'] );

cmyk['M'] = ( cmyk['M'] - cmyk['K'] ) / ( 1 - cmyk['K'] );

cmyk['Y'] = ( cmyk['Y'] - cmyk['K'] ) / ( 1 - cmyk['K'] );

cmyk['C'] = Math.round(cmyk['C']*100);

cmyk['M'] = Math.round(cmyk['M']*100);

cmyk['Y'] = Math.round(cmyk['Y']*100);

cmyk['K'] = Math.round(cmyk['K']*100);

if (!cmyk['C'])

cmyk['C'] = 0;

if (!cmyk['M'])

cmyk['M'] = 0;

if (!cmyk['Y'])

cmyk['Y'] = 0;

if (!cmyk['K'])

cmyk['K'] = 0;

return cmyk;

}

function CMYKToRGB (C,M,Y,K) {

C = C/100;

M = M/100;

Y = Y/100;

K = K/100;

//Where CMYK and CMY values = 0 ? 1

C = ( C * ( 1 - K ) + K );

M = ( M * ( 1 - K ) + K );

Y = ( Y * ( 1 - K ) + K );

//CMY values = 0 ? 1

//RGB values = 0 ? 255

rgb['red'] = ( 1 - C ) * 255;

rgb['green'] = ( 1 - M ) * 255;

rgb['blue'] = ( 1 - Y ) * 255;

return rgb;

Link to comment
  • 10 months later...
  • 3 weeks later...

Or real.

Meanwhile I've learned that the Calculator of ColorSync is not scriptable.

The results of this conversion are disappointing, but obviously the best I can do.

[font:Courier New]PROCEDURE RGBToCMYK;

{ ------------------------------------------------------ }

CONST { testing }

red = 68;

green =76;

blue = 132;

{ ------------------------------------------------------ }

VAR

cmykC, cmykM, cmykY, cmykK : REAL;

cmykCstring, cmykMstring, cmykYstring, cmykKstring : STRING;

BEGIN

cmykC := 1 - ( red / 255 );

cmykM := 1 - ( green / 255 );

cmykY := 1 - ( blue / 255 );

cmykK := 1;

IF ( cmykC < cmykK ) THEN cmykK := cmykC;

IF ( cmykM < cmykK ) THEN cmykK := cmykM;

IF ( cmykY < cmykK ) THEN cmykK := cmykY;

cmykC := ( cmykC - cmykK ) / ( 1 - cmykK );

cmykM := ( cmykM - cmykK ) / ( 1 - cmykK );

cmykY := ( cmykY - cmykK ) / ( 1 - cmykK );

IF cmykK = 1 THEN BEGIN

cmykCstring := '0';

cmykMstring := '0';

cmykYstring := '0';

END

ELSE BEGIN

cmykC := ( cmykC - cmykK ) / ( 1 - cmykK ); IF cmykC < 0 THEN cmykC :=0;

cmykM := ( cmykM - cmykK ) / ( 1 - cmykK ); IF cmykM < 0 THEN cmykM :=0;

cmykY := ( cmykY - cmykK ) / ( 1 - cmykK ); IF cmykY < 0 THEN cmykY :=0;

cmykCstring := NUM2STR(3, cmykC);

cmykMstring := NUM2STR(3, cmykM);

cmykYstring := NUM2STR(3, cmykY);

END;

cmykKstring := NUM2STR(3, cmykK);

MESSAGE(CONCAT(cmykCstring, ' ', cmykMstring, ' ', cmykYstring, ' ', cmykKstring));

END;

RUN(RGBToCMYK);[/font]

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...