// Save the current preferences
var startRulerUnits = app.preferences.rulerUnits
var startDisplayDialogs = app.displayDialogs
var docRef = app.activeDocument;
var activeChannels = docRef.activeChannels;
// Set Adobe Photoshop CS2 to use pixels and display no dialogs
app.preferences.rulerUnits = Units.PIXELS
app.displayDialogs = DialogModes.NO
// prompt("Define XY in pixels", 0, "Get Color Sample");
var x = 15;
var y = 15;
var cInput = new Array();
var cOutput = 0;
// collect the pixel values in all channels
selectBounds(docRef, [x, y, x+1, y+1]);
for (var i = 0; i < docRef.channels.length; i++) {
docRef.channels[i].visible = true;
// turn OFF all channels except current
for (var j = 0; j < docRef.channels.length; j++) {
if (i != j) {docRef.channels[j].visible = false;}
}
cInput[i] = findPV(docRef.channels[i].histogram);
}
docRef.selection.deselect();
docRef.activeChannels = activeChannels;
// get the average value (brightness)
for (var i = 0; i < cInput.length; i++) {
cOutput += (cInput[i] / cInput.length);
}
// adjust per channel
for (var i = 0; i < cInput.length; i++) {
docRef.channels[i].visible = true;
// turn OFF all channels except current
for (var j = 0; j < docRef.channels.length; j++) {
if (i != j) {docRef.channels[j].visible = false;}
}
docRef.activeChannels = Array(docRef.channels[i]);
var cPoints = new Array(Array(0,0), Array(cInput[i], cOutput), Array(255,255));
docRef.adjustLayer.adjustCurves(cPoints);
}
// Reset the application preferences
app.preferences.rulerUnits = startRulerUnits
app.displayDialogs = startDisplayDialogs
docRef.activeChannels = activeChannels;
function selectBounds(doc, b) {
doc.selection.select([[ b[0], b[1] ],
[ b[2], b[1] ],
[ b[2], b[3] ],
[ b[0], b[3] ]]);
}
function findPV(h) {
for (var i = 0; i <= 255; i++ ) {
if (h[i]) { return i; }
}
return 0;
}