Petri Posted November 8, 2006 Share Posted November 8, 2006 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. Quote Link to comment
islandmon Posted November 8, 2006 Share Posted November 8, 2006 Did you know that ColorSync has a neat calculator and ColorSync is scriptable: Quote Link to comment
Petri Posted November 9, 2006 Author Share Posted November 9, 2006 Yes. No. AppleScripting is not my fort? and especially not of programs. Quote Link to comment
islandmon Posted November 9, 2006 Share Posted November 9, 2006 The trick is to get at the code source of the Calculator ; ) http://www.apple.com/downloads/dashboard/developer/colourmod.html Try this: http://www.apple.com/downloads/macosx/automator/equilibriumdebabelizerautomatoractionspack1.html Quote Link to comment
islandmon Posted November 9, 2006 Share Posted November 9, 2006 Here's the code calcs you're seeking: 1) Download > http://www.apple.com/downloads/dashboard/developer/colourmod.html 2) Widget installs into >/Users/~/Library/Widgets/ColourMod.wdgt 3) Select Widget and >show >Contents 4) Use texteditor , Xcode or BBedit to look into the file "ColourModScript.js" at the end of the script are all the conversion calculations. Quote Link to comment
Petri Posted November 9, 2006 Author Share Posted November 9, 2006 Very funny... Too involved, too sophisticated. I guess just have to figure out what the input values are (% I believe) and especially what the output values mean. When I interpreted the formulae in Excel, I got funny values. Quote Link to comment
islandmon Posted November 9, 2006 Share Posted November 9, 2006 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; Quote Link to comment
Petri Posted November 9, 2006 Author Share Posted November 9, 2006 Thanks. The functions look suspiciously similar to those I've tried to use with erroneous results, but we'll see what VS thinks about them. Quote Link to comment
Petri Posted October 2, 2007 Author Share Posted October 2, 2007 Islandmon, This seems to become handy again. Now I obviously need to convert RGB to CMYK in bulk! At least after NNA sorts out the CMYK-oddities on the new colour palettes. Thanks once more! Quote Link to comment
Petri Posted October 22, 2007 Author Share Posted October 22, 2007 Well, obviously this was not supposed to be easy. What might if (!cmyk['C']) cmyk['C'] = 0; if (!cmyk['M']) cmyk['M'] = 0; if (!cmyk['Y']) cmyk['Y'] = 0; if (!cmyk['K']) cmyk['K'] = 0; mean? Quote Link to comment
islandmon Posted October 22, 2007 Share Posted October 22, 2007 if (!cmyk['C'])=If the product of > cmyk['C'] = ( cmyk['C'] - cmyk['K'] ) / ( 1 - cmyk['K'] ); =0 then cmyk['C'] = 0 Quote Link to comment
Petri Posted October 22, 2007 Author Share Posted October 22, 2007 Islandmon, you are as lucid as ever! Do you by any chance mean cmykC := ( cmykC - cmykK ) / ( 1 - cmykK ); IF cmykC < 0 THEN cmykC :=0; You do speak VectorScript, don't you? Quote Link to comment
islandmon Posted October 22, 2007 Share Posted October 22, 2007 VAR cmykC : INTEGER; Quote Link to comment
Petri Posted October 22, 2007 Author Share Posted October 22, 2007 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] Quote Link to comment
Recommended Posts
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.