2

I need help finding the closest color match from a set of predefined colors and a single random color, here is my code:

color = array('124','197','118'); // LIGHT GREEN

$match = array(
    array('255', '000', '000', 'FF0000'), //red
    array('000', '255', '000', '00FF00'), //green
    array('000', '000', '255', '0000FF'), //blue    
    array('0', '255', '255', '00ffff'), //cyan
    array('117', '076', '036', '754c24'), //brown
    array('000', '000', '000', '000000'), //black
    array('149', '149', '149', '959595'), //grey
    array('242', '101', '034', 'f26522'), //orange
    array('245', '152', '157', 'f5989d'), //pink
    array('255', '255', '000', 'FFFF00'), //yellow
    array('102', '045', '145', '662d91'), //purple
    array('255', '255', '255', 'FFFFFF')); //white

echo 'Color: <div style="background-color:#'.$color.';width:25px;height:25px"></div>';    //color

foreach($match as $co) $temp[] = array( sqrt(($color[0]-$co[0])^2+($color[1]-$co[1])^2+($color[2]-$co[2])^2) , $co[3]);

asort($temp);

foreach($temp as $ta) { echo 'Matched Color: <div style="background-color:#'.$ta[1].';width:25px;height:25px"></div>'; break; } 

It returns grey instead of green? How can I fix this problem? Than

CanSpice
  • 34,814
  • 10
  • 72
  • 86
John
  • 21
  • 2
  • 1
    You would probably want to look into calculating the color's `HSL` (Hue, Saturation, Light) values as that would be more accurate at finding near matching colors than `RGB`. I don't have an example (which is why this is a comment, not an answer), but it shouldn't be hard to find something. – Jonathan Kuhn Jan 06 '11 at 21:19

2 Answers2

1

here is a link to a matching question with answer:

RGB to closest predefined color

but, if you have an indexed image already, I suggest using this instead:

http://php.net/manual/en/function.imagecolorclosest.php

Community
  • 1
  • 1
dqhendricks
  • 19,030
  • 11
  • 50
  • 83
  • That first link uses the same formula that is in my script, I have also tried using the imagecolorcloset php function by creating a image containing every predefined color, however it yields the same results. – John Jan 07 '11 at 02:35
1

I put your colors into my color converter to analyze your problem. I think you can see that gray is also the colosest match visually.

I think the tripple 124/197/118 is not a light green but foremost a desaturated green which explains the result.

I'm not sure about some of your color definitions. For me

000 255 000 = Lime Green
000 128 000 = Green
128 128 128 = Gray (50%)

If you still don't like the result:

1, leave out gray from your predefined colors

or

2, define a threshold for returning gray
-> If result is gray but distance > threshold x,
then take the second colosest match
John wwc
  • 11
  • 1
  • I have tried to leave out gray, and now orange is appearing as the matched color instead of green. – John Jan 07 '11 at 12:33
  • Try: brown(150,75,0) | orange(255,127,0) | [bright] pink(255,0,127) | purple(127,0,255) ... note however that brown is just a dark orange! You might want to look at the color picker on my site and shift through pure colors by 30° distance for your predefined colors. – John wwc Jan 07 '11 at 12:53