Finding Similar HEX Colors Using a Threshold

I have an array of RGB hex colors. I would like to find a quick and dirty way to group them by similarity of color and threshold value.

spec: enter image description here

+5
source share
2 answers

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;

Then you compare $distance_squaredwith the squared threshold. Factors can be adjusted (especially blue can get a higher coefficient), as well as their amount (to match the threshold)

For a β€œslow and clean” solution, I started with here (and here for a more practical approach).

+3
source

"", , . , HSL/HSV , RGB.

+1

All Articles