How to select specific color using UXP plugin

I would like to be able to select the most light color of the layer just like using the wand tool.

The layer will be in black and whites (and grey) but not sure if this can be done even with batchplay.

The ultimate goal is to select the lightest color from the layer and add a gradient in new layer.

Thanks in advance

Yes, that’s possible.
Take this as an example image:

You can read out the documents histogram with batchPlay. If you just need the brightest color of one specific layer, either merge your layers before that or make only said layer visible.

The histogram is an array of 255 values, representing the luminosity and the count of pixels for each luminosity. Btw, while researching this I learned that this is perceived luminosity, not absolute luminosity.
Here’s an excerpt of the histogram for my example document:
(Subtract 1 as array indexes start at 0, but my VSC line numbers start at 1)

image

image

As you can see, there are 18225 pixels with luminosity 0, representing the black rectangle of my image. The red rectangle has the same size and therefore the same amount of pixels for that luminosity value, which is 146.

Since you want the brightest color, reverse the array (to start at 255) and get the first index whose value is greater than 0 ( meaning that there are pixels in your image for that luminosity value).
Also, since we reversed the array, we should subtract the index from 255 to get the index of the non-reversed array.

const brightestLuminosity = 255 - histogram.reverse().findIndex(pixels => pixels > 0)

In my example, this results to 146, as expected.

You can then use the threshold filter to get a black and white representation of your document showing a specific luminosity (the brightest one we just found out) as white and all other luminosities as black:

Then simply make a pixel selection from the documents channel (for example RGB channel) et voilà:

You can use that selection for further processing like adding a gradient etc.

Thanks for your help!

I think I wasn’t quite familiar with batchplay yet.

So I assume using threshold filter and pixel selection and adding gradient is also available with batchplay?

I think I will need to look up for some documentations for histogram first

Yep, all mentioned functions are possible with batchPlay.

Thanks lastly, is it possible to get a histogram of a layer not the whole document?

also for some reason

const brightestLuminosity = 255 - histogram.reverse().findIndex(pixels => pixels > 0)

is returning 256 since histogram.reverse().findIndex(pixels => pixels > 0) is returning -1

:point_down:


findIndex() returns -1 if no element of the array met the condition. So if your histogram contains only zeros, there are no pixels in your document. Do you have a document opened, which is not empty?

I managed to solve it, I just had to make it histogram[0].histogram

Thanks for the help

Ah yes, batchPlay results are objects wrapped in an array and have the property that was read as a key.
You’re welcome :slight_smile:

Would there be any good documentation to have a look at batchPlay for a beginner?

I am using Alchemist but having hard time trying to edit the output

for example I would like to show and hide layer by name but alchemist only allows me to do selected layers :frowning:

There’s no documentation on batchPlay, except for the rough syntax on how to use it.
I’d recommend:

  • play around with Alchemist. For example recording selecting a layer, gives the following:
    image
    So this shows already how to reference a layer by its name. There’s only a handful of other reference types, which I’ve mentioned in some posts on this forum, so the next tip is:
  • read a lot in this forum and look at batchPlay examples
  • maybe take a look at my photoshop-types repo, if you want a global overview of what classes and properties there are in Photoshop.

Regarding documentation: Have you seen @DavideBarranca YouTube series?
He has this great 3 part mini series about batchPlay and the Alchemist plugin: