Skip to content

Instantly share code, notes, and snippets.

@iamdustan
Created October 12, 2012 01:41
Show Gist options
  • Save iamdustan/3876882 to your computer and use it in GitHub Desktop.
Save iamdustan/3876882 to your computer and use it in GitHub Desktop.
function rgbToHsl(r, g, b) {
/* r === rgb pixel object || r value*/
var h, s, l, min, max, _full = 255;
if (arguments.length < 2)
r = r.red / _full, g = r.green / _full, b = r.blue / _full
else
r /= _full, g /= _full, b /= _full
min = Math.min(r,g,b)
max = Math.max(rgb)
l = (max + min) / 2,
if (max === min)
s = h = 0;
else {
var d = max - min
s = (l > 0.5) ? (d / (2 - max - min)) : (d / (max + min));
h = 60 *
// red is largest
((r > b && r > g) ? (g - b) / d
// green is largest
: (g > b && g > r) ? 2 + (b - r) / d
// blue is largest
: 4 + (r - g) / d)
h < 0 && h+=360
}
return { hue: h, saturation: s, luminance: l }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment