Skip to main content
Known Participant
March 3, 2025
Answered

set dark, gray, light points on curve from available color sample at specific coordinates by script

  • March 3, 2025
  • 1 reply
  • 300 views

Hi everyone,

i want to create photoshop script to set black point, white point, gray point on the curve from available color sample at specific position on image.

 

set "black point" of curve from color sample at position x=1000, y=40
set "gray point" of curve from color sample at position x=1000, y=500
set "white point" of curve from color sample at position x=1000, y=960

 

i tried a program but it doesn't seem to work. my photoshop version is 25.12

please guide me, I appreciate your help.

 

 

 

 

#target photoshop
function setCurvePoints() {
    var doc = app.activeDocument;

    // Get color sample values at specified positions
    var blackPointSample = getColorSample(1000, 40);
    var grayPointSample = getColorSample(1000, 500);
    var whitePointSample = getColorSample(1000, 960);
    

    // Create a new Curves adjustment layer
    var idCurves = stringIDToTypeID("Curves");
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putClass(idCurves);
    desc.putReference(charIDToTypeID("null"), ref);
    executeAction(charIDToTypeID("Mk  "), desc, DialogModes.NO);

    var adjustmentLayer = doc.activeLayer;

    // Set the black point
    setCurvePoint(adjustmentLayer, blackPointSample, [0, 0]);

    // Set the white point
    setCurvePoint(adjustmentLayer, whitePointSample, [255, 255]);

    // Set the gray point
    setCurvePoint(adjustmentLayer, grayPointSample, [128, 128]);

    alert("Curves adjustment points have been set.");
}

// Get color sample at specified position
function getColorSample(x, y) {
    var samplePoint = [x, y];
    var colorSampler = app.activeDocument.colorSamplers.add(samplePoint);
    var colorSample = colorSampler.color.rgb;
    colorSampler.remove();
    return colorSample;
}

