Histogram Selected Layer Values

Does anyone know the way to change the histogram from “Entire Image” to “Source Layer” in javascript?

Alchemist can give me the Histograms values as a 256 value Array, with the total pixel count as the last value, however I can’t figure out how to access the other settings the Histogram has

Alchemist gives the below

[
   {
      "histogram": [0,
                                         //255 more 0's
                    50                  //total number of pixels
]
   }
]

Basically i’m trying to create a function that can check a selection to see if there are any pixels in it, so maybe there is another way

You can create a pixel selection within which you will read the histogram. I think this should work. But if your layers are not perfect rectangles then it gets more complicated.

You might want to try another approach, just using the Selection Bounds.
Something like that :

var selectionExists, selBounds;
async function getSelectionBounds() {
  try {
    const result = await require("photoshop").action.batchPlay(
      [{
        "_obj": "get",
        "_target": [{
            "_property": "selection"
          },
          {
            "_ref": "document",
            "_enum": "ordinal",
            "_value": "targetEnum"
          }
        ]
      }], {
        "synchronousExecution": false,
        "modalBehavior": "execute"
      });
    const left = result[0].selection.left._value;
    const top = result[0].selection.top._value;
    const right = result[0].selection.right._value;
    const bottom = result[0].selection.bottom._value;
    selBounds = [left, top, right, bottom];
    selectionExists = true;
    console.log("selectionExists:", selectionExists, selBounds)
    return selectionExists = true;
  } catch (e) {
    console.warn("No selection")
    return selectionExists = false;
  }
}

It will return and warn if no selection:
Screenshot 2021-06-15 at 20.44.28

If a selection is active, then it will show you the Bounds in an array:
Screenshot 2021-06-15 at 20.44.17

Hope this helps, I use that all the time :wink:

Thanks Pierre, this is useful. Do you know if there is a way to check a rectangular selection to see if it has any pixels inside it?

You know how PS throws a warning when you try and transform a selection with no pixels, that’s what I need to somehow access

That’s what the function above does actually. Or maybe I didn’t get your question. Function returns false if no pixels in the selection. Else, you get the selection coordinates of it. Makes sense?

For me it detects if a selection is active, and if it is, give the 4 boundary points as an array, if no selection is active then it returns false - is that correct?

That’s a useful function, but what I’m after specifically is some sort of function that can detect if there are any rasterized pixels inside the selection.

The histogram has the ability to show if there are any pixels within the selection on the active layer when it’s switched to “Selected Layer”. If it’s set to document it works on all layers merged.

Basically I have a function that makes a selection in a constant area of a document, then does a warp/transform on the selected layer. I’m trying to get a method of stopping the function running in case the user hasn’t moved the image/layer within the selection bounds which would result in the “Could not transform the selected pixels because the selected area is empty.” warning