I have an android app that I want to tell me if two colors match. And I know matching is done by using their distance in the color wheel. how do I get the distance between two of them so I can tell if they match or not
Asked
Active
Viewed 415 times
1 Answers
0
From this post here, you can tell if 2 colors match using something like this:
boolean sameColor(Color c1, Color c2){
double distance = (c1.r - c2.r)*(c1.r - c2.r) + (c1.g - c2.g)*(c1.g - c2.g) + (c1.b - c2.b)*(c1.b - c2.b)
if (distance == 0){
return true;
}
return false;
}
Alternatively, if you want to check if the 2 colors are within some tolerance of eachother, you could try something like this instead:
boolean sameColor(Color c1, Color c2){
double distance = (c1.r - c2.r)*(c1.r - c2.r) + (c1.g - c2.g)*(c1.g - c2.g) + (c1.b - c2.b)*(c1.b - c2.b)
if (distance > tolerance){
return true;
}
return false;
}

AbsoluteSpace
- 710
- 2
- 11
- 21
-
Thanks. But by "match" you mean? – Lawrene Nov 23 '18 at 07:40
-
match meaning that the difference between the 2 colors is 0 – AbsoluteSpace Nov 23 '18 at 15:37