1

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

Lawrene
  • 11
  • 5

1 Answers1

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