Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Dont Trust PhotoShops JavaScript ColorSampler

Beware the ColorSampler in PhotoShop's JavaScript (aka ExtendScript) implementation. In CS6, it doesn't always pick the color from the coordinates assigned to it. Problematic since that's the purpose of the tool.

To illustrate, take this image :

It's ten pixels high with alternating rows of red and green. The hex colors are [TODO: Code shorthand span ] and ` #005500 ` , respectively. Now, take this script :

javascript
app.preferences.rulerUnits = Units.PIXELS;

for (var row=0; row <=9; row = row + 1) {
  app.activeDocument.colorSamplers.removeAll();
  app.activeDocument.colorSamplers.add([0, row]);
  $.writeln(app.activeDocument.colorSamplers[0].color.rgb.hexValue);
}

It makes sure PhotoShop is using pixels and then samples the first pixel of each row. If the ColorSampler worked as expected, the result would be a list of ten lines alternating between '990000' and '005500'. Instead, running it in PhotoShop CS6 (Version 13.0.6) on a MacBook with OS X 10.10 (14A389) produces this :

990000
        005500
        990000
        005500
        990000
        005500
        990000
        990000
        990000
        005500

Everything looks good until just before the end where '990000' occurs three times in a row. That shows the ColorSampler is pulling the color from either row 7 or 9 even though it was assigned to row 8. It's only one error but in programming that's all it takes to render something useless.

I've found a work around to make the ColorSampler trustworthy again. Instead of using integers (e.g. ` [10, 0] ` ) for the coordinates, use floats that have an extra tenth of a pixel added on them (e.g. ` [10.1, 0.1] ` ). In theory, this doesn't make sense because a pixel is supposed to be the smallest possible unit. Regardless, it works. For example, here's an updated version of the script that adds "0.1" to every row except the last one. (If you add it to the last one it pushes off the canvas and PhotoShop throws an error.)

javascript
app.preferences.rulerUnits = Units.PIXELS;

for (var row=0; row <=9; row = row + 1) {
  var adjustedRow = (row == app.activeDocument.height) ? row : row + 0.1;
  app.activeDocument.colorSamplers.removeAll();
  app.activeDocument.colorSamplers.add([0, adjustedRow]);
  $.writeln(app.activeDocument.colorSamplers[0].color.rgb.hexValue);
}

Running this on the same test image produces the expected output :

990000
        005500
        990000
        005500
        990000
        005500
        990000
        005500
        990000
        005500

And with that, ColorSampler is trustworthy enough to use again. (Though, I'll certainly be keeping a close eye on it.)

Anecdotal evidence suggest very few folks automate PhotoShop with JavaScript. Seeing a bug like this in mature software is more evidence. Kudos to Adobe for continuing to invest resources in something so few use.