2 Replies Latest reply on Nov 19, 2010 2:52 PM by thebestcpu

    Filling a channel results in incorrect color

    RudeTudeDude

      This is a question pertaining to automation with JavaScript, however I have noticed this problem also shows up when using actions within photoshop as well. I am simply trying to automate the process of filling a channel with a color. For some reason, when you automate the fill command on a channel, the result is not the color you specified. For example, running the following code will fill the channel "Alpha 1" with 149,149,149 rather than 128, 128, 128. Is this just a bug, or am I missing something?

       

      #target photoshop


      app.bringToFront();

      app.activeDocument.selection.selectAll();

      // Make "Alpha 1" the active channel
      app.activeDocument.activeChannels = [app.activeDocument.channels.getByName("Alpha 1")];

      // Create a color to fill the "Alpha 1" channel with.
      var fillColor = new SolidColor();
      fillColor.rgb.red = 128;
      fillColor.rgb.green = 128;
      fillColor.rgb.blue = 128;

      app.activeDocument.selection.fill(fillColor);
        • 1. Re: Filling a channel results in incorrect color
          Michael L Hale Level 5

          There are two things going on. One is a single channel can not contain a RGB color. For channels you should use fillColor.gray.gray = someValue; You can use an RGB color but it will be converted( see below )

           

          The other thing is unless the document is in Grayscale mode that gray color has to be converted into whatever the document's color mode is and that conversion is made using the defalut gray profile in the apps color settings. For example if I run your code with the defalut gray profile set to 'dot gain 20%' I get the same results( 149, 149, 149 ). But if I change it to 'Gray Gamma 2.2' I get 129, 129, 129 which is closer to what you seem to expect.

          1 person found this helpful
          • 2. Re: Filling a channel results in incorrect color
            thebestcpu Level 1

            This same number anomaly bugged me for a while until I figured it out.   Michael is correct that the single channels are interpretted as grayscale K values in the set grayscale space.   I do not believe it is just the conversion from color to grayscale however.

             

            I concluded that in some circumstances, Photoshop writes to the channel with a conversion from color to gray K values and sometimes it does not.

             

            The issue of the 149 stems from writing directly in the channel without conversion, yet reading back out with a conversion.   128s are written in without conversion.  The numbers in the isolated channel are interpretted as grayscale K values  128/255=50%.     When reading back out this grayscale value, it looks at both the present grayscale space and the present color space.  When you have a grayscale space of 20% Dot Gain and an sRGB color space, a K value of 50% would be converted to an RGB value of 149.  The acutal value that comes out is dependent on both the grayscale space and the color space set at the moment. (you would get a smaller number with a larger gamut color space)

             

            So you ended up doing a round trip writing into the channel and reading back out of the channel.  You would end up with the same number if there was a color space conversion both directions.  With your code, you have no conversion writing in, yet you have a conversion reading out.  Depending on what you are tyring to do, this may be what you need.  If not there are alternative approaches and Michael mentioned one of several possibilities.  If this needs more clarification, ask more question and let us know what you are tyring to achieve.  Writing to the channel with conversion can be done.

             

            So why you may ask did Photoshop do it this way.  I think because the channels play double duties.  1) As grayscale values tied to the set grayscale space and 2) As values used directly in determining opacity/transparency.  They chose reading as if always #1.    That's my best guess.  Hope this helps.