Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Color Sampler script part 2

Engaged ,
Oct 20, 2009 Oct 20, 2009

OK. So I'm back to this. Here is what I'm having trouble figuring out now so any help would of course be appreciated.

When I use the colorSamplers scripting I am able to obtain information about a single point. However, when setting the sample points there is the option to select single point, 3 by 3 average, 5 by 5 average, 11 by 11, etc. and the average number is what shows in the Info panel. But, the script still returns the single point information and not the average. Is there any way to obtain this information using a script other than stepping through each point with the colorSamplers[0].move() and doing the math in the script to get the average? This is how I'm doing it now, but for large areas it can get slow.

Any advice?

TOPICS
Actions and scripting
4.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Oct 21, 2009 Oct 21, 2009

No, I don't mind at all. You might want to replace your scriptlistner deselect function with the DOM method just to make the code a little tighter.

activeDocument.selection.deselect();

Translate
Adobe
Guru ,
Oct 20, 2009 Oct 20, 2009

If taking multi samples are too slow you can do something like this to get the average. Note this is for RGB images

// with ruler set to pixels
var x = 100;// top left corner
var y = 100;// of sample
var sampleSize = 11;// sample size
activeDocument.selection.select([[x, y], [x+sampleSize, y], [x+sampleSize, y+sampleSize], [x, y+sampleSize]], SelectionType.REPLACE, 0, false);
activeDocument.activeLayer.applyAverage();
var re = RegExp( '[123456789]' );
var sColor = new SolidColor();
sColor.rgb.red = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
sColor.rgb.green = re.exec( activeDocument.channels[1].histogram.toString() ).index/2;
sColor.rgb.blue = re.exec( activeDocument.channels[2].histogram.toString() ).index/2;
executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );
alert( sColor.rgb.hexValue );

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Oct 20, 2009 Oct 20, 2009

I turned this into a function that works with gray, rgb, lab, or cmyk images. If the doc mode is not one of those listed the function does not return a color. There is no doc bounds check and at least with CS4 if try to make a selection off canvas the returned color is for the whole doc. Still expects the ruler set to pixels

function getSelectionColor( x, y, sampleSize ){
     try{
          x = x - ( sampleSize / 2 );
          y = y - ( sampleSize / 2 );
          activeDocument.selection.select([[x, y], [x+sampleSize, y], [x+sampleSize, y+sampleSize], [x, y+sampleSize]], SelectionType.REPLACE, 0, false);
          activeDocument.activeLayer.applyAverage();
          var re = RegExp( '[123456789]' );
          var sColor = new SolidColor();
          if ( activeDocument.mode == DocumentMode.GRAYSCALE ) {
              var gv = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
              sColor.gray.gray = 100 * (gv/255);
          }
          if ( activeDocument.mode == DocumentMode.RGB ) {
              sColor.rgb.red = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
              sColor.rgb.green = re.exec( activeDocument.channels[1].histogram.toString() ).index/2;
              sColor.rgb.blue = re.exec( activeDocument.channels[2].histogram.toString() ).index/2;
          }
          if ( activeDocument.mode == DocumentMode.LAB ) {
              var lv = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
              sColor.lab.l = 100 * ( lv/255 );
              sColor.lab.a = ( re.exec( activeDocument.channels[1].histogram.toString() ).index/2 ) - 128;
              sColor.lab.b = ( re.exec( activeDocument.channels[2].histogram.toString() ).index/2 ) -128;
          }
          if ( activeDocument.mode == DocumentMode.CMYK ) {
              var cv = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
              sColor.cmyk.cyan = 100 * (1-(cv/255));
              cv = re.exec(activeDocument.channels[1].histogram.toString() ).index/2;
              sColor.cmyk.magenta = 100 * (1-(cv/255));
              cv = re.exec(activeDocument.channels[2].histogram.toString() ).index/2;
              sColor.cmyk.yellow = 100* (1-(cv/255));
              cv = re.exec(activeDocument.channels[3].histogram.toString() ).index/2;
              sColor.cmyk.black = 100 * (1-(cv/255));
          }
          executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );
          return sColor;
     }catch(e){}
}
var c = getSelectionColor( -12, -12, 11 );// no selection is made and the whole doc is sampled

var c = getSelectionColor( 100, 100, 3 );// selection centered at 56,56- odd sample sizes one pixel off centred

var c = getSelectionColor( 500,500, 4 );// selection sentered at 250,250
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 21, 2009 Oct 21, 2009

I hope you don't mind, Michael, but I modified your script a little bit to use the Color Sampler tool to set the spots. This makes it easier for me as I can see what the average is supposed to be in the info panel before running the script. This also allows me to set multiple points to compare the average sample from each in order to make color corrections between the 2 (or more) areas.

Here is what I have....

// A is the area you want sampled. s is the number of the Color Picker spot you set.
function deselect() {
    var dslct = new ActionDescriptor();
    var slctn = new ActionReference();
    slctn.putProperty(charIDToTypeID('Chnl'), stringIDToTypeID('selection'));
    dslct.putReference(stringIDToTypeID('null'), slctn);
    dslct.putEnumerated(charIDToTypeID('T   '), charIDToTypeID('Ordn'), charIDToTypeID('None'));
    executeAction(charIDToTypeID('setd'), dslct,  DialogModes.NO);
}

function getSelectionColor( s,A ){
    try{
        if (!s) { s=1;}
        if (!A) {A=1;}
        CP=app.activeDocument.colorSamplers;
        s=s-1;
        sampleSize=A;
        r=((A-1)/2);
        x=Math.round(CP.position[0]-r);
        y=Math.round(CP.position[1]-r);
        activeDocument.selection.select([[x, y], [x+sampleSize, y], [x+sampleSize, y+sampleSize], [x, y+sampleSize]], SelectionType.REPLACE, 0, false);
        activeDocument.activeLayer.applyAverage();
        var re = RegExp( '[123456789]' );
        var sColor = new SolidColor();
        if ( activeDocument.mode == DocumentMode.GRAYSCALE ) {
            var gv = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
            sColor.gray.gray = 100 * (gv/255);
        }
        if ( activeDocument.mode == DocumentMode.RGB ) {
            sColor.rgb.red = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
            sColor.rgb.green = re.exec( activeDocument.channels[1].histogram.toString() ).index/2;
            sColor.rgb.blue = re.exec( activeDocument.channels[2].histogram.toString() ).index/2;
        }
        if ( activeDocument.mode == DocumentMode.LAB ) {
            var lv = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
            sColor.lab.l = 100 * ( lv/255 );
            sColor.lab.a = ( re.exec( activeDocument.channels[1].histogram.toString() ).index/2 ) - 128;
            sColor.lab.b = ( re.exec( activeDocument.channels[2].histogram.toString() ).index/2 ) -128;
        }
        if ( activeDocument.mode == DocumentMode.CMYK ) {
            var cv = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
            sColor.cmyk.cyan = 100 * (1-(cv/255));
            cv = re.exec(activeDocument.channels[1].histogram.toString() ).index/2;
            sColor.cmyk.magenta = 100 * (1-(cv/255));
            cv = re.exec(activeDocument.channels[2].histogram.toString() ).index/2;
            sColor.cmyk.yellow = 100* (1-(cv/255));
            cv = re.exec(activeDocument.channels[3].histogram.toString() ).index/2;
            sColor.cmyk.black = 100 * (1-(cv/255));
        }
        executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );
        deselect();
        return sColor;
    }catch(e){}
}
var sampleColor=new SolidColor();
sampleColor=getSelectionColor(1,5);
alert(sampleColor.rgb.red+" "+sampleColor.rgb.green+" "+sampleColor.rgb.blue);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Oct 21, 2009 Oct 21, 2009

No, I don't mind at all. You might want to replace your scriptlistner deselect function with the DOM method just to make the code a little tighter.

activeDocument.selection.deselect();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 21, 2009 Oct 21, 2009

I proudly display by ID10T badge. Thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Oct 21, 2009 Oct 21, 2009

There is nothing really wrong with using scriptlistner code. Sometimes that is the only way to get something done. Some times it's better to use sl code even when there is a DOM version because it's faster.

It's just in this case the DOM version is shorter, more readable, and as fast the the sl version.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 10, 2009 Dec 10, 2009

I seem to be having a problem with this function, though I'm not sure whether it's the function itself or something else. I keep getting errors when I call this function from anywhere. I pulled this function out and placed it into a script by itself and I'm getting the same problem. I cut out a lot of it until there is really nothing left to the function itself, but I still can't see the problem. Here's the separate script as I have it now:

var startRulerUnits = app.preferences.rulerUnits;
var startTypeUnits = app.preferences.typeUnits ;
var startDisplayDialogs = app.displayDialogs;

app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
app.displayDialogs = DialogModes.NO;

var doc = app.doc;


function getSelectionColor(s,A){
    alert(doc.colorSamplers[0].color.rgb.red);
    return doc.colorSamplers[0].color;
}
var sampleColor=new SolidColor();
sampleColor=getSelectionColor(1,5);
alert(sampleColor.rgb.red+" "+sampleColor.rgb.green+" "+sampleColor.rgb.blue);

app.preferences.rulerUnits = startRulerUnits;
app.preferences.typeUnits = startTypeUnits;
app.displayDialogs = startDisplayDialogs;

When I run it, I get Error 21: undefined is not an object  Line: 13->alert(doc.colorSamplers[0].color.rgb.red);

If I comment out line 13, I get the same error for line 14

This function worked great originally and as I was working on other portions of the main project, I wasn't using this function. I got to the point where I wanted to put it back into the project so I did and now this is what happens.

The above script is all that is in that test script file. There is nothing above or below this portion that isn't commented out.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Dec 10, 2009 Dec 10, 2009

It's not clear what the function's arguments are but it will fail if there are no colorsamplers in the doc

function getSelectionColor(s,A){// what are 's' and 'A'?

// will fail it there is no colorSmaplers

if(doc.colorSamplers.length > 0){

alert(doc.colorSamplers[0].color.rgb.red);

return doc.colorSamplers[0].color;

}else{

return undefined;

}

}

var doc = activeDocument;

alert(getSelectionColor())

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 10, 2009 Dec 10, 2009

I feel like an idiot. I had "var doc=app.doc" instead of "var doc=app.activeDocument" I guess that happened when I did a find/replace. GRRRR.

But, I think I'm still having trouble with the script as I posted above back in October. The variation on your script.

Gimme a bit to play with it now that I got that worked out.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 10, 2009 Dec 10, 2009

OK. Here's the problem now... The script as follows is from your original script above, Michael, with the modification I made to use a color sampler spot to set the selection area to average. s is the color picker # to from which to reference. A is the area to use for the average.

var startRulerUnits = app.preferences.rulerUnits;
var startTypeUnits = app.preferences.typeUnits ;
var startDisplayDialogs = app.displayDialogs;

app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
app.displayDialogs = DialogModes.NO;

var doc = app.activeDocument;

function getSelectionColor(){
//    try{
//        if (!s) { s=1;}
//        if (!A) {A=3;}
        s=1;
        A=5;
        if (doc.selection.bounds[3]>0) A=(doc.selection.bounds[3]-doc.selection.bounds[0])/2;
        CP=doc.colorSamplers;
        s=s-1;
        sampleSize=A;
        r=((A-1)/2);
        x=Math.round(CP.position[0]-r);
        y=Math.round(CP.position[1]-r);
        activeDocument.selection.select([[x, y], [x+sampleSize, y], [x+sampleSize, y+sampleSize], [x, y+sampleSize]], SelectionType.REPLACE, 0, false);
        activeDocument.activeLayer.applyAverage();
        var re = RegExp( '[123456789]' );
        var sColor = new SolidColor();
        if ( activeDocument.mode == DocumentMode.GRAYSCALE ) {
            var gv = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
            sColor.gray.gray = 100 * (gv/255);
        }
        if ( activeDocument.mode == DocumentMode.RGB ) {
            sColor.rgb.red = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
            sColor.rgb.green = re.exec( activeDocument.channels[1].histogram.toString() ).index/2;
            sColor.rgb.blue = re.exec( activeDocument.channels[2].histogram.toString() ).index/2;
        }
        if ( activeDocument.mode == DocumentMode.LAB ) {
            var lv = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
            sColor.lab.l = 100 * ( lv/255 );
            sColor.lab.a = ( re.exec( activeDocument.channels[1].histogram.toString() ).index/2 ) - 128;
            sColor.lab.b = ( re.exec( activeDocument.channels[2].histogram.toString() ).index/2 ) -128;
        }
        if ( activeDocument.mode == DocumentMode.CMYK ) {
            var cv = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
            sColor.cmyk.cyan = 100 * (1-(cv/255));
            cv = re.exec(activeDocument.channels[1].histogram.toString() ).index/2;
            sColor.cmyk.magenta = 100 * (1-(cv/255));
            cv = re.exec(activeDocument.channels[2].histogram.toString() ).index/2;
            sColor.cmyk.yellow = 100* (1-(cv/255));
            cv = re.exec(activeDocument.channels[3].histogram.toString() ).index/2;
            sColor.cmyk.black = 100 * (1-(cv/255));
        }
        executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );
        activeDocument.selection.deselect();
        return sColor;
//    }catch(e){}
}

var sampleColor=new SolidColor();
sampleColor=getSelectionColor();
alert(sampleColor.rgb.red+" "+sampleColor.rgb.green+" "+sampleColor.rgb.blue);

I was getting nothing returned so I commented out the try and catch lines. Now the error I get is "Error 1302: No such element. Line: 16-> A=5;" As shown, I also remeoved the arguments passed to "getSelectionColor()"

If I had hair, I'd be ripping it out. This worked great before and I didn't change anything in it from then until now. I just commented out the lines that called the function and put in other lines that grabbed the colors directly from the color picker. Now I commented out the new lines and uncommented the old lines.

It's probably something so simple, too.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 10, 2009 Dec 10, 2009

I commented out the line

//        if (doc.selection.bounds[3]>0) A=(doc.selection.bounds[3]-doc.selection.bounds[0])/2;

and now it works.

I just don't understand why this would mess up the rest of it. At least I narrowed it down to that.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Dec 10, 2009 Dec 10, 2009

If you are sure the doc has the colorSamplers then I think the problem is with the activelayer. The function has the line activeDocument.activeLayer.applyAverage();

If there are no pixels in the selection that line will fail and because it's in a try block the error is not reported nor is sColor defined.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Dec 10, 2009 Dec 10, 2009

It still doesn't look right, as can't see where you are adding the colorSampler?

IE: if you add this line doc.colorSamplers.removeAll(); the script will fail.

Also the line you have commented, you are subtracting the left of the selection from the bottom of the selection?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 10, 2009 Dec 10, 2009

Michael: I'm running this with only a Background layer in the stack to test it.

Paul: The color sampler points are added manually before the script is run. And yes, I put in the wrong array elements for the bounds of the selection. After all these years, I still slip when it comes to arrays sometimes. Gotta remind myself they start with 0 and not 1.


Thank you both.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Dec 10, 2009 Dec 10, 2009

When I asked what was 's' and 'A' I didn't notice the function in the top of the post. If I use that function with a single layer background doc it works for me.( if there is a sampler )

var startRulerUnits = app.preferences.rulerUnits;
var startTypeUnits = app.preferences.typeUnits ;
var startDisplayDialogs = app.displayDialogs;

app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
app.displayDialogs = DialogModes.NO;

var doc = app.activeDocument;

function getSelectionColor( s,A ){
    try{
        if (!s) { s=1;}
        if (!A) {A=1;}
        CP=app.activeDocument.colorSamplers;
          if( CP.length == 0 ) return -1;
        s=s-1;
        sampleSize=A;
        r=((A-1)/2);
        x=Math.round(CP.position[0]-r);
        y=Math.round(CP.position[1]-r);
        activeDocument.selection.select([[x, y], [x+sampleSize, y], [x+sampleSize, y+sampleSize], [x, y+sampleSize]], SelectionType.REPLACE, 0, false);
        activeDocument.activeLayer.applyAverage();
        var re = RegExp( '[123456789]' );
        var sColor = new SolidColor();
        if ( activeDocument.mode == DocumentMode.GRAYSCALE ) {
            var gv = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
            sColor.gray.gray = 100 * (gv/255);
        }
        if ( activeDocument.mode == DocumentMode.RGB ) {
            sColor.rgb.red = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
            sColor.rgb.green = re.exec( activeDocument.channels[1].histogram.toString() ).index/2;
            sColor.rgb.blue = re.exec( activeDocument.channels[2].histogram.toString() ).index/2;
        }
        if ( activeDocument.mode == DocumentMode.LAB ) {
            var lv = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
            sColor.lab.l = 100 * ( lv/255 );
            sColor.lab.a = ( re.exec( activeDocument.channels[1].histogram.toString() ).index/2 ) - 128;
            sColor.lab.b = ( re.exec( activeDocument.channels[2].histogram.toString() ).index/2 ) -128;
        }
        if ( activeDocument.mode == DocumentMode.CMYK ) {
            var cv = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
            sColor.cmyk.cyan = 100 * (1-(cv/255));
            cv = re.exec(activeDocument.channels[1].histogram.toString() ).index/2;
            sColor.cmyk.magenta = 100 * (1-(cv/255));
            cv = re.exec(activeDocument.channels[2].histogram.toString() ).index/2;
            sColor.cmyk.yellow = 100* (1-(cv/255));
            cv = re.exec(activeDocument.channels[3].histogram.toString() ).index/2;
            sColor.cmyk.black = 100 * (1-(cv/255));
        }
        executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );
        //deselect();
        return sColor;
    }catch(e){}
}

