Color Sampler average color

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Tom, Kukurykus

jugenjury

Color Sampler average color

Post by jugenjury »

The Color Sampler tool in Photoshop is nice when you need to find the color of a spot or area in an image. It allows for a point sample as well as various area sizes to be averaged. Unfortunately, when using the ColorSampler properties in a script, you are only able to return the color value of a single point and not an average of an area.

Thanks to Mike Hale for helping me with a few key elements of this function that I use quite often. It allows you to return an average color for the area given centered on the set Color Sampler point on your document.

Code: Select allfunction getAverageColor(s,A){
   doc=app.activeDocument;
   if (!s) { s=1;}
   if (!A) {A=3;}
   CP=doc.colorSamplers;
   s=s-1;
   sampleSize=A;
   r=((A-1)/2);
   x=Math.round(CP[s].position[0]-r);
   y=Math.round(CP[s].position[1]-r);
   doc.selection.select([[x, y], [x+sampleSize, y], [x+sampleSize, y+sampleSize], [x, y+sampleSize]], SelectionType.REPLACE, 0, false);
   doc.activeLayer.applyAverage();
   var sColor = new SolidColor();
   sColor=CP[s].color;
   executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );
   doc.selection.deselect();
   return sColor;
}

Usage: averageColor=getAverageColor([ColorPicker],[Area]);

Where: ColorPicker is the # of the Color Selection made in the document (1-4) and Area is the length of one side of an imaginary square which determines the area to be averaged.

Note: averageColor must be set to a new SolidColor()