Monday, May 29, 2017

adobe photoshop - How to select all shapes with specific color and replace this color with an other one?


I am totally new in Photoshop scripting, but I have some JavaScript experience.


I want to make a script (or find an existing one and study it) that finds every shape with a specific fill color in my Photoshop project and replaces it with an other one. But I have no idea where to start.




Answer



Normally you'd expect a script for this to be something like


activeDocument.layers.forEach(function(el) {
if (el.fillColor == oldColor) el.fillColor = newColor
});

I welcome you to a world of Photoshop Scripting: a world of pain and suffering. While there's a certain number of things you can achieve using Photoshop DOM, a lot of functions and properties are only a available as Action Manager code, used to drive Photoshop at a lower level.


So while the algorithm stays the same — I look for the shape layers with a particular color and then change it — the code may look a little confusing.


var layers = getAllShapeLayersData(),
sourceColor = [255, 0, 255], // color to look for

targetColor = [128, 128, 128]; // color to change to

// creating a native Photoshop color object to compare hex values instead of RGBs
var colorToChange = new SolidColor();
colorToChange.rgb.red = sourceColor[0];
colorToChange.rgb.green = sourceColor[1];
colorToChange.rgb.blue = sourceColor[2];

// for all found layers
for (var i = 0; i < layers.length; i++)

{
// if the shape fill color hex value is the same as source color
if (layers[i].color.rgb.hexValue == colorToChange.rgb.hexValue)
{
// select it first
selectById(layers[i].id);
//apply a different color
changeShapeColor(targetColor);
}
}


function getAllShapeLayersData()
{
var lyrs = [];
try
{
activeDocument.backgroundLayer;
var layers = 0
}
catch (e)

{
var layers = 1;
};
while (true)
{
ref = new ActionReference();
ref.putIndex(charIDToTypeID('Lyr '), layers);
try
{
var desc = executeActionGet(ref);

}
catch (err)
{
break;
}

var lyr = {};
lyr.type = desc.getInteger(stringIDToTypeID("layerKind"));
lyr.name = desc.getString(charIDToTypeID("Nm "));
lyr.id = desc.getInteger(stringIDToTypeID("layerID"));


if (lyr.type == 4) // shape layer
{
var adj = desc.getList(stringIDToTypeID("adjustment")).getObjectValue(0);

if (adj.hasKey(stringIDToTypeID("color")))
{
var curColor = new SolidColor();
curColor.rgb.red = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("red"));
curColor.rgb.green = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("grain"));

curColor.rgb.blue = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("blue"));
lyr.color = curColor;
lyrs.push(lyr);
}


}


layers++;

}
return lyrs
}; // end of getAllLayersData()

function changeShapeColor(color)
{
var desc8 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(stringIDToTypeID('contentLayer'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc8.putReference(charIDToTypeID('null'), ref1);

var desc9 = new ActionDescriptor();
var desc10 = new ActionDescriptor();
desc10.putDouble(charIDToTypeID('Rd '), color[0]);
desc10.putDouble(charIDToTypeID('Grn '), color[1]);
desc10.putDouble(charIDToTypeID('Bl '), color[2]);
desc9.putObject(charIDToTypeID('Clr '), charIDToTypeID('RGBC'), desc10);
desc8.putObject(charIDToTypeID('T '), stringIDToTypeID('solidColorLayer'), desc9);
executeAction(charIDToTypeID('setd'), desc8, DialogModes.NO);
}; // end of changeShapeColor()


function selectById(id)
{
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putIdentifier(charIDToTypeID('Lyr '), id);
desc1.putReference(charIDToTypeID('null'), ref1);
executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
}; // end of selectById()

No comments:

Post a Comment

technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...