Ask HN: color sorting algorithm?

2 points by xentronium ↗ HN
Imagine a pallete with N colors. You need to display them in some order that suits human eye best.

I seem to have problems with definition of what I want. Ideally, I'd like to see some gradients, e.g.: blue gradient from lightest to darkest, yellow gradient from lightest to darkest, etc.

The problem is that sorting by [H,S,V/L] or [H,V/L,S] doesn't seem to work for non-ideal palletes. The biggest problems are with shades of gray or something similar to shade of gray (when [R,G,B] components are almost equal) that appear in between gradients. Also, it should allow some deviation on hue (which would be invisible for human eye but surely visible for simple sorting algorithm), but my simple tries of generating a keys consisting of weighed sum of components failed.

What I also tried: building a matrix of RGB distance and then trying to sort colors keeping distance between two neighbours minimal; using combination of HSL and YUV components; sorting by [R,G,B], [H,S,L] keys and all the possible transpositions of keys.

What I haven't tried yet: hilbert curves, from what I see on pictures it won't help.

Suggestions, anyone?

7 comments

[ 3.2 ms ] story [ 18.6 ms ] thread
You need to display them in some order that suits human eye best.

That sounds woefully ill-defined, if you ask me. My suggestion would be that you try to figure out better what it is you are looking for, and then start thinking about the algorithm to implement it.

I seem to have problems with definition of what I want.

Yep.

You can try using ranges of frequencies in the light specter and ordering by wavelength.
Isn't it similar to hue?
Technically yes, but you are trying to sort by HSV, or just by euclidean distance between RGB values, but RGB values doesn't correlate to frequencies. You could map RGB values to frequencies and order by frequency/wavelength instead of ordering by RGB values using distance.
I have the same problem I sorted colors by eucleudian distance , even distance CIELAB2000 in LCH format , it does not give the best result. How to convert the RGB to frequency/Wavelength ??
what is the wavelength of brown, and beige, or white?

As colors are not uniquely defined by their wavelength there is no linear one dimensional ordering of colors. It's more like cube: http://goo.gl/aCp5X

So now the questions is how to sort it anyway if you need to, which dimension are you going to choose?

Have you considered that what you're trying to do is impossible? Without constraints on the colors being sorted, there is no single "obvious" mapping from a random collection of colours chosen from 3-space into 1-space.

If you do have constraints on the colors being chosen then you'll need to be explicit as to what they are, and then use them in a non-trivial manner.