How to get Color Sampler in UXP?

Does anyone have a snippet showing how to get a color sample from a specific location using a color sampler?

I’m not asking about the color picker. I want to be able to get a color from a specific coordinate automatically without any dialogs. Basically I just want to create a color sampler point, get the color, and then delete the color sampler.

1 Like

OK, after nearly reaching the end of the internet, I was able to find out how to do this from someone’s blog site… thank you Kawano Shuji if you are in this group :slight_smile:

In case anyone else needs to get an RGB color sample form a pixel coordinate, here is the function to return the RGB values as an array.

async function getColor(X,Y){
    
const batchPlay = require("photoshop").action.batchPlay;
    
const result = await batchPlay(
[
    {
    _obj:"colorSampler",
    _target:{_ref: "document",_enum: "ordinal",_value: "targetEnum"},
        samplePoint: {
        horizontal: X,
        vertical: Y
        }
    }
],{});

const red=result[0].colorSampler.red;
const green=result[0].colorSampler.grain;
const blue=result[0].colorSampler.blue; 
    
return [red,green,blue];  
    
}
7 Likes

Thanks, @ddbell. This is a much cleaner way of doing this compared to what I had been doing. Are you able to share a link to the Kawano Shuji blog post with this info?

1 Like

I like it because it doesn’t actually create an actual colorSampler.

Here is the link. It is all in Japanese except the sections of coding. I modified the coding from what is posted on his site. I just got the important pieces that were needed and fit it into the way I make the functions.

3 Likes

@DavideBarranca :point_up: something for you to answer? :smiley:

1 Like

Hi @ddbell,
that snippet of yours does indeed the trick.

The only drawback (of sorts) is that it requires a modal state w/ Manifest v5 (i.e., you’ll need to wrap the batchPlay call with an executeAsModal—there’s probably a "make" going on behind the scenes.

If you check an existing colorSample instead, the color is returned without the need of a modal state. Although if it’s for a one-time check, you’d need to create (modal), read, and remove (modal) a disposable sample, so in the end it’s handier the way you’ve found.

ColorSamplers are high in the ToDo list for DOM, so in there’s gonna be an ExDOM (how @Jarda uses to call the JSX DOM with his Czech humour :smile:) equivalent soon-ish.

—Davide

1 Like

Understood. I’m running API 2 and the entire sequence is under a single function using executeAsModal already with a single suspendHistory.

ExDom parity doesn’t strictly require it, but I reckon that a new Document’s method like sampleColor({ x, y }) would be useful to have.

Speaking of manifest 5, I’m still on manifest 4 / API 2. Have you heard if manifest version 5 is going to be required any time soon? I just got done converting everything from API 1 to 2 and really just want to leave the plugins alone and not mess with them unless I have to.

1 Like

@ddbell +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, …

1 Like

I haven’t heard any definitive words on that matter, although v4 has been deprecated for a while now.

No worries, I just posted a question in the forum.

I will ask in the next Pre-release meeting Q&A if I don’t have an answer by then. I’m just trying to figure out if I need to convert all of my 10 plugins before CC 2023 is released.

Does anyone know how to control the color space of the returned sample data from the flyweight sampler?

For example, with the persistent color samplers created using the tool, you can select the color space (and bit size) for each sampler point individually from the Info panel.

Those choices are then reflected in the data provided by the colorSamplerList document property.

I’m hoping there is a similar way to specify those kind of options with this flyweight sampler.

@DavideBarranca :point_up: question for you? :slight_smile:

Worth to mention that Color Samplers were recently added to DOM in PS 24.0

https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/colorsampler/
https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/colorsamplers/
https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/document/#samplecolor

And colors can be converted like this: https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/solidcolor/#properties

1 Like

Hold on - are you saying that Photoshop lets you define colour samples on an image that are showing values for a different colour space the one native to the document?!

Yes, exactly. The context menu for each color sampler in the Info panel allows you to choose the color space and bit depth of the displayed value. PS handles the color conversion and displays the appropriate value.

You can also programmatically invoke those menu options, with each color sampler having its own set of menu command IDs per color space and bit depth.

For example the IDs for RGB color and 8-bit depth are the following:

// Menu Command IDs
...
ColorSampler1_RGBColor = 3602,
ColorSampler2_RGBColor = 3612,
ColorSampler3_RGBColor = 3622,
ColorSampler4_RGBColor = 3632,
ColorSampler5_RGBColor = 3642,
ColorSampler6_RGBColor = 3652,
ColorSampler7_RGBColor = 3662,
ColorSampler8_RGBColor = 3672,
ColorSampler9_RGBColor = 3682,
ColorSampler10_RGBColor = 3692,
...
ColorSampler1_8Bit = 3751,
ColorSampler2_8Bit = 3754,
ColorSampler3_8Bit = 3757,
ColorSampler4_8Bit = 3760,
ColorSampler5_8Bit = 3763,
ColorSampler6_8Bit = 3766,
ColorSampler7_8Bit = 3769,
ColorSampler8_8Bit = 3772,
ColorSampler9_8Bit = 3775,
ColorSampler10_8Bit = 3778,
...

Very helpful, thanks!

I just played with 32bits and samplers and at some point I found a bug that I cannot reproduce.

DOM is not able to handle this kind of value for RGB. Ranges [0,255] are fine but [0,1] are not. Especially with different property names… so under certain circumstances, there could be a bug in 32bits per channel in RGB.

Fucking A, This is a total revelation! I love how even after twenty years of use PS can still surprise me!
I’ve been creating temp files in different colour spaces and reading their values for literally two decades…

1 Like