Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Illustrator javascript find color of RasterItem

Explorer ,
May 25, 2019 May 25, 2019

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.

1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
New Here ,
Jan 30, 2021 Jan 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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 23, 2023 Apr 23, 2023
LATEST

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.
     * @author m1b
     * @version 2023-04-24
     * @Param {Document} doc - an Illustrator Document.
     * @Param {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.
     * @author m1b
     * @version 2023-04-24
     * @Param {Document} doc - an Illustrator Document.
     * @Param {RasterItem} item - an Illustrator RasterItem.
     * @Param {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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines