# How to check if a color is close to another color?

**URL:** <https://discourse.processing.org/t/how-to-check-if-a-color-is-close-to-another-color/1018>\
**Category:** Project Guidance\
**Created:** [June 16, 2018, 9:54pm UTC](https://discourse.processing.org/t/how-to-check-if-a-color-is-close-to-another-color/1018 "2018-06-16T21:54:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [June 16, 2018, 9:54pm UTC](https://discourse.processing.org/t/how-to-check-if-a-color-is-close-to-another-color/1018/1 "2018-06-16T21:54:06Z")

</div>

I’m trying to check if a color is close to another color. Help!

-Grim

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 16, 2018, 10:15pm UTC](https://discourse.processing.org/t/how-to-check-if-a-color-is-close-to-another-color/1018/2 "2018-06-16T22:15:22Z")

</div>

Use the `dist()` function. Example below. Consult the reference to learn about the different functions presented here.

Kf

```java
final int RED=240;
final int GREEN=60;
final int BLUE=5;

final float COLOR_THRESHOLD=50;

color c=color(255,50,10);
int cred=red(c);
int cgreen=green(c);
int cblue=blue(c);

float cDista=dist(cred,cgreen,cblue,RED,GREEN,BLUE);

if(cDista<COLOR_THRESHOLD){
  println("Colors are close to each other based on selected threshold: "+COLOR_THRESHOLD);
}
else{
  println("Colors are not close to each other on RGB space.");
}

```

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [June 16, 2018, 11:27pm UTC](https://discourse.processing.org/t/how-to-check-if-a-color-is-close-to-another-color/1018/3 "2018-06-16T23:27:02Z")

</div>

Thanks! That should work!

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [June 20, 2018, 4:30pm UTC](https://discourse.processing.org/t/how-to-check-if-a-color-is-close-to-another-color/1018/4 "2018-06-20T16:30:33Z")

</div>

Notice that the distance in RGB colorspace and perceptual distance (how different two colors appear to the average person) can be quite different.

For an interesting example code for calculating perceptual distance between two colors, see:

> <https://github.com/processing/processing/issues/4663>
