Get the RGB color of a single pixel

Upload Photoshop Scripts, download Photoshop Scripts, Discussion and Support of Photoshop Scripts

Moderators: Tom, Kukurykus

wade

Get the RGB color of a single pixel

Post by wade »

Code: Select allvar pixelColor = getPixelColor(100,100); // the two values are the horizontal and vertical position of the pixel you want the color from
function getPixelColor(pixH,pixV){ // this only works for an active RGB document
   var selRegion = null;
   selRegion = Array(Array(pixH, pixV),
            Array(pixH + 1, pixV),
            Array(pixH + 1, pixV + 1),
            Array(pixH, pixV + 1),
            Array(pixH, pixV));
   var currDoc = app.activeDocument;
   currDoc.selection.select(selRegion);
   var histR = currDoc.channels.getByName("Red").histogram;
   var histG = currDoc.channels.getByName("Green").histogram;
   var histB = currDoc.channels.getByName("Blue").histogram;
   var returnColor = new RGBColor();
   for (iHistR in histR){
      if(histR[iHistR]!=0){
         returnColor.red = iHistR;
      }
   }
   for (iHistG in histG){
      if(histG[iHistG]!=0){
         returnColor.green = iHistG;
      }
   }
   for (iHistB in histB){
      if(histB[iHistB]!=0){
         returnColor.blue = iHistB;
      }
   }
   return returnColor;
}
alert(pixelColor.hexValue+"\n"+pixelColor.red+","+pixelColor.green+","+pixelColor.blue);


-wade
xbytor

Get the RGB color of a single pixel

Post by xbytor »

Here's my variant:
Code: Select allgetColorAt = function(doc, x, y) {

   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) { return i; }
      }
      return 0;
    }

   selectBounds(doc, [x, y, x+1, y+1]);
    var pColour = new SolidColor();
   
    pColour.rgb.red   = findPV(doc.channels["Red"].histogram);
    pColour.rgb.green = findPV(doc.channels["Green"].histogram);
    pColour.rgb.blue  = findPV(doc.channels["Blue"].histogram);

   doc.selection.deselect(); // or, even better, undo
};
wade

Get the RGB color of a single pixel

Post by wade »

x that is much more elegant. I keep seeing requests for this and I put mine together to explore doing a (really slow) version of Conway's Game of Life (http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) in CS2. Just to see if it can be done.

after i get that working i will move up to a 256 color averaging version and finally the 256avg version run on each color channel. That should create some interesting images when seeded with a typical photograph. At least it will give my home computer something to do while I am at work
!nso

Get the RGB color of a single pixel

Post by !nso »

Guys, you are the best!

I've nearly cracked my head thinking how to do that. I've only discovered scripting for photoshop 2 days ago (though been programming for web for 8 years) and i think it ROCKS!

x, i tried your toolkit -> thumbs up, lots and lots of hours in that, eh?
(ColorBook crashed my PS CS2 though)
xbytor

Get the RGB color of a single pixel

Post by xbytor »

There are other variants of this here and on Adobe Forums. I thing the first version of this I saw was by fazstp.

The ColorBook was never really 'productized' mostly because I never really needed it for my own work or for a client. If it's something you'll find useful, I'll polish it up for the next rev of xtools. Just get me some more info about what's causing it to fail (e.g. one of builtin books). There's probably a conversion error in there somewhere.

-X