• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

how to determine if a layer is all the same color?

Community Beginner ,
Jun 01, 2010 Jun 01, 2010

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.

TOPICS
Actions and scripting

Views

450

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Advisor ,
Jun 01, 2010 Jun 01, 2010

Copy link to clipboard

Copied

One way of checking would be like this:

  1. Make only that channel visible
  2. Copy Merged
  3. Paste
  4. Rotate the layer 180%
  5. Set the blend mode to Difference
  6. Check the doc.histogram array. If the all elements except the first are 0, then it's all one color

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Jun 01, 2010 Jun 01, 2010

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.histogram.length;
    var h = activeDocument.channels.histogram;
    for (var i = 0; i < hEnd; i++) {
        if (h > 0) {
            allSame++;
            if (allSame > 1) {
                allSame = false;
                break;
            }
        }
    }
}

for (var i = 0; i < allSame.length; i++) {
    if (allSame == 1) {
        allSame = true;
    }
}

alert(allSame);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 01, 2010 Jun 01, 2010

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines