Skip to main content
brian_p_dts
Community Expert
Community Expert
October 5, 2022
Answered

Can't get spot color RGB values

  • October 5, 2022
  • 3 replies
  • 539 views

Hi all, I have the following snippet. For the else statement I am getting undefined for all values. The path is a PathItem. Any thoughts on where I'm going astray? Thanks. 

var rgb = " ;
if (path.fillColor.typename !== "SpotColor") {
    if (path.fillColor.typename == "RGBColor") {
        rgb = path.fillColor.red + "" + path.fillColor.green + path.fillColor.blue;
    }
}
else {
    rgb = path.fillColor.spot.spotKind + ":" + path.fillColor.spot.red + "" + path.fillColor.spot.green + 
    path.fillColor.spot.blue;
}
$.writeln(rgb);

 

This topic has been closed for replies.
Correct answer Charu Rajput

@brian_p_dts 

In addition to what @m1b said, also I noticed, you are using wrong property. To fetch the red, you need to use spot.color.red not spot.red

Try following version

 

var rgb = "";
var _path = app.selection[0];
if (_path.fillColor.typename !== "SpotColor") {
    if (_path.fillColor.typename == "RGBColor") {
        rgb = _path.fillColor.red + "" + _path.fillColor.green + _path.fillColor.blue;
    }
}
else {
    rgb = _path.fillColor.spot.spotKind + " : " + _path.fillColor.spot.color.red + "" + _path.fillColor.spot.color.green + 
    _path.fillColor.spot.color.blue;
}
$.writeln(rgb);

 

3 replies

m1b
Community Expert
Community Expert
October 6, 2022

Actually I had another thought. Getting the fillColor from a path item isn't straightforward. For example, if your path item is a CompoundPathItem, then it doesn't have a fillColor property at all (you must get its first pathItems element's fillColor).

- Mark

brian_p_dts
Community Expert
Community Expert
October 6, 2022

Yeah, I was accounting for compound items when getting to the path variable. Charu's solution ended up being correct; I was missing the .color at the end of spot. Spots are so weird! Good tip about path, though it doesn't appear to have caused the issue. 

Sergey Osokin
Inspiring
October 7, 2022

Spot not weird. If you look at the documentation, Spot is an object with a set of properties. "Color" data is just one of the subobjects. There is an even shorter way to get an array of Spot colors using getInternalColor(): var arr = selection[0].fillColor.spot.getInternalColor(). The difference is that you get the color values without rounding.

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
October 6, 2022

@brian_p_dts 

In addition to what @m1b said, also I noticed, you are using wrong property. To fetch the red, you need to use spot.color.red not spot.red

Try following version

 

var rgb = "";
var _path = app.selection[0];
if (_path.fillColor.typename !== "SpotColor") {
    if (_path.fillColor.typename == "RGBColor") {
        rgb = _path.fillColor.red + "" + _path.fillColor.green + _path.fillColor.blue;
    }
}
else {
    rgb = _path.fillColor.spot.spotKind + " : " + _path.fillColor.spot.color.red + "" + _path.fillColor.spot.color.green + 
    _path.fillColor.spot.color.blue;
}
$.writeln(rgb);

 

Best regards
m1b
Community Expert
Community Expert
October 5, 2022

Hi @brian_p_dts, I think it is because Illustrator is notoriously messy with its own global vars, including "path". For example run this script, even in a fresh session, "$.writeln(path)" and you will get a result (the equivalent of "app.path"). Same goes for "selection" which is same as "app.selection". Bad Illustrator!

 

So try changing your var name from path to pathItem, or something. Very annoying but I think that is the problem here. I have been caught by it before.

- Mark