]>
git.proxmox.com Git - extjs.git/blob - extjs/packages/ux/classic/src/colorpick/ColorUtils.js
4 Ext
.define('Ext.ux.colorpick.ColorUtils', function (ColorUtils
) {
5 var oldIE
= Ext
.isIE
&& Ext
.ieVersion
< 10;
10 constructor: function () {
14 backgroundTpl
: oldIE
?
15 'filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0, ' +
16 'startColorstr=\'#{alpha}{hex}\', endColorstr=\'#{alpha}{hex}\');' :
17 'background: {rgba};',
19 setBackground
: oldIE
? function (el
, color
) {
21 var tpl
= Ext
.XTemplate
.getTpl(ColorUtils
, 'backgroundTpl'),
23 hex
: ColorUtils
.rgb2hex(color
.r
, color
.g
, color
.b
),
24 alpha
: Math
.floor(color
.a
* 255).toString(16)
26 bgStyle
= tpl
.apply(data
);
28 el
.applyStyles(bgStyle
);
30 } : function (el
, color
) {
32 var tpl
= Ext
.XTemplate
.getTpl(ColorUtils
, 'backgroundTpl'),
34 rgba
: ColorUtils
.getRGBAString(color
)
36 bgStyle
= tpl
.apply(data
);
38 el
.applyStyles(bgStyle
);
42 // parse and format functions under objects that match supported format config
43 // values of the color picker; parse() methods recieve the supplied color value
44 // as a string (i.e "FFAAAA") and return an object form, just like the one
45 // ColorPickerModel vm "selectedColor" uses. That same object form is used as a
46 // parameter to the format() methods, where the appropriate string form is expected
47 // for the return result
50 HEX6: function(colorO
) {
51 return ColorUtils
.rgb2hex(colorO
.r
, colorO
.g
, colorO
.b
);
54 // "FFAA00FF" (last 2 are opacity)
55 HEX8: function(colorO
) {
56 var hex
= ColorUtils
.rgb2hex(colorO
.r
, colorO
.g
, colorO
.b
),
57 opacityHex
= Math
.round(colorO
.a
* 255).toString(16);
59 if (opacityHex
.length
< 2) {
63 hex
+= opacityHex
.toUpperCase();
69 hexRe
: /#?([0-9a-f]{3,8})/i,
70 rgbaAltRe
: /rgba\(\s*([\w#\d]+)\s*,\s*([\d\.]+)\s*\)/,
71 rgbaRe
: /rgba\(\s*([\d\.]+)\s*,\s*([\d\.]+)\s*,\s*([\d\.]+)\s*,\s*([\d\.]+)\s*\)/,
72 rgbRe
: /rgb\(\s*([\d\.]+)\s*,\s*([\d\.]+)\s*,\s*([\d\.]+)\s*\)/,
75 * Turn a string to a color object. Supports these formats:
77 * - "#ABC" (HEX short)
79 * - "#ABCDEFDD" (HEX with opacity)
80 * - "red" (named colors - see
81 * [Web Colors](http://en.wikipedia.org/wiki/Web_colors) for a full list)
82 * - "rgba(r,g,b,a)" i.e "rgba(255,0,0,1)" (a == alpha == 0-1)
85 * - "rgba(#ABCDEF, 0.8)"
87 * @param {String} color The color string to parse.
88 * @return {Object} Object with various color properties.
89 * @return {Number} return.r The red component (0-255).
90 * @return {Number} return.g The green component (0-255).
91 * @return {Number} return.b The blue component (0-255).
92 * @return {Number} return.a The red component (0-1).
93 * @return {Number} return.h The hue component (0-1).
94 * @return {Number} return.s The saturation component (0-1).
95 * @return {Number} return.v The value component (0-1).
97 parseColor: function (color
) {
103 rgb
= me
.colorMap
[color
],
114 else if (color
=== 'transparent') {
123 match
= me
.hexRe
.exec(color
);
125 match
= match
[1]; // the captured hex
126 switch (match
.length
) {
132 //double the number (e.g. 6 - > 66, a -> aa) and convert to decimal
133 r
: parseInt(match
[0] + match
[0], 16),
134 g
: parseInt(match
[1] + match
[1], 16),
135 b
: parseInt(match
[2] + match
[2], 16),
143 r
: parseInt(match
.substr(0,2), 16),
144 g
: parseInt(match
.substr(2,2), 16),
145 b
: parseInt(match
.substr(4,2), 16),
146 a
: parseInt(match
.substr(6,2) || 'ff', 16) / 255
152 match
= me
.rgbaRe
.exec(color
);
154 // proper css => rgba(r,g,b,a)
156 r
: parseFloat(match
[1]),
157 g
: parseFloat(match
[2]),
158 b
: parseFloat(match
[3]),
159 a
: parseFloat(match
[4])
163 match
= me
.rgbaAltRe
.exec(color
);
165 // scss shorthands =? rgba(red, 0.4), rgba(#222, 0.9), rgba(#444433, 0.8)
166 ret
= me
.parseColor(match
[1]);
167 // we have HSV filled in, so poke on "a" and we're done
168 ret
.a
= parseFloat(match
[2]);
172 match
= me
.rgbRe
.exec(color
);
175 r
: parseFloat(match
[1]),
176 g
: parseFloat(match
[2]),
177 b
: parseFloat(match
[3]),
188 hsv
= this.rgb2hsv(ret
.r
, ret
.g
, ret
.b
);
190 return Ext
.apply(ret
, hsv
);
198 getRGBAString: function(rgba
) {
199 return "rgba(" + rgba
.r
+ "," + rgba
.g
+ "," + rgba
.b
+ "," + rgba
.a
+ ")";
203 * Returns a rgb css string whith this color (without the alpha channel)
207 getRGBString: function(rgb
) {
208 return "rgb(" + rgb
.r
+ "," + rgb
.g
+ "," + rgb
.b
+ ")";
212 * Following standard math to convert from hsl to rgb
213 * Check out wikipedia page for more information on how this works
219 * @return {Object} An object with "r", "g" and "b" color properties.
221 hsv2rgb: function(h
, s
, v
) {
232 var x
= c
* (1 - Math
.abs(hprime
% 2 - 1));
236 switch (Math
.floor(hprime
)) {
257 console
.error("unknown color " + h
+ ' ' + s
+ " " + v
);
268 rgb
[0] = Math
.round(rgb
[0] * 255);
269 rgb
[1] = Math
.round(rgb
[1] * 255);
270 rgb
[2] = Math
.round(rgb
[2] * 255);
280 * http://en.wikipedia.org/wiki/HSL_and_HSV
281 * @param {Number} r The red component (0-255).
282 * @param {Number} g The green component (0-255).
283 * @param {Number} b The blue component (0-255).
284 * @return {Object} An object with "h", "s" and "v" color properties.
286 rgb2hsv: function(r
,g
,b
) {
291 var M
= Math
.max(r
,g
,b
);
292 var m
= Math
.min(r
,g
,b
);
298 hprime
= ((g
- b
) / c
) % 6;
299 } else if (M
=== g
) {
300 hprime
= ((b
- r
) / c
) + 2;
301 } else if (M
=== b
) {
302 hprime
= ((r
- g
) / c
) + 4;
338 rgb2hex: function(r
, g
, b
) {
355 return (r
+ g
+ b
).toUpperCase();
359 aliceblue
: [240, 248, 255],
360 antiquewhite
: [250, 235, 215],
362 aquamarine
: [127, 255, 212],
363 azure
: [240, 255, 255],
364 beige
: [245, 245, 220],
365 bisque
: [255, 228, 196],
367 blanchedalmond
: [255, 235, 205],
369 blueviolet
: [138, 43, 226],
370 brown
: [165, 42, 42],
371 burlywood
: [222, 184, 135],
372 cadetblue
: [95, 158, 160],
373 chartreuse
: [127, 255, 0],
374 chocolate
: [210, 105, 30],
375 coral
: [255, 127, 80],
376 cornflowerblue
: [100, 149, 237],
377 cornsilk
: [255, 248, 220],
378 crimson
: [220, 20, 60],
380 darkblue
: [0, 0, 139],
381 darkcyan
: [0, 139, 139],
382 darkgoldenrod
: [184, 132, 11],
383 darkgray
: [169, 169, 169],
384 darkgreen
: [0, 100, 0],
385 darkgrey
: [169, 169, 169],
386 darkkhaki
: [189, 183, 107],
387 darkmagenta
: [139, 0, 139],
388 darkolivegreen
: [85, 107, 47],
389 darkorange
: [255, 140, 0],
390 darkorchid
: [153, 50, 204],
391 darkred
: [139, 0, 0],
392 darksalmon
: [233, 150, 122],
393 darkseagreen
: [143, 188, 143],
394 darkslateblue
: [72, 61, 139],
395 darkslategray
: [47, 79, 79],
396 darkslategrey
: [47, 79, 79],
397 darkturquoise
: [0, 206, 209],
398 darkviolet
: [148, 0, 211],
399 deeppink
: [255, 20, 147],
400 deepskyblue
: [0, 191, 255],
401 dimgray
: [105, 105, 105],
402 dimgrey
: [105, 105, 105],
403 dodgerblue
: [30, 144, 255],
404 firebrick
: [178, 34, 34],
405 floralwhite
: [255, 255, 240],
406 forestgreen
: [34, 139, 34],
407 fuchsia
: [255, 0, 255],
408 gainsboro
: [220, 220, 220],
409 ghostwhite
: [248, 248, 255],
411 goldenrod
: [218, 165, 32],
412 gray
: [128, 128, 128],
414 greenyellow
: [173, 255, 47],
415 grey
: [128, 128, 128],
416 honeydew
: [240, 255, 240],
417 hotpink
: [255, 105, 180],
418 indianred
: [205, 92, 92],
419 indigo
: [75, 0, 130],
420 ivory
: [255, 255, 240],
421 khaki
: [240, 230, 140],
422 lavender
: [230, 230, 250],
423 lavenderblush
: [255, 240, 245],
424 lawngreen
: [124, 252, 0],
425 lemonchiffon
: [255, 250, 205],
426 lightblue
: [173, 216, 230],
427 lightcoral
: [240, 128, 128],
428 lightcyan
: [224, 255, 255],
429 lightgoldenrodyellow
: [250, 250, 210],
430 lightgray
: [211, 211, 211],
431 lightgreen
: [144, 238, 144],
432 lightgrey
: [211, 211, 211],
433 lightpink
: [255, 182, 193],
434 lightsalmon
: [255, 160, 122],
435 lightseagreen
: [32, 178, 170],
436 lightskyblue
: [135, 206, 250],
437 lightslategray
: [119, 136, 153],
438 lightslategrey
: [119, 136, 153],
439 lightsteelblue
: [176, 196, 222],
440 lightyellow
: [255, 255, 224],
442 limegreen
: [50, 205, 50],
443 linen
: [250, 240, 230],
444 magenta
: [255, 0, 255],
446 mediumaquamarine
: [102, 205, 170],
447 mediumblue
: [0, 0, 205],
448 mediumorchid
: [186, 85, 211],
449 mediumpurple
: [147, 112, 219],
450 mediumseagreen
: [60, 179, 113],
451 mediumslateblue
: [123, 104, 238],
452 mediumspringgreen
: [0, 250, 154],
453 mediumturquoise
: [72, 209, 204],
454 mediumvioletred
: [199, 21, 133],
455 midnightblue
: [25, 25, 112],
456 mintcream
: [245, 255, 250],
457 mistyrose
: [255, 228, 225],
458 moccasin
: [255, 228, 181],
459 navajowhite
: [255, 222, 173],
461 oldlace
: [253, 245, 230],
462 olive
: [128, 128, 0],
463 olivedrab
: [107, 142, 35],
464 orange
: [255, 165, 0],
465 orangered
: [255, 69, 0],
466 orchid
: [218, 112, 214],
467 palegoldenrod
: [238, 232, 170],
468 palegreen
: [152, 251, 152],
469 paleturquoise
: [175, 238, 238],
470 palevioletred
: [219, 112, 147],
471 papayawhip
: [255, 239, 213],
472 peachpuff
: [255, 218, 185],
473 peru
: [205, 133, 63],
474 pink
: [255, 192, 203],
475 plum
: [221, 160, 203],
476 powderblue
: [176, 224, 230],
477 purple
: [128, 0, 128],
479 rosybrown
: [188, 143, 143],
480 royalblue
: [65, 105, 225],
481 saddlebrown
: [139, 69, 19],
482 salmon
: [250, 128, 114],
483 sandybrown
: [244, 164, 96],
484 seagreen
: [46, 139, 87],
485 seashell
: [255, 245, 238],
486 sienna
: [160, 82, 45],
487 silver
: [192, 192, 192],
488 skyblue
: [135, 206, 235],
489 slateblue
: [106, 90, 205],
490 slategray
: [119, 128, 144],
491 slategrey
: [119, 128, 144],
492 snow
: [255, 255, 250],
493 springgreen
: [0, 255, 127],
494 steelblue
: [70, 130, 180],
495 tan
: [210, 180, 140],
497 thistle
: [216, 191, 216],
498 tomato
: [255, 99, 71],
499 turquoise
: [64, 224, 208],
500 violet
: [238, 130, 238],
501 wheat
: [245, 222, 179],
502 white
: [255, 255, 255],
503 whitesmoke
: [245, 245, 245],
504 yellow
: [255, 255, 0],
505 yellowgreen
: [154, 205, 5]
509 function (ColorUtils
) {
510 var formats
= ColorUtils
.formats
,
513 formats
['#HEX6'] = function (color
) {
514 return '#' + formats
.HEX6(color
);
517 formats
['#HEX8'] = function (color
) {
518 return '#' + formats
.HEX8(color
);
521 Ext
.Object
.each(formats
, function (name
, fn
) {
522 lowerized
[name
.toLowerCase()] = function (color
) {
524 return ret
.toLowerCase();
528 Ext
.apply(formats
, lowerized
);