Skip to main content
Inspiring
December 16, 2014
Answered

Exporting correct Hex colors via scripting

  • December 16, 2014
  • 1 reply
  • 4125 views

Hello all,

I need to export the fillColor of a paragraph style via scripting.

I can get the fillColor property of the paragraph style, and can also convert CMYK to Hex. However, I do realize that the color values shown in InDesign are in whatever Color Space the document is using.

My question is, is there a native API to convert the color values to a web safe color value? If not, how do I go about mapping the colors?

Additionally, I see that Adobe internally knows how to convert it to a proper hex color (screenshot attached - note the color property in the Export Tagging pane). If I cannot use some native API, is there a way to access the text in that pane?

Thanks all, in advance!

This topic has been closed for replies.
Correct answer DerKäse

Oh no! I'm so sorry! I thought I had removed that part in the question, and made it "True Color", but I guess that's why I shouldn't write posts in a hurry! Long chain of edits that led to web safe colors. I also cannot seem to find an edit button to fix it!

Thanks for pointing that out btw!

Anyways, thanks a bunch for your prompt responses! You're right - I spent a couple hours yesterday going through almost the entire APIs and didn't see anything that caught my eye. Thought I might have missed out on something. BUT!!!! Progress - I *just* found out that if I account for the tint value, and then edit a current CMYK color to RGB color through the "Swatch Options" window in InDesign, I can get the exact RGB color that I'm looking for. That being said, I'm looking to see whether I can do this:

  1. Programmatically create a new Color.
    1. If I'm converting from a Tint instance (CMYK with Tint value), give the newly created color instance the updated (tint compensated) color values in CMYK.
    2. Or, if it's not a tint, then simply give it the CMYK value.
  2. Set it's ColorSpace to RGB - I'm hoping this will trigger whatever internal fairy dust is being used.
  3. Read the new color values
  4. Celebrate victory.

I cannot seem to find a method to "create" a "new instance of Color", so I'm going to use the "duplicate" method of an existing color, and update it's color values, and use it as a scratch instance for converting all my colors.

If this works, I'll certainly update the post so that the community may make use of it!


Win! It worked!!!

Here's the complete solution -

Assuming the original color is stored in a variable named color:

var color = passedInColor; //the original color that we wish to convert.

// Create a temporary color instance that we'll use to extract updated colorValues.

var scratchColor = workingDoc.colors.add({

                         model: color.model,

                         space: color.space,

                         colorValue: color.colorValue

                    });

// Now, we force adobe's internal color conversion mechanism to trigger by changing the scratchColor's color space.

scratchColor.space = ColorSpace.RGB;

var updatedValues = scratchColor.colorValue; // Updated values now has the properly mapped and converted RGB values.

// You may need to round off the R, G and B values in the updatedValues array.

// And that's it!

Why am I not surprised that it had to be a hacky work around?! *sigh*. Working on this platform has been so so so frustrating! I do hope adobe gets its act together!

1 reply

Jongware
Community Expert
Community Expert
December 16, 2014

You need to (1) convert the CMYK color space to RGB, then (2) find the closest 'web safe' value.

Converting CMYK to RGB is just a matter of changing the color space, but you will find it's as accurate as all color space conversions (i.e., "not very"). However, there is no reasonable alternative.

Changing the RGB values from 0..255 to the 216 color "web safe" palette is a matter of maths: there are 6 levels of R,G,B each.  Divide by 51, round, multiply by 51 again and you have right value. Note that your example shows #aaaad0, which is not one of the web safe colors. The nearest equivalent would be #9999cc.

DerKäseAuthor
Inspiring
December 16, 2014

Thanks for your reply!

I'm actually not looking to transform it to a web safe palette. I'm looking for True Color (24 bit, 256 values for each R, G and B). I'm simply using chrome for testing the color; and making sure it looks the way it does in InDesign.

The CMYK that I had was 33, 30, 3 and 0, which translates to an RGB of 171, 179, 247, which then translates to a hex value of #abb3f7.

However, what I'm looking for is how do I map that CMYK color space (U.S. Web Coated SWOP V2), to an RGB, such that I get an output of 170, 170, 208 (aaaad0). If Adobe's doing it internally, so precisely, there's got to be a way!

Jongware
Community Expert
Community Expert
December 16, 2014

Ah okay; you mentioned "web safe color" and I wondered why, as that is So 1995.

It seems that internal conversion is not exposed to scripting. Off the record: using my trusty old CS4, I get different values for 33,30,3,0: 170,169,206, which is #AAA9CE in hex. So, very close to what you are seeing in the Export Tags dialog. I did not see any differences when assigning different color profiles, or possibly I did not do that the right way.