Skip to main content
Participant
March 29, 2023
Answered

Extendscript Color Picker Information - Average Value?

  • March 29, 2023
  • 1 reply
  • 1188 views

Hello, in PS I am manually creating a Color Sampler with the Color Sampler Tool.

 

With that, I can then read the information from the Info Panel with my following script:

 

var colorSamplerRef = app.activeDocument.colorSamplers[0];

var currentColor = colorSamplerRef.color;

var readColorHue = currentColor.hsb.hue
var readColorSaturation = currentColor.hsb.saturation
var readColorBrightness = currentColor.hsb.brightness

 

It only reads the values from 1 sample. If I set the "sample size" to for example "51 by 51 average" the script completely ignores that.

 

Can I add settings for the Color Sampler Script to read an average sample value?

 

My workaround would be to set 3 color samplers manually and take the average but still that's not very handy and accurate.

This topic has been closed for replies.
Correct answer jazz-y
quote

maybe the @r-bin will help.

 

By @jazz-y

 

Привет, Дима. Слушай, мне некогда корячиться с переводчиком. Расталкуй товарищу как работает эта функция https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-color-from-selected-area/m-p/12113603#M553549

 

Там нужно заменить x+1 и y+1 на x+51 и y+51, ну или по желанию.


Спасибо! Я уже накидал свой вариант, но, конечно, в более сокращенном виде 🙂

This code is only suitable for RGB:

 

const AVERAGE_AREA = 51;
var startRulerUnits = app.preferences.rulerUnits,
    startTypeUnits = app.preferences.typeUnits;

app.preferences.rulerUnits = Units.PIXELS
app.preferences.typeUnits = TypeUnits.PIXELS

var doc = activeDocument,
    colorSampler = activeDocument.colorSamplers[0],

    x = colorSampler.position[0].value,
    y = colorSampler.position[1].value;

var selRegion = [
    [x - AVERAGE_AREA / 2, y - AVERAGE_AREA / 2],
    [x + AVERAGE_AREA / 2, y - AVERAGE_AREA / 2],
    [x + AVERAGE_AREA / 2, y + AVERAGE_AREA / 2],
    [x - AVERAGE_AREA / 2, y + AVERAGE_AREA / 2]
]

doc.selection.select(selRegion)
var mean= [],
    color = new SolidColor;

for (var i = 0; i < doc.channels.length; i++) {
    var n = p = 0,
        cur = doc.channels[i].histogram;
    for (var x = 0; x < cur.length; x++) {
        n += cur[x]
        p += cur[x] * x
    }
    mean.push(p / n)
}
doc.selection.deselect()

with (color.rgb) { red = mean[0]; green = mean[1]; blue = mean[2]; };

var readColorHue = Math.round(color.hsb.hue)
var readColorSaturation = Math.round(color.hsb.saturation)
var readColorBrightness = Math.round(color.hsb.brightness)

alert('H ' + readColorHue + ' S ' + readColorSaturation + ' B ' + readColorBrightness)

app.preferences.rulerUnits = startRulerUnits
app.preferences.typeUnits = startTypeUnits

 

@r-bin proposed a similar solution to this problem, but it is more complete and faster. At the link above you will find a function that changes app.foregroundColor to the color at the given point. You can get the coordinates of this point from the .position property of the colorSampler. After that, you need to create a selection by sequentially describing the coordinates of the selection points (note that @r-bin shifts the coordinates to the right down from the desired point by 1 pixel, you need to rewrite the code so that the point you need is in the center of the selection). Further, everything is simple - after executing the function, you just need to read the color value of app.foregroundColor

1 reply

Stephen Marsh
Community Expert
Community Expert
March 29, 2023

I'm not in front of a computer, however, my first thought is to create a selection at the required size, then blur/average, read the sample and undo the blur (or copy the pixels to a new layer, blur/average, read and delete the layer).


Perhaps AM code can use the average sample size.

Legend
March 29, 2023

No. AM code also shows 1x1 pixel color value

 

A workaround is needed here. The problem is that blur filters includes pixels outside the selection for averaging. Perhaps the Custom filter would help here, but I don’t know how to use matrices at all, maybe the @r-bin will help.

 

The simplest thing that comes to mind is to find the point where the Color Sampler is set, create a selection of the desired size, read and average the information about the colors of the pixels from the histogram. However, I am also not at the computer right now to check how the result will match the information panel.

 
 

 

 

Legend
March 29, 2023
quote

maybe the @r-bin will help.

 

By @jazz-y

 

Привет, Дима. Слушай, мне некогда корячиться с переводчиком. Расталкуй товарищу как работает эта функция https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-color-from-selected-area/m-p/12113603#M553549

 

Там нужно заменить x+1 и y+1 на x+51 и y+51, ну или по желанию.