// Set a point on the Curves adjustment layer
function setCurvePoint(layer, colorSample, targetPoint) {
    try {
        var idCurves = stringIDToTypeID("curves");
        var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated(charIDToTypeID("AdjL"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
        desc.putReference(charIDToTypeID("null"), ref);

        var list = new ActionList();
        var curvePoint = new ActionDescriptor();
        curvePoint.putEnumerated(charIDToTypeID("Chnl"), charIDToTypeID("Chnl"), charIDToTypeID("RGB "));
        curvePoint.putDouble(charIDToTypeID("Icnt"), targetPoint[0]);
        curvePoint.putDouble(charIDToTypeID("Otpt"), targetPoint[1]);
        list.putObject(idCurves, curvePoint);
        desc.putList(charIDToTypeID("Adjs"), list);

        executeAction(idCurves, desc, DialogModes.NO);
    } catch (e) {
        alert("Error setting curve point: " + e.message);
    }
}

setCurvePoints();

 

 

 

 

Correct answer c.pfaffenbichler

 

// create curves layer with white, gray and black determined by three color samplers;
// 2025, use it at your own risk;
// call the function to run the script;
createCurveAdjustmetFromColorSamplers();
// create a function fo hold most of the code;
function createCurveAdjustmetFromColorSamplers(){
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var myDocument = activeDocument;
// needs an open document in a color mode that supports layers;
if(app.documents.length == 0 || ( app.activeDocument.mode != DocumentMode.RGB ) ){    
alert('This script requires a document in RGB');
return;
}
// color sample;
var outputArray = new Array();
var theSamples = [[1000,40], [1000, 500], [1000, 960]];
// collect the samples;
for(var sampleIndex = 0; sampleIndex < theSamples.length; sampleIndex++ ){
var theSampler = myDocument.colorSamplers.add(theSamples[sampleIndex]);
outputArray.push(theSampler.color);
theSampler.remove()
}
// create arrays of the color values:
var theArray = [[], [], []];
for (var m = 0; m < outputArray.length; m++) {
theArray[0].push(Math.round(outputArray[m].rgb.red));
theArray[1].push(Math.round(outputArray[m].rgb.green));
theArray[2].push(Math.round(outputArray[m].rgb.blue));
};
// create the curves layer;
rgbCurvesLayer ([theArray[0], [0, 128, 255], theArray[1], [0, 128, 255], theArray[2], [0, 128, 255]]);
app.preferences.rulerUnits = originalRulerUnits;
};
////// make rgb curves layer //////
function rgbCurvesLayer (theArray) {
var idpresetKind = stringIDToTypeID( "presetKind" );
var idpresetKindType = stringIDToTypeID( "presetKindType" );
var idChnl = charIDToTypeID( "Chnl" );
var idCrv = charIDToTypeID( "Crv " );
var idCrvA = charIDToTypeID( "CrvA" );
var idAdjL = charIDToTypeID( "AdjL" );
// =======================================================
    var desc5 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref2 = new ActionReference();
        ref2.putClass( idAdjL );
    desc5.putReference( idnull, ref2 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc6 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc7 = new ActionDescriptor();
            desc7.putEnumerated( idpresetKind, idpresetKindType, stringIDToTypeID( "presetKindDefault" ) );
        desc6.putObject( idType, charIDToTypeID( "Crvs" ), desc7 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc5.putObject( idUsng, idAdjL, desc6 );
executeAction( charIDToTypeID( "Mk  " ), desc5, DialogModes.NO );
// =======================================================
    var desc8 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref3 = new ActionReference();
        ref3.putEnumerated( idAdjL, charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
    desc8.putReference( idnull, ref3 );
    var idT = charIDToTypeID( "T   " );
        var desc9 = new ActionDescriptor();
        desc9.putEnumerated( idpresetKind, idpresetKindType, stringIDToTypeID( "presetKindCustom" ) );
        var idAdjs = charIDToTypeID( "Adjs" );
            var list1 = new ActionList();
                var desc10 = new ActionDescriptor();
                    var ref4 = new ActionReference();
                    ref4.putEnumerated( idChnl, idChnl, charIDToTypeID( "Rd  " ) );
                desc10.putReference( idChnl, ref4 );
                    var list2 = new ActionList();
// add r points;
for (var m = 0; m < theArray[0].length; m++) {
    addCurvePoint (list2, theArray[0][m], theArray[1][m])
    };	
                desc10.putList( idCrv, list2 );
            list1.putObject( idCrvA, desc10 );
                var desc15 = new ActionDescriptor();
                    var ref5 = new ActionReference();
                    ref5.putEnumerated( idChnl, idChnl, charIDToTypeID( "Grn " ) );
                desc15.putReference( idChnl, ref5 );
                    var list3 = new ActionList();
// add g points;
for (var m = 0; m < theArray[2].length; m++) {
    addCurvePoint (list3, theArray[2][m], theArray[3][m])
    };
                desc15.putList( idCrv, list3 );
            list1.putObject( idCrvA, desc15 );
                var desc20 = new ActionDescriptor();
                    var ref6 = new ActionReference();
                    ref6.putEnumerated( idChnl, idChnl, charIDToTypeID( "Bl  " ) );
                desc20.putReference( idChnl, ref6 );
                    var list4 = new ActionList();
// add b points;
for (var m = 0; m < theArray[4].length; m++) {
    addCurvePoint (list4, theArray[4][m], theArray[5][m])
    };
                desc20.putList( idCrv, list4 );
            list1.putObject( idCrvA, desc20 );
        desc9.putList( idAdjs, list1 );
    desc8.putObject( idT, charIDToTypeID( "Crvs" ), desc9 );
executeAction( charIDToTypeID( "setd" ), desc8, DialogModes.NO );
//
return app.activeDocument.activeLayer;
////// add curve point //////
function addCurvePoint (theList, valueHor, valueVer) {
var desc11 = new ActionDescriptor();
desc11.putDouble( charIDToTypeID( "Hrzn" ), valueHor );
desc11.putDouble( charIDToTypeID( "Vrtc" ), valueVer );
theList.putObject( charIDToTypeID( "Pnt " ), desc11 );
};
};

 

1 reply

c.pfaffenbichler
Community Expert
Community Expert
March 4, 2025

Where did you get that code? 

 

Edit: This would work with three Color Samplers. 

 

// 2025, use it at your own risk;
// call the function to run the script;
createCurveAdjustmetFromColorSamplers();
// create a function fo hold most of the code;
function createCurveAdjustmetFromColorSamplers(){
// needs an open document in a color mode that supports layers;
if(app.documents.length == 0 || ( app.activeDocument.mode != DocumentMode.RGB ) ){    
alert('This script requires a document in RGB');
return;
}
// check for three colorSamplers
if(app.activeDocument.colorSamplers.length != 3 ){
alert('This script requires three colorSamplers.');
return;
};
// for the bottom layer
var outputArray = new Array();
// store the number of samples because it will be needed in more than one place
var numberOfSamples = app.activeDocument.colorSamplers.length;
// collect the samples from the bottom layer
for(var sampleIndex = 0; sampleIndex < numberOfSamples; sampleIndex++ ){
outputArray.push(app.activeDocument.colorSamplers[sampleIndex].color);
}
// create arrays of the color values:
var theArray = [[], [], []];
for (var m = 0; m < outputArray.length; m++) {
theArray[0].push(Math.round(outputArray[m].rgb.red));
theArray[1].push(Math.round(outputArray[m].rgb.green));
theArray[2].push(Math.round(outputArray[m].rgb.blue));
};
// create the curves layer;
rgbCurvesLayer ([theArray[0], [0, 128, 255], theArray[1], [0, 128, 255], theArray[2], [0, 128, 255]])
};
////// make rgb curves layer //////
function rgbCurvesLayer (theArray) {
var idpresetKind = stringIDToTypeID( "presetKind" );
var idpresetKindType = stringIDToTypeID( "presetKindType" );
var idChnl = charIDToTypeID( "Chnl" );
var idCrv = charIDToTypeID( "Crv " );
var idCrvA = charIDToTypeID( "CrvA" );
var idAdjL = charIDToTypeID( "AdjL" );
// =======================================================
    var desc5 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref2 = new ActionReference();
        ref2.putClass( idAdjL );
    desc5.putReference( idnull, ref2 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc6 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc7 = new ActionDescriptor();
            desc7.putEnumerated( idpresetKind, idpresetKindType, stringIDToTypeID( "presetKindDefault" ) );
        desc6.putObject( idType, charIDToTypeID( "Crvs" ), desc7 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc5.putObject( idUsng, idAdjL, desc6 );
executeAction( charIDToTypeID( "Mk  " ), desc5, DialogModes.NO );
// =======================================================
    var desc8 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref3 = new ActionReference();
        ref3.putEnumerated( idAdjL, charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
    desc8.putReference( idnull, ref3 );
    var idT = charIDToTypeID( "T   " );
        var desc9 = new ActionDescriptor();
        desc9.putEnumerated( idpresetKind, idpresetKindType, stringIDToTypeID( "presetKindCustom" ) );
        var idAdjs = charIDToTypeID( "Adjs" );
            var list1 = new ActionList();
                var desc10 = new ActionDescriptor();
                    var ref4 = new ActionReference();
                    ref4.putEnumerated( idChnl, idChnl, charIDToTypeID( "Rd  " ) );
                desc10.putReference( idChnl, ref4 );
                    var list2 = new ActionList();
// add r points;
for (var m = 0; m < theArray[0].length; m++) {
    addCurvePoint (list2, theArray[0][m], theArray[1][m])
    };	
                desc10.putList( idCrv, list2 );
            list1.putObject( idCrvA, desc10 );
                var desc15 = new ActionDescriptor();
                    var ref5 = new ActionReference();
                    ref5.putEnumerated( idChnl, idChnl, charIDToTypeID( "Grn " ) );
                desc15.putReference( idChnl, ref5 );
                    var list3 = new ActionList();
// add g points;
for (var m = 0; m < theArray[2].length; m++) {
    addCurvePoint (list3, theArray[2][m], theArray[3][m])
    };
                desc15.putList( idCrv, list3 );
            list1.putObject( idCrvA, desc15 );
                var desc20 = new ActionDescriptor();
                    var ref6 = new ActionReference();
                    ref6.putEnumerated( idChnl, idChnl, charIDToTypeID( "Bl  " ) );
                desc20.putReference( idChnl, ref6 );
                    var list4 = new ActionList();
// add b points;
for (var m = 0; m < theArray[4].length; m++) {
    addCurvePoint (list4, theArray[4][m], theArray[5][m])
    };
                desc20.putList( idCrv, list4 );
            list1.putObject( idCrvA, desc20 );
        desc9.putList( idAdjs, list1 );
    desc8.putObject( idT, charIDToTypeID( "Crvs" ), desc9 );
executeAction( charIDToTypeID( "setd" ), desc8, DialogModes.NO );
//
return app.activeDocument.activeLayer;
////// add curve point //////
function addCurvePoint (theList, valueHor, valueVer) {
var desc11 = new ActionDescriptor();
desc11.putDouble( charIDToTypeID( "Hrzn" ), valueHor );
desc11.putDouble( charIDToTypeID( "Vrtc" ), valueVer );
theList.putObject( charIDToTypeID( "Pnt " ), desc11 );
};
};

 

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
March 5, 2025

 

// create curves layer with white, gray and black determined by three color samplers;
// 2025, use it at your own risk;
// call the function to run the script;
createCurveAdjustmetFromColorSamplers();
// create a function fo hold most of the code;
function createCurveAdjustmetFromColorSamplers(){
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var myDocument = activeDocument;
// needs an open document in a color mode that supports layers;
if(app.documents.length == 0 || ( app.activeDocument.mode != DocumentMode.RGB ) ){    
alert('This script requires a document in RGB');
return;
}
// color sample;
var outputArray = new Array();
var theSamples = [[1000,40], [1000, 500], [1000, 960]];
// collect the samples;
for(var sampleIndex = 0; sampleIndex < theSamples.length; sampleIndex++ ){
var theSampler = myDocument.colorSamplers.add(theSamples[sampleIndex]);
outputArray.push(theSampler.color);
theSampler.remove()
}
// create arrays of the color values:
var theArray = [[], [], []];
for (var m = 0; m < outputArray.length; m++) {
theArray[0].push(Math.round(outputArray[m].rgb.red));
theArray[1].push(Math.round(outputArray[m].rgb.green));
theArray[2].push(Math.round(outputArray[m].rgb.blue));
};
// create the curves layer;
rgbCurvesLayer ([theArray[0], [0, 128, 255], theArray[1], [0, 128, 255], theArray[2], [0, 128, 255]]);
app.preferences.rulerUnits = originalRulerUnits;
};
////// make rgb curves layer //////
function rgbCurvesLayer (theArray) {
var idpresetKind = stringIDToTypeID( "presetKind" );
var idpresetKindType = stringIDToTypeID( "presetKindType" );
var idChnl = charIDToTypeID( "Chnl" );
var idCrv = charIDToTypeID( "Crv " );
var idCrvA = charIDToTypeID( "CrvA" );
var idAdjL = charIDToTypeID( "AdjL" );
// =======================================================
    var desc5 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref2 = new ActionReference();
        ref2.putClass( idAdjL );
    desc5.putReference( idnull, ref2 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc6 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc7 = new ActionDescriptor();
            desc7.putEnumerated( idpresetKind, idpresetKindType, stringIDToTypeID( "presetKindDefault" ) );
        desc6.putObject( idType, charIDToTypeID( "Crvs" ), desc7 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc5.putObject( idUsng, idAdjL, desc6 );
executeAction( charIDToTypeID( "Mk  " ), desc5, DialogModes.NO );
// =======================================================
    var desc8 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref3 = new ActionReference();
        ref3.putEnumerated( idAdjL, charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
    desc8.putReference( idnull, ref3 );
    var idT = charIDToTypeID( "T   " );
        var desc9 = new ActionDescriptor();
        desc9.putEnumerated( idpresetKind, idpresetKindType, stringIDToTypeID( "presetKindCustom" ) );
        var idAdjs = charIDToTypeID( "Adjs" );
            var list1 = new ActionList();
                var desc10 = new ActionDescriptor();
                    var ref4 = new ActionReference();
                    ref4.putEnumerated( idChnl, idChnl, charIDToTypeID( "Rd  " ) );
                desc10.putReference( idChnl, ref4 );
                    var list2 = new ActionList();
// add r points;
for (var m = 0; m < theArray[0].length; m++) {
    addCurvePoint (list2, theArray[0][m], theArray[1][m])
    };	
                desc10.putList( idCrv, list2 );
            list1.putObject( idCrvA, desc10 );
                var desc15 = new ActionDescriptor();
                    var ref5 = new ActionReference();
                    ref5.putEnumerated( idChnl, idChnl, charIDToTypeID( "Grn " ) );
                desc15.putReference( idChnl, ref5 );
                    var list3 = new ActionList();
// add g points;
for (var m = 0; m < theArray[2].length; m++) {
    addCurvePoint (list3, theArray[2][m], theArray[3][m])
    };
                desc15.putList( idCrv, list3 );
            list1.putObject( idCrvA, desc15 );
                var desc20 = new ActionDescriptor();
                    var ref6 = new ActionReference();
                    ref6.putEnumerated( idChnl, idChnl, charIDToTypeID( "Bl  " ) );
                desc20.putReference( idChnl, ref6 );
                    var list4 = new ActionList();
// add b points;
for (var m = 0; m < theArray[4].length; m++) {
    addCurvePoint (list4, theArray[4][m], theArray[5][m])
    };
                desc20.putList( idCrv, list4 );
            list1.putObject( idCrvA, desc20 );
        desc9.putList( idAdjs, list1 );
    desc8.putObject( idT, charIDToTypeID( "Crvs" ), desc9 );
executeAction( charIDToTypeID( "setd" ), desc8, DialogModes.NO );
//
return app.activeDocument.activeLayer;
////// add curve point //////
function addCurvePoint (theList, valueHor, valueVer) {
var desc11 = new ActionDescriptor();
desc11.putDouble( charIDToTypeID( "Hrzn" ), valueHor );
desc11.putDouble( charIDToTypeID( "Vrtc" ), valueVer );
theList.putObject( charIDToTypeID( "Pnt " ), desc11 );
};
};

 

Known Participant
March 7, 2025

@c.pfaffenbichler this is awesome i thought i would never do that thank you so much for this.