Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 color similarity and threshold value.

spec: enter image description here

like image 577
Arturino Avatar asked Jun 29 '12 17:06

Arturino


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;

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).

like image 173
Walter Tross Avatar answered Sep 22 '22 16:09

Walter Tross


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.

like image 29
Oliver Charlesworth Avatar answered Sep 21 '22 16:09

Oliver Charlesworth