17 Replies Latest reply on May 3, 2016 4:43 AM by JJMack

    Javascript colorsamplers only returns 255 or 0

    jime75168259 Level 1

      I using Photoshop CC2015 on a Windows 8.1 PC, Coding in Javascript.

       

       

      I'm trying to get the color of some pixels. I was working fine but now it is only returning 255 or 0.

       

       

      My code:

        theSampler1 = doc.colorSamplers.add([580, 380]);

          $.writeln(theSampler1.color.rgb.red);

          $.writeln(theSampler1.color.rgb.green);

          $.writeln(theSampler1.color.rgb.blue);

       

       

      Returns 0,0,0 but should be 79,131,7.

      It was working fine, but now it is not. Is there a setting or something I changed and need to change back?

        • 1. Re: Javascript colorsamplers only returns 255 or 0
          JJMack Most Valuable Participant

          Did you perhaps put a black dot at position 580,380 in the document

          • 2. Re: Javascript colorsamplers only returns 255 or 0
            dominique filiol Level 1

            Bonjour

             

            Try this before and at end :

             

            activeDocument.colorSamplers.removeAll();

            • 3. Re: Javascript colorsamplers only returns 255 or 0
              SuperMerlin Level 5

              Make sure your ruler units are set to pixels.

              • 4. Re: Javascript colorsamplers only returns 255 or 0
                jime75168259 Level 1

                Not a black dot, I do have the removeAll();, and it is set to Units.PIXELS.

                 

                preferences.typeUnits = TypeUnits.PIXELS;

                preferences.rulerUnits = Units.PIXELS;

                 

                for(var i = 0; i < doc.layers.length; i++){

                        var layer = doc.layers[i]

                        doc.colorSamplers.removeAll();

                        if(layer.isBackgroundLayer){

                                    //Pixel sample1

                                    theSampler1 = doc.colorSamplers.add([1208, 1252);

                                    layersArr [0] = Math.round(theSampler1.color.rgb.red);

                                    layersArr [1] = Math.round(theSampler1.color.rgb.green);

                                    layersArr [2] = Math.round(theSampler1.color.rgb.blue);

                               }

                       else{

                                    theSampler1 = doc.colorSamplers.add([580, 380]);

                                    layersArr [0] = Math.round(theSampler1.color.rgb.red);

                                    layersArr [1] = Math.round(theSampler1.color.rgb.green);

                                    layersArr [2] = Math.round(theSampler1.color.rgb.blue);

                               }

                          }

                 

                I'm actually check 4 different pixels in the background and all of them are returning 255, 255, 255.

                Then I'm checking 4 different pixels on the next layers that are not background, and not the same location. They return 0,0, 0.

                The code was above is shorten, not the full code. It was working fine, then all the sudden it stop returning the correct values. I have restart the apps and PC.

                 

                Thanks for you helps so far, any other ideas?

                • 5. Re: Javascript colorsamplers only returns 255 or 0
                  JJMack Most Valuable Participant

                  I'm on Windows 10 CC 2015 with the four update you code works on my system once I added the missing bracket ], defining the layerArr and setting doc your code worked.

                  Are you sure at your sample points your doc has a color  other than black.  I add a layer where  I had red and 50% gray in those areas and that was what was set into you layerArr array. which you reuse for the second sample

                   

                  Capture.jpg

                  • 6. Re: Javascript colorsamplers only returns 255 or 0
                    jime75168259 Level 1

                    Yes, I'm sure about the color, This is what my output was:

                    {"Black & White 1 layer0":"[pixel:580x380,R:79,G:131,B:7,Opacity100]",

                    "Black & White 1 layer0":"[pixel:412x2592,R:142,G:187,B:148,Opacity100]",

                    "Black & White 1 layer0":"[pixel:3224x1080,R:36,G:20,B:4,Opacity100]",

                    "Black & White 1 layer0":"[pixel:2544x2676,R:72,G:120,B:34,Opacity100]",

                    "Background":"[pixel:1208x1252,R:168,G:79,B:0,Opacity100]",

                    "Background":"[pixel:1308x2236,R:131,G:54,B:0,Opacity100]",

                    "Background":"[pixel:2180x1296,R:201,G:110,B:5,Opacity100]",

                    "Background":"[pixel:2324x2348,R:151,G:71,B:0,Opacity100]", }

                     

                    I have change the sample points and keep getting the same output of 0,0,0 or 255,255,255. The picture is of a yellowish fruit called a Carambola. All the background points are on the fruit. The other points are a gray scale color on the B&W layer.

                     

                    Same picture, nothing has changed in the code or the picture. So I think it may be a setting in Photoshop?

                    • 7. Re: Javascript colorsamplers only returns 255 or 0
                      JJMack Most Valuable Participant

                      Here the code I use It looks like your and worked for me.

                      preferences.typeUnits = TypeUnits.PIXELS;
                      preferences.rulerUnits = Units.PIXELS;
                      
                      var doc = app.activeDocument;
                      var layersArr = new Array();
                      
                      
                      var colors = ''; 
                       for (var s=0,len=app.activeDocument.colorSamplers.length;s<len;s++) {
                        var colorSamplerRef = app.activeDocument.colorSamplers[s];
                        colors = colors + ("ColorSampler" + s + " x=" + colorSamplerRef.position[0].value + " ,y=" + colorSamplerRef.position[1].value + " Color=" 
                        + Math.round(colorSamplerRef.color.rgb.red) + "," 
                        + Math.round(colorSamplerRef.color.rgb.green) + "," 
                        + Math.round(colorSamplerRef.color.rgb.blue) + "\n" );
                        }; 
                      
                      
                      for(var i = 0; i < doc.layers.length; i++){
                              var layer = doc.layers[i];
                        //alert(layer.name);
                              doc.colorSamplers.removeAll();
                              if(layer.isBackgroundLayer){
                                          //Pixel sample1
                                          theSampler1 = doc.colorSamplers.add([1208, 1252]);
                                          layersArr [0] = Math.round(theSampler1.color.rgb.red);
                                          layersArr [1] = Math.round(theSampler1.color.rgb.green);
                                          layersArr [2] = Math.round(theSampler1.color.rgb.blue);
                        colors = colors + ("\ntheSampler1 x=" + theSampler1.position[0].value + " ,y=" + theSampler1.position[1].value + " Color=" 
                        + layersArr[0] + ","  + layersArr[1] + "," + layersArr[2] + "\n");
                                     }
                             else{
                                          theSampler1 = doc.colorSamplers.add([580, 380]);
                                          layersArr [0] = Math.round(theSampler1.color.rgb.red);
                                          layersArr [1] = Math.round(theSampler1.color.rgb.green);
                                          layersArr [2] = Math.round(theSampler1.color.rgb.blue);
                        colors = colors + ("\ntheSampler1 x=" + theSampler1.position[0].value + " ,y=" + theSampler1.position[1].value + " Color=" 
                        + layersArr[0] + ","  + layersArr[1] + "," + layersArr[2] + "\n");
                                     }
                                }
                      
                      alert(colors)
                      
                      1 person found this helpful
                      • 8. Re: Javascript colorsamplers only returns 255 or 0
                        jime75168259 Level 1

                        Okay, this is very strange! So I change a value that did not show in my code to you and now it is working.

                        Here is what I changed:

                        Before:

                        strJSON =  strJSON + "\"layer" + i + ":pixel2544x2676"\":\"RGB[" + layersArr[0] + "," + layersArr[1] + "," + layersArr[2] + "]\", ";

                        After:

                        strJSON =  strJSON + "\"layer" + i + "-2544x2676-" + layer.kind + "\":\"RGB[" + layersArr[0] + "," + layersArr[1] + "," + layersArr[2] + "]\", ";

                         

                        I removed the ":" from the string and now it is working fine.

                        • 9. Re: Javascript colorsamplers only returns 255 or 0
                          michelr31372089 Level 2

                          Hi all,

                          Just a question : when the background layer is active, if you select Layer...New...

                          Do you read "Layer from Background" or "Background from Layer" ?

                          • 10. Re: Javascript colorsamplers only returns 255 or 0
                            michelr31372089 Level 2

                            Forget my question as your code is working now. Next time, I think it will be more useful to have a copy of the exact code.

                            • 11. Re: Javascript colorsamplers only returns 255 or 0
                              jime75168259 Level 1

                              Second update. So this morning is was not working correctly again. So it was not the code. What I have discovered is that in the Layers Panel, the thumbnail needs to be selected for my code to get the color of a pixel.

                               

                              Layers.png

                               

                              When anything else is selected in the Layer Panel, I get the RGB:0,0,0, or RBG:255,255,255. So it was not the code but a setting in Photoshop. Still not 100% sold on this as a full solution.

                              • 12. Re: Javascript colorsamplers only returns 255 or 0
                                pixxxel schubser MVP & Adobe Community Professional

                                jime75168259,

                                sure, it is the code:

                                // ...
                                if(layer.isBackgroundLayer) {
                                    //Pixel sample1
                                    theSampler1 = doc.colorSamplers.add([1208, 1252);
                                    layersArr [0] = Math.round(theSampler1.color.rgb.red);
                                    layersArr [1] = Math.round(theSampler1.color.rgb.green);
                                    layersArr [2] = Math.round(theSampler1.color.rgb.blue);
                                    } else { // !!! this is the reason for black or white values if something else in the layers panel is selected
                                    theSampler1 = doc.colorSamplers.add([580, 380]);
                                    layersArr [0] = Math.round(theSampler1.color.rgb.red);
                                    layersArr [1] = Math.round(theSampler1.color.rgb.green);
                                    layersArr [2] = Math.round(theSampler1.color.rgb.blue);
                                }
                                // ...
                                

                                 

                                Have fun

                                 

                                • 13. Re: Javascript colorsamplers only returns 255 or 0
                                  JJMack Most Valuable Participant

                                  I would think if a layer mask was targeted you would only get shades of gray. ie 0.0.0 and 255,255.255 are shades of gray. So in your Script the first thing you should do is target the RGB channel.  You may need to use action manager code to do that.  I had to in CS2.

                                   

                                  There was a bug in scripting CS2. CS2  would not place in a new image smart object layer. if the current target was a layer mask.  That should not have mattered the script work fine in CS3.  I could place in an imaged add a layer mask and keep repeating  the process. In CS2 I had to add the select RGB Channel before the place in case I had placed an image and masked it for the mask became the current thing target on the current layer That I wanted to place in a new layer over.  It should have not mattered if the layers content or the mask was the target.  As long as it was on the correct layer.

                                   

                                  // =======avoid bug in cs2 ====================================
                                  var idslct = charIDToTypeID( "slct" );
                                      var desc5 = new ActionDescriptor();
                                      var idnull = charIDToTypeID( "null" );
                                          var ref3 = new ActionReference();
                                          var idChnl = charIDToTypeID( "Chnl" );
                                          var idChnl = charIDToTypeID( "Chnl" );
                                          var idRGB = charIDToTypeID( "RGB " );
                                          ref3.putEnumerated( idChnl, idChnl, idRGB );
                                      desc5.putReference( idnull, ref3 );
                                      var idMkVs = charIDToTypeID( "MkVs" );
                                      desc5.putBoolean( idMkVs, false );
                                  executeAction( idslct, desc5, DialogModes.NO );
                                  • 14. Re: Javascript colorsamplers only returns 255 or 0
                                    jime75168259 Level 1

                                    How do I target the RGB Channel?

                                    • 15. Re: Javascript colorsamplers only returns 255 or 0
                                      JJMack Most Valuable Participant

                                      The scriptlistener code I poster does that.  If the Layer Mask is targeted and you click on the layers content icon in the layer palette to target the layers content that is what the Adobe scriptlistener plug-in recorded.  The plug-in is like Photoshop Action recorded. However it records Script code to use Photoshop Action manager. Its like an Action step. All the setting are hard coded for the step and executed. The code is not very readable.

                                       

                                      You can create script functions from script listener cord by replacing the variable hard coded constant  setting with  variable.    If you look at the code posted you can set some if the hard coded sets "slct" , "Chnl", "RGB "  select channel RGB

                                       

                                      Capture.jpg

                                      • 16. Re: Javascript colorsamplers only returns 255 or 0
                                        michelr31372089 Level 2

                                        Hi all,

                                        I think it is important to know (for a general project) if the mode is RGB or CMYK.

                                        After this, you can select the Alpha channel with the listener code given by JJMack.

                                        Now you will have only color values (if there are color informations of course).

                                         

                                        var docRef = activeDocument;
                                        
                                        var idMode = charIDToTypeID( "RGB " );
                                        if (docRef.mode==DocumentMode.CMYK) {idMode = charIDToTypeID( "CMYK" )}
                                        
                                        selectAlphaChannel()
                                        
                                        function selectAlphaChannel()
                                        {
                                        // =======================================================
                                        var idslct = charIDToTypeID( "slct" );
                                            var desc57 = new ActionDescriptor();
                                            var idnull = charIDToTypeID( "null" );
                                                var ref43 = new ActionReference();
                                                var idChnl = charIDToTypeID( "Chnl" );
                                                var idChnl = charIDToTypeID( "Chnl" );
                                                ref43.putEnumerated( idChnl, idChnl, idMode );
                                            desc57.putReference( idnull, ref43 );
                                            var idMkVs = charIDToTypeID( "MkVs" );
                                            desc57.putBoolean( idMkVs, false );
                                        executeAction( idslct, desc57, DialogModes.NO );
                                        }
                                        
                                        • 17. Re: Javascript colorsamplers only returns 255 or 0
                                          JJMack Most Valuable Participant

                                          Yes what Image mode your editing is would be important as to which channel needs to be selected.  The function could be named like

                                          function  targetLayerContent(mode)   or () where the first part of the function would use app.activeDocument.mode to find the correct channel to select.