Convert Colors to Grayscale in InDesign

There was recently a discussion on the InDesign forums where John Hawkinson was trying to find a way to get grayscale values using the eyedropper tool.

Feel free to read the discussion there, but to make a long story short, there’s no way to pick up accurate colors from placed graphics. Even if the eyedropper tool was capable of picking up the colors, there’s still issues relating to color profiles. While a perfect solution is not easily achievable, a good-enough for most uses proved to be relatively easy to script.

Here we offer a script to do just that with the following caveats:

  1. I made a decent effort to match the black density of the colors so there should not be a noticeable shift in the darkness, but it’s not 100% perfect.
  2. The script blindly assumes that all three base colors of RGB have the same darkness (which they don’t). So, the results from non-grayscale balanced colors will be even less accurate. [EDIT — I’ve incorporated the formula explained here for much better results. See the comments below for a very interesting Applescript by Rob Day which takes gray profiles into account as well.]

That said, the results are better than what you’ll see in Illustrator using the Grayscale option in the color panel there.

You can download the script here.

Installation

The download includes two files. It has an Extension Manager installer which installs the script automatically, and it contains the bare script file if you’d rather do a manual install.

Extension Manager Install

To have Extension Manager install automatically in the correct location, double-click ConvertToGrayscale.mxp. This should open the latest version of Extension Manager and install the script into the latest version of InDesign.

  • If you are on Windows, you might need to launch Extension Manager manually as an administrator.
  • If you would like to install in other versions of InDesign, manually open Extension Manager corresponding to the correct version and install, or drag the installer onto the application to open it using that version.

Manual Install

If you prefer manual installations, just place the script inside a folder within your scripts folder named “startup scripts”. For example: Adobe InDesign CS5/Scripts/In-Tools/startup scripts/ConvertSwatchToGrayscale.jsx

Use

Using the script is very simple.

  1. Open your Color Panel (Window > Color > Color)
  2. Make sure the color you’d like to convert is active. (The script is sensitive to whether the fill or stroke is active.)
  3. Select Convert to Grayscale in the flyout menu.

Convert Grayscale Menu

That’s it!

After

Geeky Details

If you are only interested in using scripts, you can stop reading here. But, if you are interested in learning more about how this script works, read on…

There’s a couple of parts of the script. One part is installing the menu item:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#targetengine "In-Tools"
 
(function(){
 
var actionName = "Convert to Grayscale";
var action = app.scriptMenuActions.item(actionName);
if(action == null) {
 var action = app.scriptMenuActions.add(actionName);
}
action.addEventListener("onInvoke", makeSwatchGreyscale);    
 
var cPickerMenu = app.menus.item("$ID/ColorPickerPanelPopup");
var prevMenu = cPickerMenu.menuElements.item("$ID/RGB");
 
cPickerMenu.menuItems.add(action,LocationOptions.AFTER,prevMenu);
cPickerMenu.menuSeparators.add(LocationOptions.AFTER,prevMenu);
 
})();
  • Line #1 specifies a target engine, because menu items need to be run in a persistent engine to work.
  • Lines #3 and #18 wrap the code that creates the menus in an anonymous function to ensure any local variable names do not pollute the public namespace.
  • Lines #5 through #9 create a menu action if one does not already exist. Menu actions from the previous session of InDesign persist after InDesign is closed to preserve keyboard shortcuts and the like. Therefore you need to check if the menu action already exists to make sure you don’t create duplicates.
  • In line 10 we attach an event listener which runs our function to do the color conversion when the menu action is run.
  • Lines #12 and #13 get references to the menu we need to create the submenu in, as well as the previous menu to help us position the menu where we would like.
  • Line #15 adds the menu item, and #16 adds a separator above the menu item.

Now for the actual code that does the conversion:

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function makeSwatchGreyscale(){
  var settings = app.strokeFillProxySettings;
  switch(settings.active){
    case StrokeFillProxyOptions.STROKE:
      var color = settings.strokeColor;
      break;
    case StrokeFillProxyOptions.FILL:
      var color = settings.fillColor;
      break;
  }
  color.space = ColorSpace.RGB
 
  var r = color.colorValue[0];
  var g = color.colorValue[1];
  var b = color.colorValue[2];
 
  var brightness = Math.sqrt((r*r*.241) + (g*g*.691) + (b*b*.068));
  var calcValue = 100 - (brightness/255 * 100);
  calcValue+=(calcValue/5.5);
 
  if(calcValue > 100){calcValue = 100;}
  if(calcValue < 0){calcValue = 0;}
 
  color.space = ColorSpace.CMYK;
  color.colorValue = [0,0,0,calcValue];
}
  • Line #20 gets a reference to the current color proxy settings.
  • Line #21-28 checks whether the fill or stroke is active and grabs the appropriate color.
  • Line #29 converts the color to RGB to make it easy for us to do our calculations.
  • Lines #31 through 33 get references to the r,g, and b values.
  • Line #35 uses an equation to get the brightness of the RGB color. The source of the equation appears to be here.
  • Line #36 takes the subtractive average (255 based) RGB value and converts it to an additive 100 based K value.
  • In line #37 we adjust that number a bit to take into account that full RGB black is much darker than just K.
  • Lines #39 and 40 make sure that the value is not higher than 100 or lower than 0 (due to rounding errors).
  • Line #42 converts the color to CMYK and #43 sets the values.

