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
... View more