Hide the layer with less color
I have two layers of color on top of each other, is there a way to calculate and hide the layer with less or lighter color?
I would like to ask for a script to do that

I have two layers of color on top of each other, is there a way to calculate and hide the layer with less or lighter color?
I would like to ask for a script to do that

Try the following script. It assumes the target layers are the 2nd and 3rd in the stack from the bottom. The colour of each target layer will be compared using Lab colour where the a and b values of zer0 = neutral. The layer closest to neutral will be automatically hidden.
/*
Hide Closest Layer to Neutral of Target Pair.jsx
v1.0, 24th December 2024
Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/hide-the-layer-with-less-color/td-p/15054871
*/
#target photoshop
try {
// Ensure there are at least 3 layers, assuming that the target layers are the 2nd and 3rd in the stack
if (app.activeDocument.artLayers.length < 3) {
throw new Error("There should be a back layer and at least two additional target layers directly above!");
}
// Get the Back(ground) layer and the first two layers above it
var backLayer = app.activeDocument.artLayers[app.activeDocument.artLayers.length - 1];
var targetLayerA = app.activeDocument.artLayers[app.activeDocument.artLayers.length - 2];
var targetLayerB = app.activeDocument.artLayers[app.activeDocument.artLayers.length - 3];
// Get the average Lab color of each layer
var labColor1 = getAverageLabColor(targetLayerA);
var labColor2 = getAverageLabColor(targetLayerB);
// Calculate the distance from neutral for each layer
var distance1 = distanceFromNeutral(labColor1);
var distance2 = distanceFromNeutral(labColor2);
// Hide the layer that is closer to neutral
if (distance1 < distance2) {
targetLayerA.visible = false;
var layerName = targetLayerA.name;
} else {
targetLayerB.visible = false;
layerName = targetLayerB.name;
}
//alert('Layer "' + layerName + '" hidden!');
} catch (error) {
alert('Error!' + '\r' + error + '\r' + 'Line: ' + error.line);
}
function distanceFromNeutral(labColor) {
var a = labColor.a;
var b = labColor.b;
return Math.sqrt(a * a + b * b);
}
function getAverageLabColor(layer) {
var samplePoint = [layer.bounds[0] + (layer.bounds[2] - layer.bounds[0]) / 2, layer.bounds[1] + (layer.bounds[3] - layer.bounds[1]) / 2];
var sampleColor = app.activeDocument.colorSamplers.add(samplePoint);
var labColor = sampleColor.color.lab;
sampleColor.remove();
return labColor;
}
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.