How do you get the hue of a #xxxxxx colour?
The wikipedia article has a formula which looks like something that can easily be implemented:
http://en.wikipedia.org/wiki/Hue#Computing_hue_from_RGB
Edit: here's a function which uses those formulas:
function getHue(color) {
var r = parseInt(color.substring(0,2),16)/255;
var g = parseInt(color.substring(2,4),16)/255;
var b = parseInt(color.substring(4,6),16)/255;
var hue;
if ((r >= g) && (g >= b)) {
hue = 60*(g-b)/(r-b);
} else if ((g > r) && (r >= b)) {
hue = 60*(2 - (r-b)/(g-b));
}
//... continue here
return hue;
}
alert(getHue('FF0000')); // correctly returns 0
alert(getHue('408000')); // correctly returns 90
alert(getHue('0000FF')); // not implemented yet
Just continue using the formulas from the table in that wikipedia article for other angles.
hue = Atan2(1.732050808 * (G - B), (2 * R - G - B)) * 57.295779513;
http://en.wikipedia.org/wiki/Hue
hue = Atan2(sqr(3) * (G - B), 2 * R - G - B)
The result will be in polar coordinates. Multiply by 180 and divide by pi to convert to angle.
I have stumbled on this issue as well after years, that's how I have worked around it.
Credits: Brandon Mathis of hslpicker.com, code was taken from here
function hexToRgb (color) {
let hex = color[0] === '#' ? color.slice(1) : color;
let c;
// expand the short hex by doubling each character, fc0 -> ffcc00
if (hex.length !== 6) {
hex = ((() => {
const result = [];
for (c of Array.from(hex)) {
result.push(`${c}${c}`);
}
return result;
})()).join('');
}
const colorStr = hex.match(/#?(.{2})(.{2})(.{2})/).slice(1);
const rgb = colorStr.map(col => parseInt(col, 16));
rgb.push(1);
return rgb;
}
function rgbToHsl (rgb) {
const r = rgb[0] / 255;
const g = rgb[1] / 255;
const b = rgb[2] / 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const diff = max - min;
const add = max + min;
const hue =
min === max ?
0
: r === max ?
(((60 * (g - b)) / diff) + 360) % 360
: g === max ?
((60 * (b - r)) / diff) + 120
:
((60 * (r - g)) / diff) + 240;
const lum = 0.5 * add;
const sat =
lum === 0 ?
0
: lum === 1 ?
1
: lum <= 0.5 ?
diff / add
:
diff / (2 - add);
const h = Math.round(hue);
const s = Math.round(sat * 100);
const l = Math.round(lum * 100);
const a = rgb[3] || 1;
return [h, s, l, a];
}
I have written a small wrapper around those 2 functions to convert array of hex colors to arrays of strings describing H / S / L components
function hexToHsl (color) {
const rgb = hexToRgb(color);
const hsl = rgbToHsl(rgb);
return `original: ${ color } - H: ${ hsl[0] } S: ${ hsl[1] } L: ${ hsl[2] }`;
}
Usage:
var colors = ['#51bce6','#6dcff6','#829CBD','#565a5c']
colors.map(color => hexToHsl(color))
=> ["original: #51bce6 - H: 197 S: 75 L: 61", "original: #6dcff6 - H: 197 S: 88 L: 70", "original: #829CBD - H: 214 S: 31 L: 63", "original: #565a5c - H: 200 S: 3 L: 35"]
If you search for how to convert RGB to HSL, you'll find a number of algorithms, including in the Wikipedia article linked by Sergey.
First, extract the RGB components of the hex color notation.
var color='#c7d92c'; // A nice shade of green.
var r = parseInt(color.substr(1,2), 16); // Grab the hex representation of red (chars 1-2) and convert to decimal (base 10).
var g = parseInt(color.substr(3,2), 16);
var b = parseInt(color.substr(5,2), 16);
That'll get you the byte (0-255) representation of your color. In this case, 199, 217, 44.
You can then use the formulae from the Wikipedia article to calculate hue, or shamelessly steal someone else's code:
function rgbToHsl(r, g, b){
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
if(max == min){
h = s = 0; // achromatic
}else{
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return [h, s, l];
}
(See the source page for documentation and a hslToRgb()
function.)
We can now put those two snippets together and get the hue:
var hue = rgbToHsl(r, g, b)[0] * 360;
The [0]
is to grab the hue – the function returns an array ([h,s,l]
). We multiply by 360 since hue is returned as a value between 0 and 1; we want to convert it to degrees.
With the example color of #c7d92c
, hue
will be ~66.24. Photoshop's color picker says the hue of that color is 66° so it looks like we're good!