Skip to main content
Inspiring
March 15, 2017
Question

Read the color of a Colorized Grayscale Image

  • March 15, 2017
  • 1 reply
  • 547 views

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!

This topic has been closed for replies.

1 reply

Silly-V
Legend
March 15, 2017

Looks nice, try to use the document.defaultFillColor property:

#target illustrator

function test(){

  var doc = app.activeDocument;

  var rasterGrayScaleItem = doc.rasterItems[0];

  rasterGrayScaleItem.selected = true;

  var clr = new RGBColor();

  clr.red = 0;

  clr.green = 0;

  clr.blue = 255;

  doc.defaultFillColor = clr;

 

};

test();

To make it a spot color, assign a SpotColor object to the defaultFillColor, instead of the .color property of a spot (which would give you the color values but not the named color association)

zertleAuthor
Inspiring
March 16, 2017

!!! That is sooo good! Thank you!