Skip to main content
Known Participant
October 30, 2012
Question

Using color sampler values with curves adjustment layer

  • October 30, 2012
  • 2 replies
  • 5561 views

Hi, I have just started trying to teach myself javascript.  Based on preliminary research, it's become apparent that there are many functions in Photoshop that are either extremely cumbersome or impossible to code by hand without using the Script Listener.

My  goal is as follows: first, I will manually load two photos as layers in a single document.  Then I will manually place two or more color sampler points on the document.  At this point I would like the script to create a curves adjustment layer (ideally clipped to layer 2) and place as individual channel anchor points  the RGB data from the color sampler points on Layer 2, and then adjust the output of the points on each channel to the color sampler RGB values of layer 1.  

As my first script, I realize this is probably going to be a lot of work.

I did find some code that returns the average value of manually placed color sampler points.  Conceptually then, I would need to add code which creates a new curves adjustment layer and adds those RGB values (from a specific layer)  as anchor points on the individual channels,  and then hides one layer and looks at the RGB values of the color sampler points, and uses them as the output values for each anchor point.

Sounds simple enough from a conceptual standpoint.

I'm looking for some guidance on how to get started.

Which parts will I definitely need Scriptlistener for and will that be adequate to do the job?

How would you recommend I get started on this?

Thanks very much for any input.

This topic has been closed for replies.

2 replies

Inspiring
November 1, 2012

You should be able to get an average color from the colorSampler itself without any additional code as long as the area you want to average is one of the sizes in the sampler tool options. If the layers overlap you will need to hide/show the top layer to get the color from both layers as colorSampler reads the composite image.

Because you are doing most of the steps manually I don't think it would be too hard to script. I think an outline of the script would look something like this.

1. hide the top layer - DOM

2. read the color from the samplers and store the solidColor objects - DOM

3. show the top layer - DOM

4. read the colors again and store those - DOM

5. create a curve adjustment layer - Action Manager(scriptlistener)

6. move the adjustment layer to the top(if needed) and create clipping group - DOM

7. edit the adjustment layer using the stored color values - Action Manager

The hard part will be step 7. Especially if you want to have a flexible number of colorSampler/curve points. But with a little effort and maybe some help from this forum I think you can come up with a script that does what you want.

Jbean25Author
Known Participant
November 2, 2012

Thank you both for your thoughts.  I see Action Manager = ScriptListener, which is good to know.  I will be working almost always with two color sampler points, so based on what you've both indicated, that could simplify things.  Is the Adobe Scripting Guide the best place to go to find the necessary commands to do steps 1-4, and 6?

In step 7, if I use the action Manager, how do I indicate I want to recall the stored color sampler values?  I could be (and am probably) way off here, but I thought Action manager records scripting code based on what happens on the screen, so if the color sampler points are stored as variables in the script, it seems like I wouldn't be able to access them in the Photoshop interface to record with Action Manager. I.E. Action Manager would record the numerical value of the points that I move in the curves without tying them to the color sampler variables.

Conceptually it seems like it should be easy to write code that sets the color sampler points as variables, then calls the channels of the curves layer and sets the input and output numbers to the desired variables. 

Thanks again for giving me some guidance.

Inspiring
November 2, 2012

I think that I was somewhat miss-leading. The scriptlistener plug-in outputs Action Manger code but that is not the same as it being equal. Yes, scirptlistener only records the settings used for that recorded acton. Sometimes you can use the code straight from the scriptlistener log but most of the time you will want to edit it so it uses variables instead of the hard coded settings.

I have been thinking about how this script would work. For one thing it would be limited to 4 points on the curve because of the ColorSampler limit of four samples. The ActionDescriptor for a curve expects the points to be in order so you would either need to place the colorSamplers in order i.e. the first sampler is for the black point of the curve or come up with a way to determine what sample is to be used for each curve point. Creating the curve descriptor will be the hardest part of writing a script to do this task.

As for the rest, yes, it is easy. I would do something like this...

// create a function fo hold most of the code

function createCurveAdjustmetFromColorSamplers(){

    // first add some condition checks

    // needs an open document in a color mode that supports layers

    if(app.documents.length == 0 || ( app.activeDocument.mode == DocumentMode.BITMAP || app.activeDocument.mode == DocumentMode.INDEXEDCOLOR ) ){   

        alert('This script requires a document in Greyscale, RGB, CMYK, or Lab mode.');

        return;

    }

    // check for at least two colorSamplers

    if(app.activeDocument.colorSamplers.length < 2 ){

        alert('This script requires at least two colorSamplers.');

        return;

    }

    // last check for at least two layers - assume they will be on same level( not in layerSet )

    if(app.activeDocument.layers.length < 2 ){

        alert('This script requires at least two layers.');

        return;

    }

    // create varaibles to hold the colorSampler's color property for each layer

    // for the bottom layer

    var outputArray = new Array();

    // for top layer - array could also be created this way

    var inputArray = [];

    // store the number of samples because it will be needed in more than one place

    var numberOfSamples = app.activeDocument.colorSamplers.length;

   

    // hide the top layer

    app.activeDocument.layers[0].visible = false;

    // collect the samples from the bottom layer

    for(var sampleIndex = 0; sampleIndex < numberOfSamples; sampleIndex++ ){

        outputArray.push(app.activeDocument.colorSamplers[sampleIndex].color);

    }

    // turn the top layer back on

    app.activeDocument.layers[0].visible = true;

    // collect those samples

    for(var sampleIndex = 0; sampleIndex < numberOfSamples; sampleIndex++ ){

        inputArray.push(app.activeDocument.colorSamplers[sampleIndex].color);

    }

    // make sure the top layer is the activeLayer

    app.activeDocument.activeLayer = app.activeDocument.layers[0];

    // use Action Manager code to create new curve adjustment layer

    makeCurveAdjustmentLayer();

}

function makeCurveAdjustmentLayer(){//works with any mode

   var desc = new ActionDescriptor();

   var ref = new ActionReference();

     ref.putClass( charIDToTypeID( "AdjL" ) );

   desc.putReference( charIDToTypeID( "null" ), ref );

   var desc1 = new ActionDescriptor();

     desc1.putClass( charIDToTypeID( "Type" ), charIDToTypeID("Crvs" ) );

   desc.putObject( charIDToTypeID( "Usng" ), charIDToTypeID( "AdjL" ), desc1 );

   executeAction( charIDToTypeID( "Mk  " ), desc, DialogModes.NO );

}

// call the function to run the script

createCurveAdjustmetFromColorSamplers();

c.pfaffenbichler
Community Expert
Community Expert
November 1, 2012

it's become apparent that there are many functions in Photoshop that are either extremely cumbersome or impossible to code by hand without using the Script Listener.

Actually you may not need to use ScriptingListener.plugin yourself if you can find the code elsewhere but the

Document Object Model

will indeed not suffice for the task at hand (as far as I can tell) and you will have to use

Action Manager code.

DOM code may look neater and be more easy to read but it has drawbacks – that not all functions can be controlled thus being the probably most significant one.

I guess you need to record the creation of a Curves Layer with the appropriate number of points for the appropriate number of Channels and feed the numbers you collect from the ColorSamplers for the two Layers into that.

As you are vague about the actual number of ColorSamplers the task becomes more complicated and you may have to utilize a for-clause and a function for creating the points.

In my opinion this task is a problematic choice for a »first script«.