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

Community
  • 1
  • 1
Arturino
  • 358
  • 7
  • 19
  • http://en.wikipedia.org/wiki/Color_quantization . Or you could try fiddling with http://www.php.net/manual/en/function.imagetruecolortopalette.php – biziclop Jun 29 '12 at 17:19
  • I would suggest yoo use a HSV model so you can compare Hue, Saturation, and Value among several colors. – TheZ Jun 29 '12 at 17:19

2 Answers2

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

Walter Tross
  • 12,237
  • 2
  • 40
  • 64
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.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • yep i figured that much too. just looking for a formula if anyone has one handy ;-) – Arturino Jun 29 '12 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). – Oliver Charlesworth Jun 29 '12 at 17:31
  • 1
    thanks - I also found this helpful : http://stackoverflow.com/questions/1633828/distance-between-colours-in-php/1634206#1634206 – Arturino Jun 29 '12 at 18:09