3 Replies Latest reply on Dec 10, 2017 11:21 PM by michal-44680644

    Retriving different histograms from activeDocument

    yairTechn Level 1



      In CS4 & CS5 on the histogram menu it's possible to select different  types of histograms

      from the channel drop box.


      I can select RGB, R,G,B, Colors and luminosity.

      However when I execute a DoJavaScipt on the active document I can only get

      the luminosity or specific R, G, B histograms based on the activeChannels  container.


      How can I get the RGB or Colors histogram (I know I can sum up all the  different channels (R,G,B)

      but it seems stupid since the RGB data and color is already presented.


      Do I need to write a plug-in to extract this data for me?



        • 1. Re: Retriving different histograms from activeDocument
          JJMack Most Valuable Participant

          I do not know javascript or photoshop scripting I only hack at code i steal from script I download. In the Photoshop CS5 Java scripting there seems to be a sample script names Histogram.jsx it get all four channels I never understood what Adobe fifth Colors was about. it looks like this


          // Function to activate all the channels according to the documents mode

          // Takes a document reference for input

          function TurnOnDocumentHistogramChannels(inDocument) {

              // see how many channels we need to activate

              var visibleChannelCount = 0


              // based on the mode of the document

              switch (inDocument.mode) {


                  case DocumentMode.BITMAP:

                  case DocumentMode.GRAYSCALE:

                  case DocumentMode.INDEXEDCOLOR:

                      visibleChannelCount = 1



                  case DocumentMode.DUOTONE:

                      visibleChannelCount = 2



                  case DocumentMode.RGB:

                  case DocumentMode.LAB:

                      visibleChannelCount = 3



                  case DocumentMode.CMYK:

                      visibleChannelCount = 4



                  case DocumentMode.MULTICHANNEL:


                      visibleChannelCount = inDocument.channels.length + 1




              // now get the channels to activate into a local array

              var aChannelArray = new Array()


              // index for the active channels array

              var aChannelIndex = 0


              for(var channelIndex = 0; channelIndex < inDocument.channels.length;

                      channelIndex++) {

                  if (channelIndex < visibleChannelCount) {

                      aChannelArray[aChannelIndex++] = inDocument.channels[channelIndex]




              // now activate them

              inDocument.activeChannels = aChannelArray



          // Save the current preferences

          var startRulerUnits = app.preferences.rulerUnits

          var startTypeUnits = app.preferences.typeUnits

          var startDisplayDialogs = app.displayDialogs


          // Set Adobe Photoshop CS5 to use pixels and display no dialogs

          app.preferences.rulerUnits = Units.PIXELS

          app.preferences.typeUnits = TypeUnits.PIXELS

          app.displayDialogs = DialogModes.NO


          // if there are no documents open then try to open a sample file

          if (app.documents.length == 0) {

              open(File(app.path + "/Samples/Fish.psd"))



          // get a reference to the working document

          var docRef = app.activeDocument


          // create the output file

          // first figure out which kind of line feeds we need

          if ($.os.search(/windows/i) != -1) {

              fileLineFeed = "Windows"

          } else {

              fileLineFeed = "Macintosh"



          // create the output file accordingly

          fileOut = new File("~/Desktop/Histogram.log")

          fileOut.lineFeed = fileLineFeed

          fileOut.open("w", "TEXT", "????")


          // write out a header

          fileOut.write("Histogram report for " + docRef.name)


          // find out how many pixels I have

          var totalCount = docRef.width.value * docRef.height.value


          // more info to the out file

          fileOut.write(" with a total pixel count of " + totalCount + "\n")


          // channel indexer

          var channelIndex = 0


          // remember which channels are currently active

          var myActiveChannels = app.activeDocument.activeChannels


          // document histogram only works in these modes

          if (docRef.mode == DocumentMode.RGB ||

              docRef.mode == DocumentMode.INDEXEDCOLOR ||

              docRef.mode == DocumentMode.CMYK) {


              // activate the main channels so we can get the documents histogram



              // Output the documents histogram

              OutputHistogram(docRef.histogram, "Luminosity", fileOut)



          // local reference to work from

          var myChannels = docRef.channels


          // loop through each channel and output the histogram

          for (var channelIndex = 0; channelIndex < myChannels.length; channelIndex++) {


              // the channel has to be visible to get a histogram

              myChannels[channelIndex].visible= true

              // turn off all the other channels

              for (var secondaryIndex = 0; secondaryIndex < myChannels.length;

                      secondaryIndex++) {

                  if (channelIndex != secondaryIndex) {

                      myChannels[secondaryIndex].visible= false




          // Use the function to dump the histogram


              myChannels[channelIndex].name, fileOut)



          // close down the output file


          alert("Histogram file saved to: " + fileOut.fsName)


          // reset the active channels

          docRef.activeChannels = myActiveChannels


          // Reset the application preferences

          app.preferences.rulerUnits = startRulerUnits

          app.preferences.typeUnits = startTypeUnits

          app.displayDialogs = startDisplayDialogs


          // Utility function that takes a histogram and name

          // and dumps to the output file

          function OutputHistogram(inHistogram, inHistogramName, inOutFile) {


              // find ouch which count has the largest number

              // I scale everything to this number for the output

              var largestCount = 0


              // a simple indexer I can reuse

              var histogramIndex = 0


              // see how many samples we have total

              var histogramCount = 0


              // search through all and find the largest single item

              for (histogramIndex = 0; histogramIndex < inHistogram.length;

                      histogramIndex++) {

                  histogramCount += inHistogram[histogramIndex]

                  if (inHistogram[histogramIndex] > largestCount)

                      largestCount = inHistogram[histogramIndex]



              // These should match

              if (histogramCount != totalCount) {

                  alert("Something bad is happening!")



              // see how much each "X" is going to count as

              var pixelsPerX = largestCount / 100


              // output this data to the file

              inOutFile.write("One X = " + pixelsPerX + " pixels.\n")


              // output the name of this histogram

              inOutFile.write(inHistogramName + "\n")


              // loop through all the items and output in the following format

              // 001

              // 002

              for (histogramIndex = 0; histogramIndex < inHistogram.length;

                      histogramIndex++) {


              // I need an extra "0" for this line item to keep everything in line

              if (histogramIndex < 10)



              // I need an extra "0" for this line item to keep everything in line

              if (histogramIndex < 100)



              // output the index to file



              // some spacing to make it look nice

              inOutFile.write(" ")


              // figure out how many X’s I need

              var outputX = inHistogram[histogramIndex] / largestCount * 100


              // output the X’s

              for (var a = 0; a < outputX; a++)








          • 2. Re: Retriving different histograms from activeDocument
            yairTechn Level 1

            Thanks but I already have the code you've posted its from the script manuals for CS4 ac CS5

            as you can see by looking the word "Luminosity" the output of the  histogram

            is that of Luminosity and not of the RGB(compound) or Color (RGB  intersections)

            The amazing thing is that all of the histogram data is already on the  Histogram

            but you can't get access to the compound RGB or Color channels.

            if there was a way to get these compound channels histogram data

            then It would do he trick.

            1 person found this helpful
            • 3. Re: Retriving different histograms from activeDocument
              michal-44680644 Level 1


              alert('histogram RGB:'+app.activeDocument.histogram);