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];  
    
}
6 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.