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!


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


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);

  println("Colors are close to each other based on selected threshold: "+COLOR_THRESHOLD);
  println("Colors are not close to each other on RGB space.");

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