NOBUG: Fixed file access permissions
[moodle.git] / lib / yuilib / 3.13.0 / color-hsv / color-hsv-debug.js
blob92f6d5d7dff3d9bfa571ef414a89c318a9929849
1 /*
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/
6 */
8 YUI.add('color-hsv', function (Y, NAME) {
10 /**
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)
18 @module color
19 @submodule color-hsv
20 @class HSV
21 @namespace Color
22 @since 3.8.0
23 **/
24 Color = {
26     /**
27     @static
28     @property REGEX_HSV
29     @type RegExp
30     @default /hsva?\(([.\d]*), ?([.\d]*)%, ?([.\d]*)%,? ?([.\d]*)?\)/
31     @since 3.8.0
32     **/
33     REGEX_HSV: /hsva?\(([.\d]*), ?([.\d]*)%, ?([.\d]*)%,? ?([.\d]*)?\)/,
35     /**
36     @static
37     @property STR_HSV
38     @type String
39     @default hsv({*}, {*}%, {*}%)
40     @since 3.8.0
41     **/
42     STR_HSV: 'hsv({*}, {*}%, {*}%)',
44     /**
45     @static
46     @property STR_HSVA
47     @type String
48     @default hsva({*}, {*}%, {*}%, {*})
49     @since 3.8.0
50     **/
51     STR_HSVA: 'hsva({*}, {*}%, {*}%, {*})',
53     /**
54     Converts provided color value to an HSV string.
55     @public
56     @method toHSV
57     @param {String} str
58     @return {String}
59     @since 3.8.0
60     **/
61     toHSV: function (str) {
62         var clr = Y.Color._convertTo(str, 'hsv');
63         return clr.toLowerCase();
64     },
66     /**
67     Converts provided color value to an HSVA string.
68     @public
69     @method toHSVA
70     @param {String} str
71     @return {String}
72     @since 3.8.0
73     **/
74     toHSVA: function (str) {
75         var clr = Y.Color._convertTo(str, 'hsva');
76         return clr.toLowerCase();
77     },
79     /**
80     Parses the RGB string into h, s, v values. Will return an Array
81         of values or an HSV string.
82     @protected
83     @method _rgbToHsv
84     @param {String} str
85     @param {Boolean} [toArray]
86     @return {String|Array}
87     @since 3.8.0
88     **/
89     _rgbToHsv: function (str, toArray) {
90         var h, s, v,
91             rgb = Y.Color.REGEX_RGB.exec(str),
92             r = rgb[1] / 255,
93             g = rgb[2] / 255,
94             b = rgb[3] / 255,
95             max = Math.max(r, g, b),
96             min = Math.min(r, g, b),
97             delta = max - min;
99         if (max === min) {
100             h = 0;
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;
107         }
109         s = (max === 0) ? 0 : 1 - (min / max);
111         // ensure h is between 0 and 360
112         while (h < 0) {
113             h += 360;
114         }
115         h %= 360;
116         h = Math.round(h);
118         // saturation is percentage
119         s = Math.round(s * 100);
121         // value is percentage
122         v = Math.round(max * 100);
124         if (toArray) {
125             return [h, s, v];
126         }
128         return Y.Color.fromArray([h, s, v], Y.Color.TYPES.HSV);
129     },
131     /**
132     Parses the HSV string into r, b, g values. Will return an Array
133         of values or an RGB string.
134     @protected
135     @method _hsvToRgb
136     @param {String} str
137     @param {Boolean} [toArray]
138     @return {String|Array}
139     @since 3.8.0
140     **/
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
146             r,
147             g,
148             b,
149             i = Math.floor(h / 60) % 6,
150             f = (h / 60) - i,
151             p = v * (1 - s),
152             q = v * (1 - (s * f)),
153             t = v * (1 - (s * (1 - f)));
155         if (s === 0) {
156             r = v;
157             g = v;
158             b = v;
159         } else {
160             switch (i) {
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;
167             }
168         }
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));
174         if (toArray) {
175             return [r, g, b];
176         }
178         return Y.Color.fromArray([r, g, b], Y.Color.TYPES.RGB);
179     }
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"]});