0

Is there a way to check if a color is close to another color ?

For example whether a color ( say #D4FFA9 ) is close of being green?

something like :

boolean areColorsClose(int colorOne, int colorTwo) {}

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Bisma Frühling
  • 776
  • 1
  • 11
  • 21
  • 1
    "is there a way"? Yes. Define what "close" means to you (for instance closer than a certain distance in the RGB colour cube) and program it respectively. – Henry Oct 27 '18 at 08:23
  • convert rgb to [hsv](https://en.wikipedia.org/wiki/HSL_and_HSV) and check `h` part – pskink Oct 27 '18 at 08:28
  • @Henry #D4FFA9 and #4E9B00 are both greens, the first one is light and the second one is dark, and by close I mean are the two previously stated colors are close? (light green and dark green, as example) – Bisma Frühling Oct 27 '18 at 08:34
  • @pskink could you please elaborate more? – Bisma Frühling Oct 27 '18 at 08:35
  • see `android.graphics.Color` official documentation – pskink Oct 27 '18 at 08:36
  • So taking this example, light green is close to dark green or not? – Henry Oct 27 '18 at 08:38
  • yes they are close in terms of being both green @Henry – Bisma Frühling Oct 27 '18 at 08:39
  • Then comparing hue as @pskink suggested seems to be what you want. – Henry Oct 27 '18 at 08:40
  • Alright I will the check the hue value, thank you both Henry and @pskink – Bisma Frühling Oct 27 '18 at 08:43
  • There is a large amount of research on measuring the difference between colors. The human eye is more sensitive to some colors so smaller differences will be noticed. Also, different cultures perceive colors differently as color perception is influenced by language. You can look here: https://en.wikipedia.org/wiki/Color_difference for a bit more information. – Ves Oct 27 '18 at 15:51

1 Answers1

-2

You could subtract one from the other, such as:

int color1 = 0x7fffff;
int color2 = 0x000123;

int color_difference = color1 - color2;

Then decide what you consider 'close to each other':

if (color_difference <= [your acceptable difference]){
    // Colors are close.
}else //Colors are too different.

Both of these colors are a shade of green: 0x08ff76 and 0x04b252

Their difference is: 44d24

if (color_difference <= 44d24){
    // Colors are close.
}else //Colors are too different.

You would need to decide what you consider close first :)

RAPTORp
  • 90
  • 6
  • 1
    That would weigh differences in the red channel higher then differences in the green channel and much higher than differences in the blue channel. – Henry Oct 28 '18 at 07:49