6

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

2

2 Answers 2

3

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

1

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.

3
  • yep i figured that much too. just looking for a formula if anyone has one handy ;-)
    – Arturino
    Jun 29, 2012 at 17:27
  • @Artur: A formula for what? Conversion formulae to e.g. HSV are given in the Wikipedia article. Euclidean distance is simply sqrt((a1-a2)^2 + (b1-b2)^2 + (c1-c2)^2). Jun 29, 2012 at 17:31
  • 1
    thanks - I also found this helpful : stackoverflow.com/questions/1633828/…
    – Arturino
    Jun 29, 2012 at 18:09

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.