0

More specifically, I'd like to accurately convert a CMYK value (probably from the ISO Coated v2 space) to an RGB value (probably from the sRGB space) on the Ruby platform (probably using ICC profiles).

ImageMagick seemed like a good place to start, but I've also heard that LittleCMS might have been ported/wrapped to work with Ruby.

Once again, I'm looking to convert single colors, NOT image files. Any ideas?

fmw42
  • 46,825
  • 10
  • 62
  • 80
sambecker
  • 1,099
  • 12
  • 28
  • Have you seen https://github.com/halostatue/color ? – msanford Oct 30 '17 at 19:08
  • @msanford I think I did but couldn't find any usage examples. Do you know of any tutorials/APIs for it? – sambecker Oct 30 '17 at 19:11
  • No, but you could check out the tests to see how it's implemented https://github.com/halostatue/color/blob/master/test/test_cmyk.rb – msanford Oct 30 '17 at 19:15
  • Ok, just dug deeper. It seems like you can't convert CMYK using any type of managed color space. The only function available is `@cmyk.to_rgb` which doesn't accept any parameters. – sambecker Oct 30 '17 at 19:39

2 Answers2

1

In ImageMagick, you can do the following:

convert xc:"cmyk(0,255,255,0)" -colorspace sRGB -format "%[pixel:u.p{0,0}]\n" info:
red

convert xc:"cmyk(0,255,255,0)" -profile /Users/fred/images/profiles/USWebCoatedSWOP.icc -profile /Users/fred/images/profiles/sRGB.icc -format "%[pixel:u.p{0,0}]\n" info:
srgb(93%,11%,14%)
fmw42
  • 46,825
  • 10
  • 62
  • 80
  • This looks very promising but I keep getting the following error in Terminal: `convert: 'sRGB' @ error/convert.c/ConvertImageCommand/3251`. The docs (https://www.imagemagick.org/script/convert.php) suggest that `convert` is for image translation, not color translation. Any ideas? – sambecker Oct 30 '17 at 20:27
  • What is your ImageMagick version and platform? There can be syntax differences. If on very old versions of IM try replacing -colorspace sRGB with RGB. Which command gave you that error. Be sure in the second command to put your own path to your profiles rather than my path. This command is converting the color to a 1 pixel image and then converting the colorspace and reading out the pixel color value, without saving the created 1 pixel image – fmw42 Oct 30 '17 at 21:49
  • I’m on macOS Sierra 10.12 using the current version of ImageMagick which I installed through Homebrew with —little-cms although lcms doesn’t show up as a delegate for some reason ... – sambecker Oct 30 '17 at 21:53
  • What version of ImageMagick. Try `convert -version`. If that does not work, then there is something wrong with your install. Perhaps you need to install lcms2 with your Homebrew of ImageMagick. My install of ImageMagick on Mac OSX Sierra (via MacPorts for delegates and source for IM, shows lcms (for lcms2). Also if you are on IM 7, then replace convert with magick. Note: that there is a color converter at http://www.imagemagick.org/contrib/color-converter.php, but it does not use profiles. – fmw42 Oct 30 '17 at 21:55
  • Ok, finally got lcms working with this command `brew reinstall imagemagick --with-little-cms --with-little-cms2`. Thanks for the help there. Now, I can get the following to work `magick convert xc:"cmyk(100,0,0,0)" -profile USWebCoatedSWOP.icc -profile sRGB_IEC61966-2-1_black_scaled.icc -format "%[pixel:u.p{0,0}]\n" info:` with this result `srgb(56.4309%,84.8676%,97.2412%)`! – sambecker Oct 30 '17 at 22:23
  • Any idea how to embed this in a Rails app? https://stackoverflow.com/questions/47026354/translate-imagemagick-cli-into-minimagick-gem – sambecker Oct 31 '17 at 01:33
  • 1
    `@sambecker` Sorry, I do not know Rails/Ruby. But at worst, you might be able to make a call to command line ImageMagick with some kind of subprocess call (similar to PHP exec or a subprocess call from Python to ImageMagick). Also see RMagick at http://rmagick.rubyforge.org – fmw42 Oct 31 '17 at 01:55
  • Is there anything you can tweak in format to ensure more significant digits in the srgb(X%,X%,X%) response? When I run this command on the server I get integers (as in your example) but when I run it locally I get four decimal places per channel (as shown in the comment above). – sambecker Oct 31 '17 at 12:40
1

Is there anything you can tweak in format to ensure more significant digits in the srgb(X%,X%,X%)

Likely due to different IM versions. IM 7.0.7.8 shows srgb(93.0648%,11.1254%,14.1741%). IM 6.9.9.20 shows integers. I tried adding -precision 4 to IM 6 command line, but still get integers. To get more precision, one has to parse the txt: output format.

For example without parsing:

convert xc:"cmyk(0,255,255,0)" -profile /Users/fred/images/profiles/USWebCoatedSWOP.icc -profile /Users/fred/images/profiles/sRGB.icc txt:
# ImageMagick pixel enumeration: 1,1,65535,srgb
0,0: (60990,7291,9289)  #EE3E1C7B2449  srgb(93%,11%,14%)

So you need to parse the 16-bit values (for IM Q16) in parenthesis, namely, (60990,7291,9289)

vals=`convert xc:"cmyk(0,255,255,0)" \
-profile /Users/fred/images/profiles/USWebCoatedSWOP.icc \
-profile /Users/fred/images/profiles/sRGB.icc txt: |\
tail -n +2 | sed -n 's/^.*[(]\(.*\)[)][ ]*\#.*$/\1/p'`
red=`echo $vals | cut -d, -f1`
green=`echo $vals | cut -d, -f2`
blue=`echo $vals | cut -d, -f3`
red=`convert -precision 4 xc: -format "%[fx:100*$red/quantumrange]" info:`
green=`convert -precision 4 xc: -format "%[fx:100*$green/quantumrange]" info:`
blue=`convert -precision 4 xc: -format "%[fx:100*$blue/quantumrange]" info:`
color="srgb($red%,$green%,$blue%)"
echo "$color"
srgb(93.06%,11.13%,14.17%)

Adjust -precision, for the number of significant digits you want.

NOTE: In IM 7, -precision does work.

magick xc:"cmyk(0,255,255,0)" -profile /Users/fred/images/profiles/USWebCoatedSWOP.icc -profile /Users/fred/images/profiles/sRGB.icc -format "%[pixel:u.p{0,0}]\n" info:
srgb(93.0648%,11.1254%,14.1741%)

magick -precision 4 xc:"cmyk(0,255,255,0)" -profile /Users/fred/images/profiles/USWebCoatedSWOP.icc -profile /Users/fred/images/profiles/sRGB.icc -format "%[pixel:u.p{0,0}]\n" info:
srgb(93.06%,11.13%,14.17%)

magick -precision 2 xc:"cmyk(0,255,255,0)" -profile /Users/fred/images/profiles/USWebCoatedSWOP.icc -profile /Users/fred/images/profiles/sRGB.icc -format "%[pixel:u.p{0,0}]\n" info:
srgb(93%,11%,14%)
fmw42
  • 46,825
  • 10
  • 62
  • 80