var sampleColor=getSelectionColor(1,5);
alert(sampleColor.rgb.red+" "+sampleColor.rgb.green+" "+sampleColor.rgb.blue);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 10, 2009 Dec 10, 2009
LATEST

OK. For anyone who has been following along here, I got it working with Michael's and Paul's help so I thought it only fitting to share this snippet of code so if anyone finds it useful, they can use it.

// This script requires at least one Color Picker selection to be active.
// That Color Picker number is set in the variable "cp".

var cp=1; // Number of the Color Picker from the Info Panel. Can be 1-4
var Avg=0; // Set to the size of the area you want to average if no selection is made. 0 to not use.

var startRulerUnits = app.preferences.rulerUnits;
var startTypeUnits = app.preferences.typeUnits ;
var startDisplayDialogs = app.displayDialogs;

app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
app.displayDialogs = DialogModes.NO;

var doc = app.activeDocument;

// Is a selection active?
function hasSelection(doc) {
    if(doc == undefined) doc = activeDocument;
    var res = false;
    var as = doc.activeHistoryState;
    doc.selection.deselect();
    if (as != doc.activeHistoryState) {
        res = true;
        doc.activeHistoryState = as;
    }
    return res;
}

// Average the colors if needed
function getSelectionColor(s,A){
        if (!s) { s=1;}
        if (!A) {A=1;}
        CP=doc.colorSamplers;
        s=s-1;
        sampleSize=A;
        r=((A-1)/2);
        x=Math.round(CP.position[0]-r);
        y=Math.round(CP.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.color;
        executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );
        doc.selection.deselect();
        return sColor;
}

// Check to see if a selection was made. If so, we want to average
//  the color for that size area but centered on the Color Picker spot.
// Or, alternately, set Avg (above) to a specific value you wish to use in case of no selection.
// If not, just use the color from the Color Picker itself.
if (hasSelection(doc)) {
    sel=doc.selection.bounds;
    Sel=new Array([sel[0],sel[1]],[sel[2],sel[1]],[sel[2],sel[3]],[sel[0],sel[3]]);
    Area=parseInt(((sel[3]-sel[1]+sel[2]-sel[0])/2));
    var sampleColor=new SolidColor();
    sampleColor=getSelectionColor(cp,Area);
}
else if (Avg>1) {
    Area=Avg;
    var sampleColor=new SolidColor();
    sampleColor=getSelectionColor(cp,Area);
}
else {
    sel=false;
    var sampleColor=new SolidColor();
    sampleColor=doc.colorSamplers[cp-1].color;
}

// Reselect original selection in case we want to re-run this with the same average area.
if (sel) doc.selection.select(Sel);

// Output.
alert(sampleColor.rgb.red+" "+sampleColor.rgb.green+" "+sampleColor.rgb.blue);

app.preferences.rulerUnits = startRulerUnits;
app.preferences.typeUnits = startTypeUnits;
app.displayDialogs = startDisplayDialogs;

What it does: You select a spot with the color picker tool. If you only want the color of that spot, run the script. If you want an average color of that spot and surrounding area, set a selection to the size of the area you wish to average. If you want to use a specific size area to average if no selection is made, set Avg equal to the length (in pixels) of one side of the square determining your area.

This may not seem like much, but if you need to do a lot of color comparisons, this can iterate through multiple Color Picker spots (up to the 4 maximum) to compare colors. You can then use that output within your script for whatever you see fit. This can also be set up as a function to use within your script. Just be sure to pass the appropriate parameters (cp and Avg if used).

A big thanks to Michael and Paul.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines