Last active
August 29, 2015 14:04
-
-
Save kkxlkkxllb/4e2699e3ca027c9eeeb0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Color = { | |
_rgba: function(r, g, b, a) { | |
return { r: r, g: g, b: b, a: a, toString: function() { return "rgba(" + r +", " + g + ", " + b + ", " + a + ")"; } }; | |
}, | |
_hsl: function(h, s, l) { | |
return { h: h, s: s, l: l, toString: function() { return "hsl(" + h +", " + s*100 + "%, " + l*100 + "%)"; } }; | |
}, | |
_hex2Rgba: function(hex) { | |
var num = parseInt(hex.substring(1), 16); | |
return this._rgba(num >> 16, num >> 8 & 255, num & 255, 1); | |
}, | |
//conversion function (modified a bit) taken from http://mjijackson.com/2008/02/ | |
//rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript | |
_hsl2Rgba: function(hsl) { | |
var h = hsl.h/360, s = hsl.s, l = hsl.l, r, g, b; | |
function hue2rgb(p, q, t) { | |
if(t < 0) t += 1; | |
if(t > 1) t -= 1; | |
if(t < 1/6) return p + (q - p) * 6 * t; | |
if(t < 1/2) return q; | |
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6; | |
return p; | |
} | |
if (s === 0) { | |
r = g = b = l; // achromatic | |
} else { | |
var q = l < 0.5 ? l * (1 + s) : l + s - l * s; | |
var p = 2 * l - q; | |
r = Math.floor( (hue2rgb(p, q, h + 1/3)) * 255); | |
g = Math.floor( (hue2rgb(p, q, h)) * 255); | |
b = Math.floor( (hue2rgb(p, q, h - 1/3)) * 255); | |
} | |
return this._rgba(r, g, b, 1); | |
} | |
} | |
module.exports = Color; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment