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-hsv', function (Y, NAME) {
11 Color provides static methods for color conversion hsv values.
13 Y.Color.toHSV('f00'); // hsv(0, 100%, 100%)
15 Y.Color.toHSVA('rgb(255, 255, 0'); // hsva(60, 100%, 100%, 1)
30 @default /hsva?\(([.\d]*), ?([.\d]*)%, ?([.\d]*)%,? ?([.\d]*)?\)/
33 REGEX_HSV: /hsva?\(([.\d]*), ?([.\d]*)%, ?([.\d]*)%,? ?([.\d]*)?\)/,
39 @default hsv({*}, {*}%, {*}%)
42 STR_HSV: 'hsv({*}, {*}%, {*}%)',
48 @default hsva({*}, {*}%, {*}%, {*})
51 STR_HSVA: 'hsva({*}, {*}%, {*}%, {*})',
54 Converts provided color value to an HSV string.
61 toHSV: function (str) {
62 var clr = Y.Color._convertTo(str, 'hsv');
63 return clr.toLowerCase();
67 Converts provided color value to an HSVA string.
74 toHSVA: function (str) {
75 var clr = Y.Color._convertTo(str, 'hsva');
76 return clr.toLowerCase();
80 Parses the RGB string into h, s, v values. Will return an Array
81 of values or an HSV string.
85 @param {Boolean} [toArray]
86 @return {String|Array}
89 _rgbToHsv: function (str, toArray) {
91 rgb = Y.Color.REGEX_RGB.exec(str),
95 max = Math.max(r, g, b),
96 min = Math.min(r, g, b),
101 } else if (max === r) {
102 h = 60 * (g - b) / delta;
103 } else if (max === g) {
104 h = (60 * (b - r) / delta) + 120;
105 } else { // max === b
106 h = (60 * (r - g) / delta) + 240;
109 s = (max === 0) ? 0 : 1 - (min / max);
111 // ensure h is between 0 and 360
118 // saturation is percentage
119 s = Math.round(s * 100);
121 // value is percentage
122 v = Math.round(max * 100);
128 return Y.Color.fromArray([h, s, v], Y.Color.TYPES.HSV);
132 Parses the HSV string into r, b, g values. Will return an Array
133 of values or an RGB string.
137 @param {Boolean} [toArray]
138 @return {String|Array}
141 _hsvToRgb: function (str, toArray) {
142 var hsv = Y.Color.REGEX_HSV.exec(str),
143 h = parseInt(hsv[1], 10),
144 s = parseInt(hsv[2], 10) / 100, // 0 - 1
145 v = parseInt(hsv[3], 10) / 100, // 0 - 1
149 i = Math.floor(h / 60) % 6,
152 q = v * (1 - (s * f)),
153 t = v * (1 - (s * (1 - f)));
161 case 0: r = v; g = t; b = p; break;
162 case 1: r = q; g = v; b = p; break;
163 case 2: r = p; g = v; b = t; break;
164 case 3: r = p; g = q; b = v; break;
165 case 4: r = t; g = p; b = v; break;
166 case 5: r = v; g = p; b = q; break;
170 r = Math.min(255, Math.round(r * 256));
171 g = Math.min(255, Math.round(g * 256));
172 b = Math.min(255, Math.round(b * 256));
178 return Y.Color.fromArray([r, g, b], Y.Color.TYPES.RGB);
183 Y.Color = Y.mix(Color, Y.Color);
185 Y.Color.TYPES = Y.mix(Y.Color.TYPES, {'HSV':'hsv', 'HSVA':'hsva'});
186 Y.Color.CONVERTS = Y.mix(Y.Color.CONVERTS, {'hsv': 'toHSV', 'hsva': 'toHSVA'});
189 }, '3.13.0', {"requires": ["color-base"]});