20 Replies Latest reply on Jan 14, 2016 1:50 AM by Laubender

# Select & modify grey swatches

Hi all

I'm trying to figure out if there's a way to programatically decide if a color is grey (not greyscale). Can any one help?

I intend to write a script that searches for grey swatches and turns them into greyscale...

thanks

• ###### 1. Re: Select & modify grey swatches

How do you decide if a color is 'gray'? For RGB, 3 same values work - (127,127,127), for example, is clearly gray. But how about (126,126,127)? (125,125,127)? (Etc. - up to where a person may decide it's no longer near gray.)

Lab appears quite easy: both a and b must be '0' (but see above - what if either is 1 or -1?).

For CMYK, is it only considered gray when C=M=Y=0 and K=a percentage?

• ###### 2. Re: Select & modify grey swatches

That's exactly my problem. I part from a CMYK color that (perception says) is grey, say C12 M9 Y10 K0 ( or a darker C49 M39 Y39 K20), and want to turn it into grayscale.

So we can assume that, K being 0, as long as the difference in percentage between C, M and Y is not too large (how much would that be?), we sort of percieve it as grey (I do, at least).

Could that be it? find out if te difference between the three is lower than x?

Actually, I just ralized that, to my eye, colors preceived as grey can be built with the amount of M and Y  being between 10% adn 50% of that of C (aprox).

Guess that could be it, then...

• ###### 3. Re: Select & modify grey swatches

Okay, that's more difficult then. My own method of converting a CMYK color is to convert it first to Lab, and then use only the Lightness property. Mind you, it runs the opposite way from Grayscale - 0 is 'black', and in Grayscale '0' means '0 percent black' (i.e., white). With this the results on RGB differ from what you'd expect - (127,127,127) is about 54% instead of 50% (well, 48.9% to be exact). Usually I fidget the gray color a bit by hand after this. So ultimately it may or may not work for you.

• ###### 4. Re: Select & modify grey swatches

Indeed, color perception is a complicate matter. And it is psychology.

See the following example. Which of the small squares is more grayish? *

The one inside the yellow field or the one with the white background?

* A screenshot from InDesign.

Both small squares are filled with the same color: 10,0,0,0 CMYK.

Also:

Rendering method of the output color profile

(different papers, yellowish newspaper vs. glossy magazines etc.pp.)

And a lot more factors we cannot discuss here.

A bit off-topic, but maybe also spot on:

See into terms like: DeviceLink-Profile, ColorManagement, PDF Workflow

Uwe

• ###### 5. Re: Select & modify grey swatches

Problem is that, taking LAB as a reference, the CMYKs I mentioned earlier give a result of L88 a-1 b0 and L51 a-2 b-1, in which case, where's the limit? I'm still stuck to the problem of having different shades or temperatures of gray...

I've got the same problem when working in CMYK, without the hassle of having to translate the color into lab. My only doubt now, is if I'm right in my calculations

var cC = swatch.colorValue[0];

var cM = swatch.colorValue[1];

var cY = swatch.colorValue[2];

if(cC*0.9>= cM ||cM >= cC*0.5){

if(cC*0.9>= cY||cY >= cC*0.5){

}

}

else{

}

• ###### 6. Re: Select & modify grey swatches

I understand what you say, Laubender

thankfully, we're not discussing as far as the rendering method just yet...

still, it's kind of hard to decide when gray stops being gray, specially with the uncertainty of the monitor calibration.

• ###### 7. Re: Select & modify grey swatches

It's more complex than that.

A color value alone does not define a color. What is missing is the viewing condition. The viewing condition plus the color value is defining the color.

I think, to define a gray, you can only work with handpicked color samples printed for a special printing condition and build a lookup table where you personally decide what goes in the table and what is off. And that's really for one special printing condition inside a narrow set of papers and of a distinct paper class. And that's a huge task where 100th of hours will go into.

After the lookup table is built, a gigantic array of color values for a destinct viewing condition, you can define an algorithm, if a given color value should be regarded as gray or not.

Uwe

• ###### 8. Re: Select & modify grey swatches

Hi Uwe,

Unless the OP is willing to put in a huge investment then the background color can be ignored.

It's 100% true that the background color can change whether the color is perceived as grey or not but most of the time an approximation can be given.

How to work out that approximation is another questions.

The OP can look at Sort color swatches in InDesign

I think that perhaps the best result can be got by converting to HSL which is quite simple to do a mathematical approximation and to then look mainly at the S figure and then at the L figure. The H figure is less important.

A S figure of 5 or less is going to be pretty grey unless one has a very high or low L.

A high S figure is going to mean that it's not going to be grey unless the L is going to be in the high range but not too high, somewhere between 91 and 95, higher than that is going to look whitish and lower with have too much color in it to look grey.

One basically needs to build a relationship between the S value and the L high and low values and then one can get a pretty good result albeit not 100% accurate.

I think it would be pretty simple to implement maybe 1 - 3 hours, which I don't have now.

FWIW

Trevor

• ###### 10. Re: Select & modify grey swatches

What I like to say is, and I know, that I repeat myself:
You cannot tell per se what l*a*b* value a distinct CMYK or RGB value is, because you have to do color management for this.

What we can look into to get the right calculated l*a*b* values (according to the used color management settings of InDesign) is the new function

document.colorTransform()

app.colorTransform()

that is available in InDesign CC 2014.2 (maybe CC 2014.1) and higher.

But this beast is a bit hard to tame.

And the grey balance definition is only a starting point. A thin area of values that can be safely considered as "neutral grey".
We have to define a zone of values that would go as grey around it. And in this regard grey is very "shaky".

If HSL is the right model to determine that, alright, but I really don't know.

Unfortunately there is no HSL color model with InDesign.

How would we calculate HSL from l*a*b* ?

Uwe

• ###### 11. Re: Select & modify grey swatches

Hi Uwe,

Nice reference.

I wasn't up to date on the new colorTrasform method.

Below is a very simplified Lab to RGB method which I think is set up for Adobe 1988 Gamut.

To covert the rgb to hsl see http://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion

I based the below on something somewhere on the net, I use it mainly for UI stuff.

Regarding the color managements one just has to use an approximation or it's going to take ages.

```// rgbToLab simplified
function LabToRGB (Lab /* array */) {
var L, a, b, ref_X, ref_Y, ref_Z, X, Y, Z, YCubed, XCubed, ZCubed, R, G, B, r, g, b;

const k116 = 16 / 116;
const k24 = 1 / 2.4;

L = Lab[0];
a = Lab[1];
b= Lab[2];

ref_X =  96.420;    // Adobe 1988 Gamut icc values
ref_Y = 100.000;
ref_Z = 82.491;

Y = ( L + 16 ) / 116;
X = a / 500 + Y;
Z = Y - b / 200;

YCubed = Y * Y * Y;
XCubed = X * X * X;
ZCubed = Z * Z * Z;

if ( YCubed > 0.008856 ) Y = YCubed else Y = ( Y - k116 ) / 7.787;
if ( XCubed > 0.008856 ) X = XCubed else X = ( X - k116 ) / 7.787;
if ( ZCubed > 0.008856 ) Z = ZCubed else Z = ( Z - k116 ) / 7.787;

X = ref_X * X / 100;
Y = ref_Y * Y / 100;
Z = ref_Z * Z / 100;

R = X *  3.2406 + Y * -1.5372 + Z * -0.4986;
G = X * -0.9689 + Y *  1.8758 + Z *  0.0415;
B = X *  0.0557 + Y * -0.2040 + Z *  1.0570;

if ( R > 0.0031308 ) R = 1.055 * Math.pow(R , k24 ) - 0.055 else R = 12.92 * R;
if ( G > 0.0031308 ) G = 1.055 * Math.pow(G , k24 ) - 0.055 else G = 12.92 * G;
if ( B > 0.0031308 ) B = 1.055 * Math.pow(B , k24 ) - 0.055 else B = 12.92 * B;

var outOfRange = (R < 0 || G < 0 || B < 0 || R > 1 || G > 1 || B > 1);

if (outOfRange) {
if (R < 0) R = 0 else if (R > 1) R = 1;
if (G < 0) G = 0 else if (G > 1) G = 1;
if (B < 0) B = 0 else if (B > 1) B = 1;
}
r = R * 255;
g = G * 255;
b = B * 255;

return [r,g,b];
}
```
• ###### 12. Re: Select & modify grey swatches

Hi Trevor,

thank you for sharing this.

In the meantime I looked it up:

colorTransform()

was introduced with InDesign CC 2014.1

The .0 version did not have this method, so Gregor Fellenz's excellent ExtendScript DOM documentation could not refer to it:

InDesign ExtendScript API (10.0)

You can read the little what Adobe has documented in the ESTK's Object Model Viewer or here:

InDesign ExtendScript API (11.0)

So basically it should be "quite easy" to convert from CMYK to RGB (or CMYK to L*a*b* or RGB to L*a*b*) directly using color management.

And that is for every CMYK and RGB color space and management method you can define with InDesign's Color Settings.

Using colorTransform() is not straightforward as it could perhaps be (or I imagined it could be) after reading the sparse documentation the first time.

And users with InDesign CC 2014.0 and below cannot benefit of that.

Uwe

• ###### 13. Re: Select & modify grey swatches

I intend to write a script that searches for grey swatches and turns them into greyscale...

Back to square one.

InDesign cannot manage grayscale color profiles.

But it can export "somehow" PDF with a color conversion to grayscale.

(InDesign CS6 and above)

What to do now with a found color, that is supposed to be "gray"?

(I leave the right method for identifying this open for now)

Turn it to a hue of color black. That's the only option we have right now.

And this color is now considered as a percentage of black of the current CMYK working space, if all goes well at output.

Where can we pick up a grayscale color value then?
In an exported PDF perhaps. Provided that we used the right grayscale color profile in the PDF export settings.

What will that be?

In my opinion it could be a Black Channel color profile (corresponding to the CMYK working profile used) that we could generate using PhotoShop.

I assume here, that the production purpose of the whole InDesign document is reproducing it with CMYK and exporting to CMYK.

But all this is leading me to the conviction, that our OP wants to stabilize the output of "gray" colors in a CMYK printing process in a very early stage.

I'd say, leave that for the PDF workflow, that comes after exporting to PDF. There are a lot of mechanisms using dedicated Device Link Profiles in PDF workflows to do that at the offset printers.

Uwe

• ###### 15. Re: Select & modify grey swatches

That bit, I've got somewhat solved with this script from in-tools Convert colors to grayscale script in InDesign | in-tools.com .

It's the definition of an acceptable range for gray that I'm lacking...

I wonder if they've got some idea of it... http://www.thisiscolossal.com/2016/01/harvard-pigment-library/  or http://www.thisiscolossal.com/2015/11/japanese-pigment-store/

• ###### 16. Re: Select & modify grey swatches

Hi Trevor,

yes, I know this script by Harbs. But its result will not reflect any color management.

I know, I'm a little nit-picking here, but we should aim for precise results if possible.

Uwe

• ###### 17. Re: Select & modify grey swatches

Hi Jlaurrieta,

can you tell me a bit more about your task or the script you want to write?
Who will use this script?
Couldn't the user decide what is actually "gray" and what is not?
Or is this all for an InDesign Server solution or a batch process?

And: Am I right with my assumption, that this should work for stabilizing a CMYK printing process?

Uwe

• ###### 18. Re: Select & modify grey swatches

Well, as far as I've tested, any color mode change will be unprecise in a scripting point of view, unless of course, it's color managed and linked to a DB of what each color should be in an other mode... I guess...

But then.... is normal eye really capable of seeing deviation the script has? as far as I've tested in my gray to grayscale effort, it was a pretty good aproximation. Admitedly, it's my judgement what I use as reference...

• ###### 19. Re: Select & modify grey swatches

Hi Laubender

I work in the marketing departmen of a medium sized company. We've recently started to do most of our commercial flyers on out own and found out that, the company that's been doing the design work for us untill now, has been supplying them with, what we consider are the wrong colors (colors that should have been corporate aren't at all, gray colors in full CMYK rader than grayscale, etc). So, in order to have most, if not all, of our documments with the same color scheme, I need to go through all of them, one by one, and change the color values manually... or find a script that simplifies the task....

Besides, I'm intriged, despite of my ingnorance concerning color management...

• ###### 20. Re: Select & modify grey swatches

Another note:

Well, we did not speak of the density and the visual appearance of very dark "gray" colors when printed on paper.

These cannot be reproduced by using a simple hue of black.

Black alone is not able to reflect the density and visual approach of a dark color.

Example:

CMYK

60,50,50,70

with an ISO Coated v2 300% profile

To use simply ≈ 90 % black as a substitute would maybe look adequate on an uncalibrated screen, but would be a "small disaster" when printed side by side with the original CMYK color. So this is also a design decision. What is the visual impact of 60,50,50,70 CMYK vs. 90 Black on printed paper. And I can only tell it is a big one.

While I'm writing that post, you have answered and gave more information. Thank you.

Hm I think, that would make the task more clear and maybe easier as well.

If you have your company rules what color is used for what, you could perhaps single out the wrong usage with inspecting the objects, that use these colors. Paragraph styles perhaps, object styles or inspecting the objects and texts directly.

Uwe