1
YUI.add('color-hsl', function (Y, NAME) {
4
Color provides static methods for color conversion to hsl values.
6
Y.Color.toHSL('f00'); // hsl(0, 100%, 50%)
8
Y.Color.toHSLA('rgb(255, 255, 0'); // hsla(60, 100%, 50%, 1)
22
@default /hsla?\(([.\d]*), ?([.\d]*)%, ?([.\d]*)%,? ?([.\d]*)?\)/
25
REGEX_HSL: /hsla?\(([.\d]*), ?([.\d]*)%, ?([.\d]*)%,? ?([.\d]*)?\)/,
31
@default hsl({*}, {*}%, {*}%)
34
STR_HSL: 'hsl({*}, {*}%, {*}%)',
40
@default hsla({*}, {*}%, {*}%, {*})
43
STR_HSLA: 'hsla({*}, {*}%, {*}%, {*})',
46
Converts provided color value to an HSL string.
53
toHSL: function (str) {
54
var clr = Y.Color._convertTo(str, 'hsl');
55
return clr.toLowerCase();
59
Converts provided color value to an HSLA string.
66
toHSLA: function (str) {
67
var clr = Y.Color._convertTo(str, 'hsla');
68
return clr.toLowerCase();
72
Parses the RGB string into h, s, l values. Will return an Array
73
of values or an HSL string.
77
@param {Boolean} [toArray]
78
@return {String|Array}
81
_rgbToHsl: function (str, toArray) {
83
rgb = Y.Color.REGEX_RGB.exec(str),
87
max = Math.max(r, g, b),
88
min = Math.min(r, g, b),
94
if (r === g && g === b) {
101
} else if (r === max) {
102
h = ((60 * (g - b) / sub) + 360) % 360;
103
} else if (g === max) {
104
h = (60 * (b - r) / sub) + 120;
106
h = (60 * (r - g) / sub) + 240;
113
if (l === 0 || l === 1) {
115
} else if (l <= 0.5) {
127
s = Math.round(s * 100);
128
l = Math.round(l * 100);
134
return 'hsl(' + h + ', ' + s + '%, ' + l + '%)';
138
Parses the HSL string into r, b, g values. Will return an Array
139
of values or an RGB string.
143
@param {Boolean} [toArray]
144
@return {String|Array}
147
_hslToRgb: function (str, toArray) {
148
// assume input is [h, s, l]
149
// TODO: Find legals for use of formula
150
var hsl = Y.Color.REGEX_HSL.exec(str),
151
h = parseInt(hsl[1], 10) / 360,
152
s = parseInt(hsl[2], 10) / 100,
153
l = parseInt(hsl[3], 10) / 100,
163
q = (l + s) - (l * s);
168
r = Math.round(Color._hueToRGB(p, q, h + 1/3) * 255);
169
g = Math.round(Color._hueToRGB(p, q, h) * 255);
170
b = Math.round(Color._hueToRGB(p, q, h - 1/3) * 255);
176
return 'rgb(' + r + ', ' + g + ', ' + b + ')';
180
Converts the HSL hue to the different channels for RGB
187
@return {Number} value for requested channel
190
_hueToRGB: function(p, q, hue) {
191
// TODO: Find legals for use of formula
194
} else if (hue > 1) {
199
return p + (q - p) * 6 * hue;
205
return p + (q - p) * (2/3 - hue) * 6;
212
Y.Color = Y.mix(Color, Y.Color);
214
Y.Color.TYPES = Y.mix(Y.Color.TYPES, {'HSL':'hsl', 'HSLA':'hsla'});
215
Y.Color.CONVERTS = Y.mix(Y.Color.CONVERTS, {'hsl': 'toHSL', 'hsla': 'toHSLA'});
218
}, '@VERSION@', {"requires": ["color-base"]});