@GregCont
Give this script a go:
/*
Recolour Colour Label Layers from Palette Swatches.jsx
v1.0 - 7th August 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-change-fill-color-layer/td-p/14778076
*/
#target photoshop
/***** Stage 1 - Get the colour palette swatches *****/
// Hide the Photoshop panels
app.togglePalettes();
// Select the colour palette layer
app.activeDocument.activeLayer = app.activeDocument.layers.getByName('COLORPALETTE');
// Edit the smart object
if (app.activeDocument.activeLayer.kind == LayerKind.SMARTOBJECT) {
app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
// Delete color samplers
app.activeDocument.colorSamplers.removeAll();
// Add a colour sampler to the primary swatch
app.activeDocument.activeLayer = app.activeDocument.layerSets.getByName('DETAILS').layerSets.getByName('COLOR').layers[2];
addSamplerToLayerCentre();
// Add a colour sampler to the secondary swatch
app.activeDocument.activeLayer = app.activeDocument.layerSets.getByName('DETAILS').layerSets.getByName('COLOR').layers[0];
addSamplerToLayerCentre();
// Add a colour sampler to the tertiary swatch
app.activeDocument.activeLayer = app.activeDocument.layerSets.getByName('DETAILS').layerSets.getByName('COLOR').layers[1];
addSamplerToLayerCentre();
// Get the primary swatch values
// Math.round || Math.floor || Math.ceil
var primarySamplerR = Math.round(app.activeDocument.colorSamplers[0].color.rgb.red);
var primarySamplerG = Math.round(app.activeDocument.colorSamplers[0].color.rgb.green);
var primarySamplerB = Math.round(app.activeDocument.colorSamplers[0].color.rgb.blue);
// Get the secondary swatch values
var secondarySamplerR = Math.round(app.activeDocument.colorSamplers[1].color.rgb.red);
var secondarySamplerG = Math.round(app.activeDocument.colorSamplers[1].color.rgb.green);
var secondarySamplerB = Math.round(app.activeDocument.colorSamplers[1].color.rgb.blue);
// Get the tertiary swatch values
var tertiarySamplerR = Math.round(app.activeDocument.colorSamplers[2].color.rgb.red);
var tertiarySamplerG = Math.round(app.activeDocument.colorSamplers[2].color.rgb.green);
var tertiarySamplerB = Math.round(app.activeDocument.colorSamplers[2].color.rgb.blue);
// Close the smart object doc without saving
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
} else {
alert("The layer isn't a smart object layer!");
}
function addSamplerToLayerCentre() {
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var layerBounds = app.activeDocument.activeLayer.bounds;
var hor = layerBounds[2] - layerBounds[0];
var ver = layerBounds[3] - layerBounds[1];
var hCentre = hor / 2 + layerBounds[0];
var vCentre = ver / 2 + layerBounds[1];
app.activeDocument.colorSamplers.add([hCentre, vCentre]);
app.preferences.rulerUnits = savedRuler;
}
/***** Stage 2 - Recolour the layers *****/
// Set the active document
var doc = app.activeDocument;
if (!documents.length) {
alert('There are no documents open!');
} else {
activeDocument.suspendHistory('Recolour Colour Label Layers from Palette Swatches.jsx', 'main()');
}
///// Main Function /////
function main() {
// Script running notification window - courtesy of William Campbell
/* https://www.marspremedia.com/download?asset=adobe-script-tutorial-11.zip
https://youtu.be/JXPeLi6uPv4?si=Qx0OVNLAOzDrYPB4 */
var working;
working = new Window("palette");
working.preferredSize = [300, 80];
working.add("statictext");
working.t = working.add("statictext");
working.add("statictext");
working.display = function (message) {
this.t.text = message || "Script running, please wait...";
this.show();
app.refresh();
};
working.display();
// Call the file processing function
processAllLayersAndSets(doc.layers);
// Ensure Photoshop has focus before closing the running script notification window
app.bringToFront();
working.close();
// End of script notification
app.beep();
alert("Script completed!");
// Restore the Photoshop panels
app.togglePalettes();
///// Sub-Functions /////
function processAllLayersAndSets(layers) {
// Loop over the layers
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];
// Store the current visibility state
var originalVisibility = layer.visible;
// Temporarily make the layer invisible
layer.visible = false;
// Select the layer
doc.activeLayer = layer;
// Layer label colours: "none", "red", "orange", "yellowColor", "grain", "blue", "violet", "gray" - New in 2024: "magenta", "seafoam", "indigo", "fuchsia"
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var desc = executeActionGet(ref);
var theLabelColID = desc.getEnumerationValue(stringIDToTypeID('color'));
if (theLabelColID === stringIDToTypeID("orange")) {
// Primary recolouring
if (app.activeDocument.activeLayer.kind == LayerKind.SOLIDFILL) {
setSolidFill(primarySamplerR, primarySamplerG, primarySamplerB);
}
} else if (theLabelColID === stringIDToTypeID("red")) {
// Secondary recolouring
if (app.activeDocument.activeLayer.kind == LayerKind.SOLIDFILL) {
setSolidFill(secondarySamplerR, secondarySamplerG, secondarySamplerB);
}
} else if (theLabelColID === stringIDToTypeID("yellowColor")) {
// Tertiary recolouring
if (app.activeDocument.activeLayer.kind == LayerKind.SOLIDFILL) {
setSolidFill(tertiarySamplerR, tertiarySamplerG, tertiarySamplerB);
}
}
// Restore the original layer visibility
layer.visible = originalVisibility;
// If it's a layer group, process the nested layers
if (layer.typename == "LayerSet") {
processAllLayersAndSets(layer.layers);
}
}
}
function setSolidFill(theRvalue, theGvalue, theBvalue) {
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var descriptor3 = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated(s2t("contentLayer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("null"), reference);
descriptor3.putDouble(s2t("red"), theRvalue);
descriptor3.putDouble(s2t("grain"), theGvalue);
descriptor3.putDouble(s2t("blue"), theBvalue);
descriptor2.putObject(s2t("color"), s2t("RGBColor"), descriptor3);
descriptor.putObject(s2t("to"), s2t("solidColorLayer"), descriptor2);
executeAction(s2t("set"), descriptor, DialogModes.NO);
}
}