Sort System.Media.Colors according to position in visible spectrum
Whats the quickest/easiest way to sort the colors in the System.Media.Colors according to its position in the visible spectrum (red to blue or blue to red doesn't matter) ?
EDIT:
Here is the result of the sort (hue->saturation->brightness):
This is probably technically correct but visually it still isn't. Can someone throw light开发者_如何学JAVA on what the problem is?
You want to sort colors by hue, it seems. To do that, you need to compute the hue of a color, and the Wpf System.Media.Color
struct doesn't include properties to do that for you.
You've two options:
- You can manually compute the hue. This isn't really hard, but it's messy: lots of if-then statements. Sample code doing this can be found on devx. This is more work and less readable, but probably the faster option.
- You can use
System.Drawing.Color.GetHue
. The older winforms-eraSystem.Drawing
color structure does support computing the hue. That makes the code really simple; something likeSystem.Drawing.Color.FromArgb(col.R, col.G, col.B).GetHue()
will suffice - nice and short! However, it does mean you'll be dealing with two structs with identical names and you'll of course need ro reference theSystem.Drawing.dll
assembly. These kind of methods tend to be slow and do lots of sanity checking, so if performance is critical this is likely to be less attractive.
The colorspace is three-dimensional. Though you can sort along one direction (such as the hue, here), the resulting sequence of colors will only appear continuous if you keep the other two directions (saturation and brightness in the HSB model) constant. That's why most color-pickers choose a two-dimensional representation, but even then, you must leave one dimension constant within a given color picker to maintain a continuous gradient.
NVM mentioned mono as possible solution, it's not. The problem is current display technology. Mono underscores that problem. What looks great in color, mono scatters, and visa versa. That does not mean that sort is impossible. It can be done mathematically only.
Making sort even more complicated, we are all color blind, and that blindness varies greatly between each individual. Math will fetch the correct sort, but none can foretell what each of us will see.
Spectrum ≡ Tone ≈ Hue (((≡ visual cortex, ≈ endocrine compensation)))
Blue ›› rgb(0, 0, 255) ›› nCol B0, 0%, 0%
Ncol an,w%,b% ›› a|(n+(((w+b)/2)/cos((w+b)/2)
a|((n+1.w%)-(n+1.b%))/cos(w-b)...and this nCol extension of RGB does a perfect sort, one-by-one, phew!
精彩评论