Skip to main content
Inspiring
January 30, 2020
Question

Get the hex color value of an object

  • January 30, 2020
  • 2 replies
  • 2611 views

I'm trying to find a way to get the hex color value of a selected object via script to pass through to my CEP panel. I know the hex value is there and ready (I can see it in the color picker), but I don't know the scripting method to access it. I've tried converting it via third party libraries but the result is always inaccurate compared to the hex value shown in the color picker. Any ideas on how to grab that value? 

This topic has been closed for replies.

2 replies

Ten A
Community Expert
Community Expert
January 30, 2020

Color conversion on Illustrator depends on the color profile setting. The ideal is to let Illustrator convert the colors and use that value.
Here is a POC Code.

var itm = app.selection[0].duplicate();
app.executeMenuCommand("deselectall");
itm.pathPoints[0].selected = PathPointSelection.ANCHORPOINT
app.executeMenuCommand("Colors9");

//get color value

itm.remove()

//send value
Inspiring
February 6, 2020

This looks promising, but I cannot find any documentation on the app.executeMenuCommand() method. Is there anywhere I can go to get a better idea of all the arguments possible for that method?

Inventsable
Legend
February 7, 2020

Hello, it's not officially documented anywhere except for a few forum posts like this one

Ten A
Community Expert
Community Expert
January 30, 2020

You can to get objects color in DOM side ExtendScript and return to Panel JavaScript.

//ExtendScript - get pathItems colour value.
alert(getColorHex())
function getColorHex(){
var tg = app.selection[0];
return "#"+tg.fillColor.red.toString(16)
+tg.fillColor.green.toString(16)
+tg.fillColor.blue.toString(16);
}

//CEP sideJavaScript
var csi = new CSInterface();
csi.evalScript("getColorHex()",function(ev){alert(ev)})

 

Inspiring
January 30, 2020

I should have specified in the original post, but the hex value has to be derived from any kind of solid color that's applied to the object, regardless of whether it's RGB, CMYK, a spot color, HSL, lab, etc. Your method seems like it would only work for RGB colors.

 

I've looked through the documentation, but I didn't find a way to leverage Illustrator's already built in color conversion.