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