Compare Color Variables

Hello,
Is there a way to compare to color variable?

let inside = color(204, 102, 0);
let middle = color(204, 102, 0);

i need to compare this two variables, but none of the usual ways work. (inside == middle)

1 Like

You could set each color frame as a variable(e.g. color(x,y,z; and color(a,b,c) ) and compare the two variables with something like if(y = b)

NOTE; the code below probably won’t work, I’m just using to show the general idea.

int x = 204;
int y = 102;
int z = 0;
int a =304;
int b = 102;
int c = 10;

leftInside = color(x,y,z);
leftMiddle = color(a,b,c);

void draw() {
if( y = b) {
print("Y is equal to B.");
}
}

I believe you would need to convert your colors to their individual component values. Something like this:

function colorsEqual(colorOne, colorTwo) {
  const rOne = red(colorOne);
  const gOne = green(colorOne);
  const bOne = blue(colorOne);
  
  const rTwo = red(colorTwo);
  const gTwo = green(colorTwo);
  const bTwo = blue(colorTwo);

  return rOne == rTwo && gOne == gTwo && bOne == bTwo;
}
3 Likes

Here is how I would do it

function colorsEqual(c1, c2)
{
  if (c1.levels == undefined || c2.levels == undefined)
    return false;
  
  for (let i = 0; i < 4; i++)
    if (c1.levels[i] != c2.levels[i])
      return false;
  
  return true;
}
1 Like

Recently I faced this issue and solved by converting color variable into strings and then compare. In your case code will be like.

let inside = color(204, 102, 0);
let middle = color(204, 102, 0);

if ("'"+inside+"'" == "'"+middle+"'") //there is a single-quote mark in double-quotes
print("colors matched");
else
print("Sorry, colors didn't match");
2 Likes

Got rid of quotes: :wink:
if (inside.toString() == middle.toString())

1 Like