[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 /* 2 YUI 3.17.2 (build 9c3c78e) 3 Copyright 2014 Yahoo! Inc. All rights reserved. 4 Licensed under the BSD License. 5 http://yuilibrary.com/license/ 6 */ 7 8 YUI.add('color-hsv', function (Y, NAME) { 9 10 /** 11 Color provides static methods for color conversion hsv values. 12 13 Y.Color.toHSV('f00'); // hsv(0, 100%, 100%) 14 15 Y.Color.toHSVA('rgb(255, 255, 0'); // hsva(60, 100%, 100%, 1) 16 17 18 @module color 19 @submodule color-hsv 20 @class HSV 21 @namespace Color 22 @since 3.8.0 23 **/ 24 Color = { 25 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]*)?\)/, 34 35 /** 36 @static 37 @property STR_HSV 38 @type String 39 @default hsv({*}, {*}%, {*}%) 40 @since 3.8.0 41 **/ 42 STR_HSV: 'hsv({*}, {*}%, {*}%)', 43 44 /** 45 @static 46 @property STR_HSVA 47 @type String 48 @default hsva({*}, {*}%, {*}%, {*}) 49 @since 3.8.0 50 **/ 51 STR_HSVA: 'hsva({*}, {*}%, {*}%, {*})', 52 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 }, 65 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 }, 78 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; 98 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 } 108 109 s = (max === 0) ? 0 : 1 - (min / max); 110 111 // ensure h is between 0 and 360 112 while (h < 0) { 113 h += 360; 114 } 115 h %= 360; 116 h = Math.round(h); 117 118 // saturation is percentage 119 s = Math.round(s * 100); 120 121 // value is percentage 122 v = Math.round(max * 100); 123 124 if (toArray) { 125 return [h, s, v]; 126 } 127 128 return Y.Color.fromArray([h, s, v], Y.Color.TYPES.HSV); 129 }, 130 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))); 154 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 } 169 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)); 173 174 if (toArray) { 175 return [r, g, b]; 176 } 177 178 return Y.Color.fromArray([r, g, b], Y.Color.TYPES.RGB); 179 } 180 181 }; 182 183 Y.Color = Y.mix(Color, Y.Color); 184 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'}); 187 188 189 }, '3.17.2', {"requires": ["color-base"]});
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |