Répondu
How do I tell if a layer is solid
- May 22, 2023
- 1 commentaire
- 1031 vue
Thank you very much for your answer. Maybe my title is wrong. I mean to judge whether the current layer is a pure color layer
By @meadX
Ah, well that is different.
Will sampling the four corners and the centre be accurate enough?
What about the histogram? The rounded RGB Luminosity mean value should equal the rounded median value. You could also compare the separate R, G and B rounded mean vs. rounded median values, or work in a copy in Lab colour mode only comparing the L channel value etc.
P.S. I am only "brainstorming" ideas, I'm not the guy to write the code!
Duplicate the target layer. Filter > Blur > Average. Change to Difference blend mode. Check the histogram for the mean or median value that is not equal to zero. Remove the dupe layer.
/*
Check If Active Layer Is a Flat Color.jsx
v1.0, 22nd May 2023 - Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-do-i-tell-if-a-layer-is-solid/td-p/13806717
Special thanks to SuperMerlin for the Histogram Mean value code:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/smart-exposure-compensation-script/m-p/11092968
*/
#target photoshop
if (app.documents.length > 0) {
// Selected layer check by jazz-y
s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayers'));
r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
if (executeActionGet(r).getList(p).count) {
function main() {
dupeLayer("_Temp_Diff-Check");
activeDocument.activeLayer.blendMode = BlendMode.DIFFERENCE;
Avrg();
var mean = meanHist(activeDocument.histogram).toFixed(0);
if (mean != 0) {
var notFlat = "The layer isn't a flat color!";
alert(notFlat);
} else {
var isFlat = "The layer is a flat color!";
alert(isFlat);
}
if (activeDocument.activeLayer.name = "_Temp_Diff-Check") {
activeDocument.activeLayer.remove();
}
}
app.activeDocument.suspendHistory("Flat color check", "main()");
} else {
alert('A layer must be selected!');
}
} else {
alert('You must have a document open!');
}
function meanHist(hist) {
var acc = 0;
var cnt = 0;
for (var i = 0; i < hist.length; i++) {
acc += i * hist[i];
cnt += hist[i];
}
return acc / cnt;
}
function dupeLayer(layName) {
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var list = new ActionList();
var reference = new ActionReference();
reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("null"), reference);
descriptor.putString(s2t("name"), layName);
descriptor.putInteger(s2t("version"), 5);
list.putInteger(6);
descriptor.putList(s2t("ID"), list);
executeAction(s2t("duplicate"), descriptor, DialogModes.NO);
}
function Avrg() {
var c2t = function (s) {
return app.charIDToTypeID(s);
};
executeAction(c2t("Avrg"), undefined, DialogModes.NO);
}
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.