That’s it!

Tags: , , , ,

20 Comments

  1. Jongware says:

    Would it be useful to use a slightly more accurate conversion of “any RGB color” to gray space? The generic formula is

    g = 0.39R + 0.5G + .11B

    and this can be plugged in right away in the totalValue calculation.

  2. rob day says:

    There’s another approach If all you are trying to do is convert a color to the CMYK black channel (ID grayscale).

    If you use a constant (5.5?) to imitate a conversion then you have to assume one source and destination profile (Adobe RGB>Dot Gain 20%?). It is possible to color manage the conversion via custom CMYK profiles instead of using a constant.

    In Photoshop you can use the legacy separation setup to make maximum black profiles, which would convert neutral Lab or RGB colors directly to the K channel on a CMYK conversion.

    This AppleScript lets you select one of 4 profiles, which imitate 15%, 20%, 25%, and 30% grayscale dot gains respectively—the ZIP file includes the profiles, which need to be installed before running the script.

    http://www.zenodesign.com/forum/ConvertToGS.zip

    All the colors I tested matched the equivalent conversion from Lab or RGB to Grayscale in Photoshop

    • Harbs says:

      Hi Rob,

      Nice script!

      I don’t think Lab always works. To demonstrate that point, create a color using the Lab color space with L set to 100 (or close to it) and switch to RGB. Any of those colors will be converted to white using the L value, and many of them are far from white… 😉

      • rob day says:

        I think that’s a gamut issue. Photoshop does the same with bright out of gamut Lab colors. Try converting 100|-20|-20 to Dot Gain 20% grayscale in Photoshop—I get 0% there also.

        • Harbs says:

          Nope. Try 255,255,105. That’s a nice yellow in gamut and you get a L value of 98. Even if most of them are out of gamut, there’s no reason to not convert those values to gray. So, do we agree that we should look for other methods? 😉

          • rob day says:

            Both my script and Photoshop are returning 3% for an AdobeRGB 255|255|105 to Dot Gain 20% conversion

          • rob day says:

            I’ve never paid much attention to Photoshop Lab to Grayscale conversions. Try filling a Lab image with Lab 100|100|50 and converting to grayscale—I get 0!

            Now covert the same image from Lab to AdobeRGB and then to grayscale 20%—I get a more expected 31%

            This version of my script converts Lab color to RGB first and returns a percentage for Lab colors where L is 100:

            http://www.zenodesign.com/forum/ConvertToGS2.zip

  3. Harbs says:

    Here’s a really interesting web page which compares the results of color to gray conversions using a number of different methods:
    http://dcgi.felk.cvut.cz/home/cadikm/color_to_gray_evaluation/

    No matter how you cut it, getting accurate conversions across the spectrum is not simple… 😉

  4. Harbs says:

    Both my script and Photoshop are returning 3% for an AdobeRGB 255|255|105 to Dot Gain 20% conversion

    I actually just exported to gray postscript from InDesign, and the results of 255,255,105 is much closer to your results (i.e. 2 to 3 % instead of my 5% or so). I guess my example is not the best. But, visually, the yellow is much “stronger” than that really weak gray, so I don’t know what to say.

    Either way, the out-of-gamut colors do not convert well when using the L value of Lab.

    • rob day says:

      There are two separate issues here—the color managed conversion from RGB to Grayscale via source and destination profiles, and the visual separation of different hues with similar luminosity. Photoshop deals with the luminosity issue via the Black & White adjustment layer (because there isn’t one conversion that would produce visually pleasing results for all images) and color manages grayscale conversions via luminosity.

      When you export postscript there isn’t an option to choose a grayscale destination—I’m pretty sure it’s the same as converting to 20% Dot gain in PS.

  5. Harbs says:

    I just updated the script to use the equation discussed earlier and updated the post as well.

  6. […] a great free script from Harbs and In-Tools that lets you Convert Colors to Grayscale in InDesign. Check out the comments at the end to see a script by Rob Day that converts the whole […]

    • rob day says:

      Since InDesignSecrets has picked this up I added a readme file to the zip archive. Also note that I haven’t tested any large complex documents yet.

Leave feedback