Search code examples
javascriptphotoshop-scriptcs4

Get the color of one pixel at Photoshop scripts


I am trying to figure out how to get the color of one defined Pixel.

In my imagination its shoud look like:

color = get.color.Pixel(x,y);

Maybe someone can help me with this piece of code?


Solution

  • Photoshop's JavaScript API doesn't provide a mechanism like you imagine in your question.

    You'll need to utilize the Document.colorSamplers.add([x, y]) method, then read each component color value via its properties:

    The following gist shows how to obtain either rgb or cmyk values for a given x,y coordinate:

    #target photoshop
    
    // Define the x and y coordinates for the pixel to sample.
    var x = 1;
    var y = 1;
    
    // Add a Color Sampler at a given x and y coordinate in the image.
    var pointSample = app.activeDocument.colorSamplers.add([(x - 1),(y - 1)]);
    
    // Obtain array of RGB values.
    var rgb = [
        pointSample.color.rgb.red,
        pointSample.color.rgb.green,
        pointSample.color.rgb.blue
    ];
    
    // Obtain array of rounded CMYK values.
    var cmyk = [
        Math.round(pointSample.color.cmyk.cyan),
        Math.round(pointSample.color.cmyk.magenta), 
        Math.round(pointSample.color.cmyk.yellow),
        Math.round(pointSample.color.cmyk.black)
    ];
    
    // Remove the Color Sampler.
    pointSample.remove();
    
    // Display the complete RGB values and each component color.
    alert('RGB: ' + rgb)
    alert('red: ' + rgb[0])
    alert('green: ' + rgb[1])
    alert('blue: ' + rgb[2])
    
    // Display the complete CMYK values and each component color.
    alert('CMYK: ' + cmyk)
    alert('cyan: ' + cmyk[0])
    alert('magenta: ' + cmyk[1])
    alert('yellow: ' + cmyk[2])
    alert('black: ' + cmyk[3])