Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Find more inspiration, events, and resources on the new Adobe Community
Explore Now