3 Replies Latest reply on Jun 1, 2010 6:17 PM by Michael L Hale

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

    oscar060384

      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.

        • 1. Re: how to determine if a layer is all the same color?
          xbytor2 Level 4

          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

          • 2. Re: how to determine if a layer is all the same color?
            Tom Ruark Adobe Employee

            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[i] = 0;
            }

            for (var c = 0; c < cEnd; c++) {
                var hEnd = activeDocument.channels[c].histogram.length;
                var h = activeDocument.channels[c].histogram;
                for (var i = 0; i < hEnd; i++) {
                    if (h[i] > 0) {
                        allSame[c]++;
                        if (allSame[c] > 1) {
                            allSame[c] = false;
                            break;
                        }
                    }
                }
            }

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

            alert(allSame);

            • 3. Re: how to determine if a layer is all the same color?
              Michael L Hale Level 5

              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.