Skip to main content
Participating Frequently
April 30, 2016
Question

Javascript colorsamplers only returns 255 or 0

  • April 30, 2016
  • 8 replies
  • 1411 views

I using Photoshop CC2015 on a Windows 8.1 PC, Coding in Javascript.

I'm trying to get the color of some pixels. I was working fine but now it is only returning 255 or 0.

My code:

  theSampler1 = doc.colorSamplers.add([580, 380]);

    $.writeln(theSampler1.color.rgb.red);

    $.writeln(theSampler1.color.rgb.green);

    $.writeln(theSampler1.color.rgb.blue);

Returns 0,0,0 but should be 79,131,7.

It was working fine, but now it is not. Is there a setting or something I changed and need to change back?

This topic has been closed for replies.

8 replies

Participating Frequently
May 2, 2016

How do I target the RGB Channel?

JJMack
Community Expert
Community Expert
May 3, 2016

The scriptlistener code I poster does that.  If the Layer Mask is targeted and you click on the layers content icon in the layer palette to target the layers content that is what the Adobe scriptlistener plug-in recorded.  The plug-in is like Photoshop Action recorded. However it records Script code to use Photoshop Action manager. Its like an Action step. All the setting are hard coded for the step and executed. The code is not very readable.

You can create script functions from script listener cord by replacing the variable hard coded constant  setting with  variable.    If you look at the code posted you can set some if the hard coded sets "slct" , "Chnl", "RGB "  select channel RGB

JJMack
MR74270
Inspiring
May 3, 2016

Hi all,

I think it is important to know (for a general project) if the mode is RGB or CMYK.

After this, you can select the Alpha channel with the listener code given by JJMack.

Now you will have only color values (if there are color informations of course).

var docRef = activeDocument;

var idMode = charIDToTypeID( "RGB " );

if (docRef.mode==DocumentMode.CMYK) {idMode = charIDToTypeID( "CMYK" )}

selectAlphaChannel()

function selectAlphaChannel()

{

// =======================================================

var idslct = charIDToTypeID( "slct" );

    var desc57 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref43 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        ref43.putEnumerated( idChnl, idChnl, idMode );

    desc57.putReference( idnull, ref43 );

    var idMkVs = charIDToTypeID( "MkVs" );

    desc57.putBoolean( idMkVs, false );

executeAction( idslct, desc57, DialogModes.NO );

}

Participating Frequently
May 2, 2016

Second update. So this morning is was not working correctly again. So it was not the code. What I have discovered is that in the Layers Panel, the thumbnail needs to be selected for my code to get the color of a pixel.

When anything else is selected in the Layer Panel, I get the RGB:0,0,0, or RBG:255,255,255. So it was not the code but a setting in Photoshop. Still not 100% sold on this as a full solution.

pixxxelschubser
Community Expert
Community Expert
May 2, 2016

jime75168259​,

sure, it is the code:

// ...

