0

I'm trying to see if colours are the same in Photoshop scripting

RGB 0,128,60 == RGB 0,128,60 // true
RGB 0,128,60 == RGB 0,128,64 // false

Only most fancy javascript bits like every and => as seen here don't work with Photoshop's ECMA script

This is what I have (based on Dogbert's example here), but I'm sure there's got to be a cleaner way. Apart from converting them to hex and then comparing them.

alert(identical_colours([[0,128,60], [0,128,60]])); // true
alert(identical_colours([[0,128,60], [0,128,64]])); // false



function identical_colours(arr)
{
  for(var i = 0; i < arr.length - 1; i++)
  {
    if(arr[i][0] !== arr[i+1][0])
    {
      return false;
    }
    if(arr[i][1] !== arr[i+1][1])
    {
      return false;
    }
    if(arr[i][2] !== arr[i+1][2])
    {
      return false;
    }
  }
  // True! Yay!
  return true;
}
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125

1 Answers1

1

Why not use Photoshop own color definition — SolidColor — that has a .isEqual() method:

var color1 = colorFromRGB(0, 128, 60);

var color2 = colorFromRGB(0, 128, 60);

alert(color1.isEqual(color2)); // > true

function colorFromRGB(r, g, b)
{
  var color = new SolidColor();
  color.rgb.red = r;
  color.rgb.green = g;
  color.rgb.blue = b;
  return color;
}
Sergey Kritskiy
  • 2,199
  • 2
  • 15
  • 20
  • I knew about SolidColor, it's difficult not to create a colour without it :), but I would have never have guessed at .isEqual() method! – Ghoul Fool Jan 20 '21 at 10:07
  • @GhoulFool I just made myself a warper that generates `SolidColor` from different inputs. Similar to my answer but from an object as argument: `newSolidColor({type:'rgb',value:[0,0,0]})`, `newSolidColor({type:'hex',value:'ffffff'})`, etc – Sergey Kritskiy Jan 20 '21 at 10:11