Convert CMYK color to RGB using a color profile like ISO V2 Coated?
I know this question has been asked in several different ways, but none of them are relevant to my problem: I would like to accurately convert one color CMYK
to RGB
using a color profile like ISO Coated V2
. I want to do it this way, because direct mathematical transformations result in vibrant colors that are unattainable in the color space CMYK
.
Ideally, this can be achieved in Ruby, but I'd love to see a solution in pseudocode or even JavaScript. I would rather avoid a solution based on a proprietary / opaque framework.
Any ideas?
source to share
The following method performs CMYK/RGB
color-driven transformation in the environment Ruby
via ImageMagick
:
def convert_cmyk_to_rgb_with_profiles(cmyk, profile_1, profile_2)
c = MiniMagick::Tool::Convert.new
c_255 = (cmyk[:c].to_f / 100.0 * 255.0).to_i
m_255 = (cmyk[:m].to_f / 100.0 * 255.0).to_i
y_255 = (cmyk[:y].to_f / 100.0 * 255.0).to_i
k_255 = (cmyk[:k].to_f / 100.0 * 255.0).to_i
c.xc("cmyk(#{c_255}, #{m_255}, #{y_255}, #{k_255})")
c.profile(File.open("lib/assets/profiles/#{profile_1}.icc").path)
c.profile(File.open("lib/assets/profiles/#{profile_2}.icc").path)
c.format("%[pixel:u.p{0,0}]\n", "info:")
result = c.call
srgb_values = /srgb\(([0-9.]+)%,([0-9.]+)%,([0-9.]+)%\)/.match(result)
r = (srgb_values[1].to_f / 100.0 * 255.0).round
g = (srgb_values[2].to_f / 100.0 * 255.0).round
b = (srgb_values[3].to_f / 100.0 * 255.0).round
return { r: r, g: g, b: b }
end
By calling:
convert_cmyk_to_rgb_with_profiles({c:100, m:0, y:0, k:0}, "USWebCoatedSWOP", "sRGB_IEC61966-2-1_black_scaled")
The basis for this decision, as well as more details and context, can be found here:
source to share
I am assuming the values ββyou show for CMYK are percentages (100/0/0/0). In Imagemagick command line you can do the following to make a sample
convert xc:"cmyk(100%,0%,0%,7%)" -profile /Users/fred/images/profiles/ISOcoated_v2_300_eci.icc -profile /Users/fred/images/profiles/sRGB.icc -scale 100x100! test.png
Or you can simply return the value like this:
convert xc:"cmyk(100%,0%,0%,7%)" -profile /Users/fred/images/profiles/ISOcoated_v2_300_eci.icc -profile /Users/fred/images/profiles/sRGB.icc -format "%[pixel:u.p{0,0}]\n" info:
SRGB (0%, 61%, 81%)
If you want values ββbetween 0 and 255, not%, add -depth 8.
convert xc:"cmyk(100%,0%,0%,7%)" -profile /Users/fred/images/profiles/ISOcoated_v2_300_eci.icc -profile /Users/fred/images/profiles/sRGB.icc -depth 8 -format "%[pixel:u.p{0,0}]\n" info:
SRGB (0156207)
You can also start with values ββfrom 0 to 255.
convert xc:"cmyk(255,0,0,17.85)" -profile /Users/fred/images/profiles/ISOcoated_v2_300_eci.icc -profile /Users/fred/images/profiles/sRGB.icc -depth 8 -format "%[pixel:u.p{0,0}]\n" info:
SRGB (0156207)
You can do this through RMagick, but I'm not an expert at RMagick. But see another post from sambecker
.
source to share