15 Replies Latest reply on Dec 7, 2017 5:21 PM by r-bin

    Get histogram of current layer (not the whole's document) by javascript, CC 2017

    michal-44680644 Level 1

      After searching for a few hours I am trying to get help here.

      I can see it manually in Histogram panel and selecting "Selected layer" (I translated this name from my polish Photoshop so english may differ), but could not find any code snippets even reading scripting guide or js ref - pdfs.

        • 1. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
          Kukurykus Adobe Community Professional

          I'm affraid you must make selection of layer you want to check its histogram.

          Use this code where in the label of CLM function you have written in polish what it is for:

           

          function CLM(v1, v2, v3) {// ZAZNACZENIE  KANAŁU / MASKI / WARSTWY Z PRZEZROCZYSTOŚCIĄ:
               function cTT(v) {return charIDToTypeID(v)} (ref1 = new ActionReference()).putProperty(c = cTT('Chnl'), cTT('fsel'));
               eval('(ref2 = new ActionReference())' + (v = (v1 && v2) ? '.putEnumerated(c, cTT(v1), cTT(v2))' : ''))
               if (v3) ref2.putName(cTT(v ? 'Lyr ' : 'Chnl'), v3); (dsc = new ActionDescriptor()).putReference(cTT('null'), ref1)
               dsc.putReference(cTT('T   '), ref2), executeAction(cTT('setd'), dsc, DialogModes.NO)
          }
          
          CLM('Chnl', 'Trsp'), hst = (aD = activeDocument).histogram, aD.selection.deselect()
          
          • 2. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
            r-bin Level 5

            //You can turn off all other layers then get the histogram then turn on all layers again

             

            toggle_other_visible()
            
            var hist = app.activeDocument.histogram;
            //var hist = app.activeDocument.channels[0].histogram;
            
            toggle_other_visible()
            
            alert(hist)
            
            function toggle_other_visible()
                {
                try {
                    var desc = new ActionDescriptor();
                    var list = new ActionList();
                    var ref = new ActionReference();
                    ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
                    list.putReference( ref );
                    desc.putList( charIDToTypeID( "null" ), list );
                    desc.putBoolean( charIDToTypeID( "TglO" ), true );
                    executeAction( charIDToTypeID( "Shw " ), desc, DialogModes.NO );
                    }
                catch (e) { alert(e);} 
                }
            
            2 people found this helpful
            • 3. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
              Kukurykus Adobe Community Professional

              r-bin your function shows histogram or current layer and transparent pixels of document, but to be honest I didn't think up nothing beside that example I always used, so now I'm at least aware it can be done using also other (your) method

              • 4. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
                r-bin Level 5

                When I need to know the exact histogram, I insert a SolidColor layer (=30) under this layer and ignore the index 30 in the histogram.

                 

                • 5. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
                  Kukurykus Adobe Community Professional

                  You had to give me simple example of that becuase I can't imagine how to do it, but sounds very easy...

                  • 6. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
                    r-bin Level 5

                    Are you going to get a histogram for layers with transparency?

                    If "yes", then the method will be more difficult. Using the selection does not give an accurate result.

                    • 7. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
                      r-bin Level 5

                      There's actually a very tricky function for getting absolutely accurate clipping values for a given percentage. I'm not ready to post it yet.

                      • 8. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
                        Jarda Bereza Level 4

                        Custom C++ plugin with scripting API would be best option.

                        • 9. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
                          r-bin Level 5

                          OK. Create a 5x5 pixel file.

                          Create three transparent layers, two of which have an opacity of 20%, the third 100%.

                          Draw on one layer a pixel with a brightness of 20, on the other 128, on the third 250.

                          Merge the layers. Compare your histogram from the script with what the histogram panel shows.

                           

                          In general, I found an acceptable simple way of obtaining an accurate histogram of brightness. I combined the code from your script with mine.

                          In CS6 use should use shift_selection_cs6() function.

                           

                          try { app.activeDocument.suspendHistory("Test", "test()") } catch(e) { alert(e); }
                          
                          function test() {
                          
                          function CLM(v1, v2, v3) {
                               function cTT(v) {return charIDToTypeID(v)} (ref1 = new ActionReference()).putProperty(c = cTT('Chnl'), cTT('fsel'));  
                               eval('(ref2 = new ActionReference())' + (v = (v1 && v2) ? '.putEnumerated(c, cTT(v1), cTT(v2))' : ''))  
                               if (v3) ref2.putName(cTT(v ? 'Lyr ' : 'Chnl'), v3); (dsc = new ActionDescriptor()).putReference(cTT('null'), ref1)  
                               dsc.putReference(cTT('T   '), ref2), executeAction(cTT('setd'), dsc, DialogModes.NO)  
                          }  
                                
                          CLM('Chnl', 'Trsp') 
                          
                          shift_selection_CC(100, 0);
                          shift_selection_CC(100, 0);
                          shift_selection_CC(0, 100);
                          shift_selection_CC(0, 100);
                          
                          toggle_other_visible()  
                          
                          hst = (aD = activeDocument).histogram, aD.selection.deselect()  
                          
                          toggle_other_visible()  
                            
                          xxx=0
                          for (var i in hst) xxx+=hst[i];
                          alert(xxx+"\n\n"+hst)  
                          }
                            
                          function toggle_other_visible()  
                              {  
                              try {  
                                  var desc = new ActionDescriptor();  
                                  var list = new ActionList();  
                                  var ref = new ActionReference();  
                                  ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );  
                                  list.putReference( ref );  
                                  desc.putList( charIDToTypeID( "null" ), list );  
                                  desc.putBoolean( charIDToTypeID( "TglO" ), true );  
                                  executeAction( charIDToTypeID( "Shw " ), desc, DialogModes.NO );  
                                  }  
                              catch (e) { alert(e);}   
                              }  
                          
                          function shift_selection_cs6(shift, contrast, dlg)
                              {
                              try {
                                  var desc = new ActionDescriptor();
                          
                                  desc.putUnitDouble( stringIDToTypeID( "refineEdgeBorderRadius" ),   charIDToTypeID( "#Pxl" ),  0 );
                                  desc.putUnitDouble( stringIDToTypeID( "refineEdgeBorderContrast" ), charIDToTypeID( "#Prc" ),  contrast );
                                  desc.putInteger(    stringIDToTypeID( "refineEdgeSmooth" ),                                    0 );
                                  desc.putUnitDouble( stringIDToTypeID( "refineEdgeFeatherRadius" ),  charIDToTypeID( "#Pxl" ),  0 );
                                  desc.putUnitDouble( stringIDToTypeID( "refineEdgeChoke" ),          charIDToTypeID( "#Prc" ),  shift );
                                  desc.putBoolean( stringIDToTypeID( "refineEdgeAutoRadius" ), false );
                                  desc.putBoolean( stringIDToTypeID( "refineEdgeDecontaminate" ), false );
                                  desc.putEnumerated( stringIDToTypeID( "refineEdgeOutput" ), stringIDToTypeID( "refineEdgeOutput" ), stringIDToTypeID("refineEdgeOutputSelection") );
                          
                                  executeAction( stringIDToTypeID( "refineSelectionEdge" ), desc, (dlg == true)?DialogModes.ALL:DialogModes.NO );
                                  }
                              catch (e) { alert(e); }
                              }
                          
                          function shift_selection_CC(shift, contrast, dlg)
                              {
                              try {
                                  var desc = new ActionDescriptor();
                          
                                  desc.putInteger( stringIDToTypeID( "smartBrushRadius" ), 0 );
                                  desc.putInteger( stringIDToTypeID( "smartBrushSmooth" ), 0 );
                                  desc.putUnitDouble( stringIDToTypeID( "smartBrushFeather" ),   charIDToTypeID( "#Pxl" ), 0 );
                                  desc.putUnitDouble( stringIDToTypeID( "smartBrushContrast" ),  charIDToTypeID( "#Prc" ), contrast );
                                  desc.putUnitDouble( stringIDToTypeID( "smartBrushShiftEdge" ), charIDToTypeID( "#Prc" ), shift );
                                  desc.putBoolean( stringIDToTypeID( "sampleAllLayers" ), false );
                                  desc.putBoolean( stringIDToTypeID( "smartBrushUseSmartRadius" ), false );
                                  desc.putBoolean( stringIDToTypeID( "smartBrushDecontaminate" ), false );
                                  desc.putEnumerated( stringIDToTypeID( "refineEdgeOutput" ), stringIDToTypeID( "refineEdgeOutput" ), stringIDToTypeID( "selectionOutputToSelection" ) );
                          
                                  executeAction( stringIDToTypeID( "smartBrushWorkspace" ), desc, (dlg == true)?DialogModes.ALL:DialogModes.NO );
                                  }
                              catch (e) { alert(e); }
                              }
                          

                           

                          Another example in pictures

                           

                          your.png

                          my.png

                          • 10. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
                            Kukurykus Adobe Community Professional

                            Not everything is clear for me in your description:

                             

                            Should I draw a pixel with a brigthenss of 20 on layer with opacity 20%?

                            Should I draw a pixel with a brigthenss of 128 on layer with opacity 20%?

                            Should I draw a pixel with a brigthenss of 255 on layer with opacity 250%?

                             

                            How to draw a pixel with brightness? Should I use pencil? If so, how to set brightness?

                            What do you mean by it, as I don't see it in CS6, while the range of all sliders is 0 - 100

                            • 11. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
                              r-bin Level 5

                              no matter

                               

                              darw on 20% with pencil 1x1 with RGB(20,20,20)

                              darw on 100% with pencil 1x1with RGB(128,128,128)

                              darw on 20% with pencil 1x1with RGB(250,250,250)

                              • 12. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
                                Kukurykus Adobe Community Professional

                                Well I did, this is my result:

                                 

                                In the alert there was 1 and then histogram where after only zero values it ended with , 1, 0, 0, 0 ,0, 0

                                While compared to histogram it showed there are 25 pixels (so 5 * 5), but the count wasn't available.

                                 

                                Mean: 254.80, Std Dev: 0.99, Median: 255, but when selected then Mean: 250.00, Std Dev: 0.00, Median: 250

                                • 13. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
                                  r-bin Level 5

                                  Did you turn off the background? At me with a script it turns off automatically.
                                  Nevertheless, did you understand that your histogram is not quite correct?

                                  • 14. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
                                    Kukurykus Adobe Community Professional

                                    Yes, background was deleted, only merged 3 layers left. Yes I saw difference in results you posted

                                    • 15. Re: Get histogram of current layer (not the whole's document) by javascript, CC 2017
                                      r-bin Level 5

                                      By the way, on these pictures (with transparency) auto-options in Levels or Curves do not work, because they show not the correct histogram. Only a direct call to the layer (Ctrl-L, Ctrl-M) shows the correct histogram of the layer.

                                       

                                      And no one in Adobe is going to even fix it