Read the color of a Colorized Grayscale Image
I've been working on a script to compile all of the colors present in a file and I'd been hung up on Colorized Grayscale images. I tried digging into the 'Colorants' property but couldn't really get any info out of it. Yesterday I was able to figure out a hack to get to the color info. If you select the image, then create a new path, it'll keep the same fill and stroke colors. Here's a bit of code to select the image, create a temporary PathItem, then read the colors, and then blast it, all while preserving any previous selections.
#target illustrator
function AlertFillColorName(){
if( !app.documents.length ) return;
var doc = app.activeDocument;
if( !doc.rasterItems.length ) return
var myImg = doc.rasterItems[0];
if( !myImg.colorizedGrayscale ) {
alert( "top-most image is not a colorized grayscale" );
return;
}
// store current selection, clear selection, select current shape
var curSel = doc.selection;
doc.selection = null;
myImg.selected = true;
// create temp path
var tempPath = doc.pathItems.add();
// if there's a fill color present
if( tempPath.filled ) {
var fill = tempPath.fillColor;
// if fill color is a SpotColor
if( fill.typename == 'SpotColor' ) {
alert( "Fill color is " + fill.spot.name );
}
// if fill color is an RGBColor
else if( fill.typename == 'RGBColor' ) {
alert( "Fill color is R=" + fill.red + " G=" + fill.green + " B=" + fill.blue );
}
}
// blast the temp path
tempPath.remove();
// reassign the previous selection
myImg.selected = false;
doc.selection = curSel;
}
AlertFillColorName();
If I mixed anything up or if you know of a more elegant method please let me know! 🙂
I still haven't figured out a good way to colorize a grayscale image with a spot color through a script though...
Thanks!