Skip to main content
kayr23628964
Known Participant
May 26, 2019
Question

Illustrator javascript find color of RasterItem

  • May 26, 2019
  • 2 replies
  • 1401 views

I am able to set a RasterItem's color with colorize(), but I would also like to find a way to get it's current color from a script. One hack I found is to select the item and then look at the selected swatch, but sometimes there is no swatch that matches the item's color.

My script performs a threshold function ( assigning black or white to every fill and stroke ) as a prepress operation when I don't want my RIP to halftone anything that is almost black or almost white.

RasterItem.colorants just gives me the string "Gray"

PS: I found a possible workaround. If you select the RasterItem then create a new filled PathItem it seems to be filled wth the RasterItem's fill color, so you can grab the color then delete the pathItem.

This topic has been closed for replies.

2 replies

m1b
Community Expert
Community Expert
April 23, 2023

Here is a function that gets the color from a RasterItem. It's unfortunate that we cannot get the color directly, eg. RasterItem.fillColor, but this is a workaround.

(function () {

    var doc = app.activeDocument,
        item = doc.rasterItems[0];

    var c = getRasterItemColor(doc, item);

    if (c != undefined)
        alert(c.typename);


    /**
     * Returns Swatch or Color applied to a RasterItem.
     * @7111211 m1b
     * @version 2023-04-24
     * @9397041 {Document} doc - an Illustrator Document.
     * @9397041 {RasterItem} item - an Illustrator RasterItem.
     * @Returns {Swatch|Color}
     */
    function getRasterItemColor(doc, item) {

        var originalSelection = doc.selection;
        doc.selection = [item];
        var rasterItemColor = doc.defaultFillColor;
        doc.selection = originalSelection;

        if (rasterItemColor.constructor.name == 'SpotColor')
            return doc.swatches.getByName(rasterItemColor.spot.name);
        else
            return rasterItemColor;

    };

})();

 

And, for completeness, here is a function that sets the color of a RasterItem. Unfortunately this is necessary because the RasterItem.colorize method does not accept SpotColors.

(function () {

    var doc = app.activeDocument,
        item = doc.rasterItems[0],
        sw = doc.swatches[5];

    colorizeRasterItem(doc, item, sw);


    /**
     * Applies Swatch or Color to a RasterItem.
     * @7111211 m1b
     * @version 2023-04-24
     * @9397041 {Document} doc - an Illustrator Document.
     * @9397041 {RasterItem} item - an Illustrator RasterItem.
     * @9397041 {Swatch|Color} swatchOrColor - an Illustrator Swatch or Color.
     */
    function colorizeRasterItem(doc, item, swatchOrColor) {

        if (swatchOrColor.hasOwnProperty('color'))
            swatchOrColor = swatchOrColor.color;

        var originalSelection = doc.selection;
        doc.selection = [item];
        doc.defaultFillColor = swatchOrColor;
        doc.selection = originalSelection;

    };

})();

(Note: I am updating this old thread because it was appearing in searches and had no answer.)

- Mark

misioptysio
Participant
January 30, 2021

Hi Kayr23...,

 

did you manage to overcome the problem with reading the color of raster image? If so, would you share the piece of code? I'm developing a  colorizer script, where I change colors of selected items according to sone rules, so how does the workaround would work when you have lots of objects selected?

 

Any help will be appreciated! Take care!

 

Piotr