Copy link to clipboard
Copied
Hi,
I want to write a script that checks if a particular channel is filled by one color. I know how to parse though the channel layers, but how do I check if the entire layer is one color?
Thanks.
Copy link to clipboard
Copied
One way of checking would be like this:
This is far faster than checking pixels individually.
Another way would be to Magic Wand (0, 0) with a tolerance of 0 and feathering turned off. Clear the selection.
Select All. Then see if the selection is empty.
-X
Copy link to clipboard
Copied
Use the histogram for each channel. Make sure you only grab the histogram *once* otherwise
it will be super slow!
Here is a version that worked for me with limited testing. I'm sure it could be cleaned up.
var cEnd = activeDocument.channels.length;
var allSame = new Array(cEnd);
for (var i = 0; i < allSame.length; i++) {
allSame = 0;
}
for (var c = 0; c < cEnd; c++) {
var hEnd = activeDocument.channels
var h = activeDocument.channels
for (var i = 0; i < hEnd; i++) {
if (h > 0) {
allSame
if (allSame
allSame
break;
}
}
}
}
for (var i = 0; i < allSame.length; i++) {
if (allSame == 1) {
allSame = true;
}
}
alert(allSame);
Copy link to clipboard
Copied
If you just want to know if the layer is one color but don't care which you can do something like this
function hasUniqueColor() {
var res = false;
var rArray = activeDocument.channels["Red"].histogram.toString().replace(/0,|,0/g,"").split(',');
var gArray = activeDocument.channels["Green"].histogram.toString().replace(/0,|,0/g,"").split(',');
var bArray = activeDocument.channels["Blue"].histogram.toString().replace(/0,|,0/g,"").split(',');
if(rArray.length == 1 && gArray.length == 1 && bArray.length == 1 ){
res = true;
}
return res;
}
That of course is for a RGB doc. You could adapt that to check just one channel. I like this way because it seems faster than looping the histogram.
And sorry if I am pointing out something you already know but you can not really get the histogram of a layer. Histogram is doc wide. If you want to check a layer you need to make sure that all layer's above it in the layer stack are not visible.