2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('color-hsl', function (Y, NAME) {
11 Color provides static methods for color conversion to hsl values.
13 Y.Color.toHSL('f00'); // hsl(0, 100%, 50%)
15 Y.Color.toHSLA('rgb(255, 255, 0'); // hsla(60, 100%, 50%, 1)
29 @default /hsla?\(([.\d]*), ?([.\d]*)%, ?([.\d]*)%,? ?([.\d]*)?\)/
32 REGEX_HSL: /hsla?\(([.\d]*), ?([.\d]*)%, ?([.\d]*)%,? ?([.\d]*)?\)/,
38 @default hsl({*}, {*}%, {*}%)
41 STR_HSL: 'hsl({*}, {*}%, {*}%)',
47 @default hsla({*}, {*}%, {*}%, {*})
50 STR_HSLA: 'hsla({*}, {*}%, {*}%, {*})',
53 Converts provided color value to an HSL string.
60 toHSL: function (str) {
61 var clr = Y.Color._convertTo(str, 'hsl');
62 return clr.toLowerCase();
66 Converts provided color value to an HSLA string.
73 toHSLA: function (str) {
74 var clr = Y.Color._convertTo(str, 'hsla');
75 return clr.toLowerCase();
79 Parses the RGB string into h, s, l values. Will return an Array
80 of values or an HSL string.
84 @param {Boolean} [toArray]
85 @return {String|Array}
88 _rgbToHsl: function (str, toArray) {
90 rgb = Y.Color.REGEX_RGB.exec(str),
94 max = Math.max(r, g, b),
95 min = Math.min(r, g, b),
101 if (r === g && g === b) {
108 } else if (r === max) {
109 h = ((60 * (g - b) / sub) + 360) % 360;
110 } else if (g === max) {
111 h = (60 * (b - r) / sub) + 120;
113 h = (60 * (r - g) / sub) + 240;
120 if (l === 0 || l === 1) {
122 } else if (l <= 0.5) {
134 s = Math.round(s * 100);
135 l = Math.round(l * 100);
141 return 'hsl(' + h + ', ' + s + '%, ' + l + '%)';
145 Parses the HSL string into r, b, g values. Will return an Array
146 of values or an RGB string.
150 @param {Boolean} [toArray]
151 @return {String|Array}
154 _hslToRgb: function (str, toArray) {
155 // assume input is [h, s, l]
156 // TODO: Find legals for use of formula
157 var hsl = Y.Color.REGEX_HSL.exec(str),
158 h = parseInt(hsl[1], 10) / 360,
159 s = parseInt(hsl[2], 10) / 100,
160 l = parseInt(hsl[3], 10) / 100,
170 q = (l + s) - (l * s);
175 r = Math.round(Color._hueToRGB(p, q, h + 1/3) * 255);
176 g = Math.round(Color._hueToRGB(p, q, h) * 255);
177 b = Math.round(Color._hueToRGB(p, q, h - 1/3) * 255);
183 return 'rgb(' + r + ', ' + g + ', ' + b + ')';
187 Converts the HSL hue to the different channels for RGB
194 @return {Number} value for requested channel
197 _hueToRGB: function(p, q, hue) {
198 // TODO: Find legals for use of formula
201 } else if (hue > 1) {
206 return p + (q - p) * 6 * hue;
212 return p + (q - p) * (2/3 - hue) * 6;
219 Y.Color = Y.mix(Color, Y.Color);
221 Y.Color.TYPES = Y.mix(Y.Color.TYPES, {'HSL':'hsl', 'HSLA':'hsla'});
222 Y.Color.CONVERTS = Y.mix(Y.Color.CONVERTS, {'hsl': 'toHSL', 'hsla': 'toHSLA'});
225 }, '3.13.0', {"requires": ["color-base"]});