Skip to main content
michal-44680644
Participating Frequently
December 7, 2017
Question

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

  • December 7, 2017
  • 4 replies
  • 2372 views

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.

This topic has been closed for replies.

4 replies

Jarda Bereza
Inspiring
December 7, 2017

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

Legend
December 7, 2017

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.

Legend
December 7, 2017

//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);}

    }

Kukurykus
Legend
December 7, 2017

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

Legend
December 7, 2017

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.

Kukurykus
Legend
December 7, 2017

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()