“At least 25% matches” is not a well-defined problem. Matches at least 25% of what, and by what metric? There are many possible options. If you are comparing RGB colors, distance metrics derived from vector norms are obvious. The three most important of them are:
- 1-norm or Manhattan distance: distance = abs (r1-r2) + abs (g1-g2) + abs (b1-b2)
- 2-normal or Euclidean distance: distance = sqrt (pow (r1-r2, 2) + pow (g1-g2, 2) + pow (b1-b2, 2)) (you calculate the square of this, which is fine - you can avoid sqrt if you just check the threshold by squaring the threshold too)
- Infinity-norm: distance = max (abs (r1-r2), abs (g1-g2), abs (b1-b2))
There are many other possibilities, of course. You can check if they are at some distance from each other: if you want to allow a difference of 25% (over the range of possible RGB values) in one color channel, the threshold values used for 3 methods are 3/4 * 255 , sqrt (3) / 4 * 255 and 255/4, respectively. This is a very crude metric.
The best way to measure the distance between colors is to convert your colors into a perceived single color space, such as CIELAB and compare there; there is a pretty good Wikipedia article on this. This may be redundant depending on your intended application, but these are color spaces where the measured distances have the best correlation with the distances perceived by the human visual system.
source share