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

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

-Grim

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

Kf

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.");
}
3 Likes

Thanks! That should work!

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:

1 Like