views:

327

answers:

3
+7  Q: 

Sorting by Color

I have a long list (1000+) of hex colors broken up in general color categories (Red, Orange, Blue, etc). When I show the list of colors in each category I need to show them in order of shade. i.e. light red first and dark red last.

What would the algorithm be to do this? (googling has failed me)

+1  A: 

It sounds like you want to sort them by intensity. Try searching for that.

Justin
+3  A: 

Convert the colors from RGB to a HSV or HSL scale and then sort them by Value or Lightness. Lightness might work better as it would capture the "faded" colors better, such as pink->red->dark red.

RGB to HSV Color Space Conversion.

James Schek
There is a PHP implementation here (beware the DaniWeb popups) I can't vouch for its quality though.
Darren Newton
http://www.daniweb.com/forums/thread38302.html
Darren Newton
Thanks for the link, I found this one that is working great:http://www.phpclasses.org/browse/package/4598.htmlWorking on the sorting now, will report back with results.Thanks for the help!
Ok, final. Used this class to convert from Hex to other versions. Very useful: http://www.phpclasses.org/browse/package/4598.html I tried sorting on the HSV but that didn't seem to be as correct as sorting on the RGB values. However, that did exactly what I was looking for. Thanks so much for everyone's help!
+1  A: 

If you convert the colors to HSV space, you could potentially sort them by the hue, then the value.

The hue will determine the color "category" - ie: red, blue, green, etc.

The value and saturation will affect the "lightness" of the final color. You might need some experimentation to get the ideal sort, though.

Reed Copsey