Ghoul Fool
Ghoul Fool

Reputation: 6947

Check if RGB colours are the same

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;
}

Upvotes: 0

Views: 206

Answers (1)

Sergey Kritskiy
Sergey Kritskiy

Reputation: 2269

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;
}

Upvotes: 1

Related Questions