Using javascript/jquery, I want to sort an array of rgba values to the colours of the visible spectrum. By doing this, like shades should be bunched together. Is there a plugin to do this or how would I go about doing it?
Spectrum image: http://www.gamonline.com/catalog/colortheory/images/spectrum.gif
If your array of colors is like this:
where each color
ciis an array of three numbers between 0 and 255then, you can use this function to convert the colors to HSL
and sort them by hue
Here is a runnable example (thanks Ionică Bizău):
sortedRgbArrwill contain the rgb colors ofrgbArrsorted more or less like the colors of the visible spectrum.The problem is that the HSL spectrum looks like this:
Your spectrum is weird because it doesn’t have all colors, such as pink.
I guess that’s because pink doesn’t exist in the nature, it’s a combination of the colors of the opposite extremes of light’s spectrum. But we have it in rgb, so you have to decide where do you want it.
Moreover, it seems that your spectrum goes from lower to higher wavelength, not frequency. But then your spectrum is a reverse of HSL’s spectrum.
Replace
c1.color[0] - c2.color[0]withc2.color[0] - c1.color[0]if you want it like your spectrum.