if(layer.isBackgroundLayer) {

    //Pixel sample1

    theSampler1 = doc.colorSamplers.add([1208, 1252);

    layersArr [0] = Math.round(theSampler1.color.rgb.red);

    layersArr [1] = Math.round(theSampler1.color.rgb.green);

    layersArr [2] = Math.round(theSampler1.color.rgb.blue);

    } else { // !!! this is the reason for black or white values if something else in the layers panel is selected

    theSampler1 = doc.colorSamplers.add([580, 380]);

    layersArr [0] = Math.round(theSampler1.color.rgb.red);

    layersArr [1] = Math.round(theSampler1.color.rgb.green);

    layersArr [2] = Math.round(theSampler1.color.rgb.blue);

}

// ...

Have fun

Participating Frequently
May 2, 2016

Okay, this is very strange! So I change a value that did not show in my code to you and now it is working.

Here is what I changed:

Before:

strJSON =  strJSON + "\"layer" + i + ":pixel2544x2676"\":\"RGB[" + layersArr[0] + "," + layersArr[1] + "," + layersArr[2] + "]\", ";

After:

strJSON =  strJSON + "\"layer" + i + "-2544x2676-" + layer.kind + "\":\"RGB[" + layersArr[0] + "," + layersArr[1] + "," + layersArr[2] + "]\", ";

I removed the ":" from the string and now it is working fine.

MR74270
Inspiring
May 2, 2016

Hi all,

Just a question : when the background layer is active, if you select Layer...New...

Do you read "Layer from Background" or "Background from Layer" ?

MR74270
Inspiring
May 2, 2016

Forget my question as your code is working now. Next time, I think it will be more useful to have a copy of the exact code.

Participating Frequently
April 30, 2016

Yes, I'm sure about the color, This is what my output was:

{"Black & White 1 layer0":"[pixel:580x380,R:79,G:131,B:7,Opacity100]",

"Black & White 1 layer0":"[pixel:412x2592,R:142,G:187,B:148,Opacity100]",

"Black & White 1 layer0":"[pixel:3224x1080,R:36,G:20,B:4,Opacity100]",

"Black & White 1 layer0":"[pixel:2544x2676,R:72,G:120,B:34,Opacity100]",

"Background":"[pixel:1208x1252,R:168,G:79,B:0,Opacity100]",

"Background":"[pixel:1308x2236,R:131,G:54,B:0,Opacity100]",

"Background":"[pixel:2180x1296,R:201,G:110,B:5,Opacity100]",

"Background":"[pixel:2324x2348,R:151,G:71,B:0,Opacity100]", }

I have change the sample points and keep getting the same output of 0,0,0 or 255,255,255. The picture is of a yellowish fruit called a Carambola. All the background points are on the fruit. The other points are a gray scale color on the B&W layer.

Same picture, nothing has changed in the code or the picture. So I think it may be a setting in Photoshop?

JJMack
Community Expert
Community Expert
April 30, 2016

Here the code I use It looks like your and worked for me.

preferences.typeUnits = TypeUnits.PIXELS;

preferences.rulerUnits = Units.PIXELS;

var doc = app.activeDocument;

var layersArr = new Array();

var colors = '';

for (var s=0,len=app.activeDocument.colorSamplers.length;s<len;s++) {

  var colorSamplerRef = app.activeDocument.colorSamplers;

  colors = colors + ("ColorSampler" + s + " x=" + colorSamplerRef.position[0].value + " ,y=" + colorSamplerRef.position[1].value + " Color="

  + Math.round(colorSamplerRef.color.rgb.red) + ","

  + Math.round(colorSamplerRef.color.rgb.green) + ","

  + Math.round(colorSamplerRef.color.rgb.blue) + "\n" );

  };

for(var i = 0; i < doc.layers.length; i++){

        var layer = doc.layers;

  //alert(layer.name);

        doc.colorSamplers.removeAll();

        if(layer.isBackgroundLayer){

                    //Pixel sample1

                    theSampler1 = doc.colorSamplers.add([1208, 1252]);

                    layersArr [0] = Math.round(theSampler1.color.rgb.red);

                    layersArr [1] = Math.round(theSampler1.color.rgb.green);

                    layersArr [2] = Math.round(theSampler1.color.rgb.blue);

  colors = colors + ("\ntheSampler1 x=" + theSampler1.position[0].value + " ,y=" + theSampler1.position[1].value + " Color="

  + layersArr[0] + ","  + layersArr[1] + "," + layersArr[2] + "\n");

               }

       else{

                    theSampler1 = doc.colorSamplers.add([580, 380]);

                    layersArr [0] = Math.round(theSampler1.color.rgb.red);

                    layersArr [1] = Math.round(theSampler1.color.rgb.green);

                    layersArr [2] = Math.round(theSampler1.color.rgb.blue);

  colors = colors + ("\ntheSampler1 x=" + theSampler1.position[0].value + " ,y=" + theSampler1.position[1].value + " Color="

  + layersArr[0] + ","  + layersArr[1] + "," + layersArr[2] + "\n");

               }

          }

alert(colors)

JJMack
Participating Frequently
April 30, 2016

Not a black dot, I do have the removeAll();, and it is set to Units.PIXELS.

preferences.typeUnits = TypeUnits.PIXELS;

preferences.rulerUnits = Units.PIXELS;

for(var i = 0; i < doc.layers.length; i++){

        var layer = doc.layers

        doc.colorSamplers.removeAll();

        if(layer.isBackgroundLayer){

                    //Pixel sample1

                    theSampler1 = doc.colorSamplers.add([1208, 1252);

                    layersArr [0] = Math.round(theSampler1.color.rgb.red);

                    layersArr [1] = Math.round(theSampler1.color.rgb.green);

                    layersArr [2] = Math.round(theSampler1.color.rgb.blue);

               }

       else{

                    theSampler1 = doc.colorSamplers.add([580, 380]);

                    layersArr [0] = Math.round(theSampler1.color.rgb.red);

                    layersArr [1] = Math.round(theSampler1.color.rgb.green);

                    layersArr [2] = Math.round(theSampler1.color.rgb.blue);

               }

          }

I'm actually check 4 different pixels in the background and all of them are returning 255, 255, 255.

Then I'm checking 4 different pixels on the next layers that are not background, and not the same location. They return 0,0, 0.

The code was above is shorten, not the full code. It was working fine, then all the sudden it stop returning the correct values. I have restart the apps and PC.

Thanks for you helps so far, any other ideas?

JJMack
Community Expert
Community Expert
April 30, 2016

I'm on Windows 10 CC 2015 with the four update you code works on my system once I added the missing bracket ], defining the layerArr and setting doc your code worked.

Are you sure at your sample points your doc has a color  other than black.  I add a layer where  I had red and 50% gray in those areas and that was what was set into you layerArr array. which you reuse for the second sample

JJMack
SuperMerlin
Inspiring
April 30, 2016

Make sure your ruler units are set to pixels.

Participating Frequently
April 30, 2016

Bonjour

Try this before and at end :

activeDocument.colorSamplers.removeAll();

JJMack
Community Expert
Community Expert
April 30, 2016

Did you perhaps put a black dot at position 580,380 in the document

JJMack