sambecker
sambecker

Reputation: 1139

Convert a CMYK color to RGB using a color profile like ISO V2 Coated?

I know this question has been asked before several different ways but none seem relevant to my problem: I would like to convert a single CMYK color accurately to RGB using a color profile such as ISO Coated V2. I want to do it this way because straightforward mathematical conversions result in bright colors unachievable in the CMYK color space.

The difference between: Real Cyan and RGB Cyan

Ideally this can be achieved in Ruby but I would be happy to see a solution in pseudo code or even JavaScript. I would prefer to avoid a solution that relies on a proprietary/opaque framework.

Any ideas?

Upvotes: 3

Views: 3314

Answers (2)

fmw42
fmw42

Reputation: 53164

I assume the values you show for CMYK are in percent (100/0/0/0). In Imagemagick command line, you can do the following to make a swatch

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

enter image description here

Or you can just get the value back as follows:

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 in the range of 0 to 255 rather than %, then 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(0,156,207)

You can also start with values between 0 and 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(0,156,207)

You can do this via RMagick, but I am not an expert on RMagick. But see the other post from sambecker.

Upvotes: 0

sambecker
sambecker

Reputation: 1139

The following method performs CMYK/RGB color-managed conversion in the Ruby environment 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 foundation for this solution, along with more detail and context, can be found here:

Converting colors (not images) with ImageMagick

Upvotes: 1

Related Questions