I have an array of RGB hex colors. I would like to find a quick and dirty way to group them by color similarity and threshold value.
spec:
quick and dirty:
$dr = $red1 - $red2;
$dg = $green1 - $green2;
$db = $blue1 - $blue2;
$fr = 2; // may be adjusted
$fg = 4; // "
$fb = 1; // "
$distance_squared = $fr * $dr * $dr + $fg * $dg * $dg + $fb * $db * $db;
You would then compare $distance_squared
to the square of the threshold. The factors may be adjusted (especially blue might get a higher factor), as well as their sum (in order to match the threshold)
For a "slow and clean" solution, I would start from here (and here for a more practical approach).
Choose a color space, and define "similarity" as e.g. Euclidean distance between the coordinates of the two colours. HSL/HSV might be a better choice than RGB, for instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With