# Compare Color Variables

**URL:** <https://discourse.processing.org/t/compare-color-variables/17238>\
**Category:** Coding Questions\
**Created:** [January 21, 2020, 10:41pm UTC](https://discourse.processing.org/t/compare-color-variables/17238 "2020-01-21T22:41:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nciprian](https://avatars.discourse-cdn.com/v4/letter/n/a88e57/32.png) [@nciprian](https://discourse.processing.org/u/nciprian)\
**Post date:** [January 21, 2020, 10:41pm UTC](https://discourse.processing.org/t/compare-color-variables/17238/1 "2020-01-21T22:41:40Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![unique\_username](https://avatars.discourse-cdn.com/v4/letter/u/a88e57/32.png) [@unique\_username](https://discourse.processing.org/u/unique_username)\
**Post date:** [January 21, 2020, 11:12pm UTC](https://discourse.processing.org/t/compare-color-variables/17238/2 "2020-01-21T23:12:55Z")

</div>

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.

```auto
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.");
}
}

```

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [January 21, 2020, 11:29pm UTC](https://discourse.processing.org/t/compare-color-variables/17238/3 "2020-01-21T23:29:43Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Yom](https://avatars.discourse-cdn.com/v4/letter/y/c4cdca/32.png) [@Yom](https://discourse.processing.org/u/Yom)\
**Post date:** [April 22, 2020, 1:59pm UTC](https://discourse.processing.org/t/compare-color-variables/17238/4 "2020-04-22T13:59:27Z")

</div>

Here is how I would do it

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

```

---

<div class="post-metadata">

**Author:** ![asadij](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asadij/32/8943_2.png) [@asadij](https://discourse.processing.org/u/asadij)\
**Post date:** [April 22, 2020, 11:10pm UTC](https://discourse.processing.org/t/compare-color-variables/17238/5 "2020-04-22T23:10:16Z")

</div>

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

```auto
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");

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 22, 2020, 11:43pm UTC](https://discourse.processing.org/t/compare-color-variables/17238/6 "2020-04-22T23:43:17Z")

</div>

> [@asadij](#):
>
> `if ("'"+inside+"'" == "'"+middle+"'")`

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

> **[Object.prototype.toString() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString)**
>
> The toString() method of Object instances returns a string representing this object. This method is meant to be overridden by derived objects for custom type coercion logic.
