Re: RGB to CMYK
Re: RGB to CMYK
- Subject: Re: RGB to CMYK
- From: "Mark J. Reed" <email@hidden>
- Date: Sun, 22 May 2011 19:28:00 -0400
Well, it helps that I was just looking at this stuff to try and do a paint-mixing program using the traditional Red/Yellow/Blue paint scheme...
Oh, and since I didn't give an example of calling my handler:
cmyk from {228,255,91}
or
set rgbColor to {228, 255, 291}
set cmykColor to cmyk from rgbColor
On Sun, May 22, 2011 at 6:15 PM, Jim Thorton
<email@hidden> wrote:
Hello,
Whoa... Thank you very much, Mr. Reed. You are quite knowledgeable about color.
On May 23, 2011, at 7:01 AM, Mark J. Reed wrote:
OK, there are different things going on. In principle, CMY (without the K) is just the complement of RGB (those orderings aren't completely arbitrary; cyan is the complement of red, magenta is the complement of green, and yellow is the complement of blue). So with each component in the range 0 to 1, CMY=(1-R,1-G,1-B). In real printing, they add black ink (the K) because trying to get good blacks out of a combination of cyan, magenta, and yellow usually results in a dark muddy brown color and torn paper. So however much of the color is equal amounts of C, M, and Y gets turned into black instead.
One issue is that RGB values are often given in byte scale (each component in the range 0-255), while CMYK values are usually given as percentages (each value in the range 0-100), so you have to scale.
Finally, the set of colors reproducible on the screen (where you use RGB) is different from the set of colors reproducible on paper (where you use CMYK). Furthermore, different screens and printers have different color gamuts and different cyan/magenta/yellow/red/green/blue/black/white values. These differences are captured in "color profiles" in image software. If you don't care about precision color matching, you can ignore those for now. However, it's possible the color picker is not ignoring that stuff, and that might explain some of the the discrepancies you're seeing.
Back to the math. Ignoring the color profile stuff, it's very easy to convert from RGB to CMYK. First, convert to CMY by just subtracting each component value from the maximum: C = max - R, M = max - G, Y = max - B.
Now you have the color in CMY form. To convert to CMYK, just take the smallest value of each of CMY and use that as K, subtracting it from the other three values. In code form, assuming you feed in values from 0-255 for RGB and want values from 0-100 for CMY:
to cmyk from rgb
set cmyk to {}
set min to 255
repeat with i from 1 to 3
set comp to round ((255 - (item i of rgb)) / 255 * 100)
set end of cmyk to comp
if comp < min then set min to comp
end repeat
set end of cmyk to min
repeat with i from 1 to 3
set item i of cmyk to (item i of cmyk) - min
end repeat
cmyk
end cmyk
So given your example:
> if I have an RGB color of {228,255,91}
> the color picker says its CMYK representation is {12,0,53,0}
Not sure where the difference is; again, it might be taking color profiles into account?
On Sun, May 22, 2011 at 3:37 PM, Jim Thorton
<email@hidden> wrote:
Dear AppleScripter,
Hello. I can honestly say that I know little about the color. I do know what the color depth means. Anyway, when you pick a color through the Color Picker window, you will get different code representations like RGB, HSV, CMYK... If t I have a color in RGB, I wonder how I can translate it into CMYK? For example, if I have an RGB color of {228,255,91}, the color picker says its CMYK representation is {12,0,53,0}. Is it possible for us to somehow get this translation easily with AppleScript?
In the meantime, I've found the following reverse translation code on multiple websites.
on cmykrgb(c, m, y, k)
set r to 255 - (round (2.55 * (c + k)))
set g to 255 - (round (2.55 * (m + k)))
set b to 255 - (round (2.55 * (y + k)))
if (r < 0) then set r to 0
if (g < 0) then set g to 0
if (b < 0) then set b to 0
return {r, g, b}
end cmykrgb
If I select the following values, the AppleScript code above gives me {224,225,120}, which is not consistent with what the Color Picker window would give me. So the reverse translation code simply doesn't work?
set c to 0
set m to 79
set y to 41
set k to 0
Thank you for your advice,
Jim
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden
--
Mark J. Reed <
email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden
--
Mark J. Reed <
email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden