1 /* ************************************************************************
3 qooxdoo - the new era of web development
8 2004-2006 by 1&1 Internet AG, Germany, http://www.1and1.org
11 LGPL 2.1: http://www.gnu.org/licenses/lgpl.html
14 * Sebastian Werner (wpbasti)
15 * Andreas Ecker (ecker)
17 ************************************************************************ */
19 /* ************************************************************************
22 #require(qx.util.Return)
23 #load(qx.renderer.color.ColorObject)
25 ************************************************************************ */
27 qx.OO.defineClass("qx.renderer.color.Color", qx.core.Object,
30 if (qx.util.Validation.isValid(vValue)) {
31 this.setValue(vValue);
34 qx.core.Object.call(this);
41 /* ************************************************************************
42 Class data, properties and methods
43 ************************************************************************ */
46 ---------------------------------------------------------------------------
48 ---------------------------------------------------------------------------
51 qx.renderer.color.Color.rgb2style = function(r, g, b) {
52 return "rgb(" + r + "," + g + "," + b + ")";
60 ---------------------------------------------------------------------------
62 ---------------------------------------------------------------------------
65 qx.renderer.color.Color.m_hex = [ "0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f" ];
66 qx.renderer.color.Color.m_rgb = { 0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,a:10,b:11,c:12,d:13,e:14,f:15 }
68 qx.renderer.color.Color.r_hex3 = /^#([0-9a-f]{1})([0-9a-f]{1})([0-9a-f]{1})$/;
69 qx.renderer.color.Color.r_hex6 = /^#([0-9a-f]{1})([0-9a-f]{1})([0-9a-f]{1})([0-9a-f]{1})([0-9a-f]{1})([0-9a-f]{1})$/;
70 qx.renderer.color.Color.r_cssrgb = /^rgb\(\s*([0-9]{1,3}\.{0,1}[0-9]*)\s*,\s*([0-9]{1,3}\.{0,1}[0-9]*)\s*,\s*([0-9]{1,3}\.{0,1}[0-9]*)\s*\)$/;
72 qx.renderer.color.Color.r_rgb = /^[0-9]{1,3},[0-9]{1,3},[0-9]{1,3}$/;
73 qx.renderer.color.Color.r_number = /^[0-9]{1,3}\.{0,1}[0-9]*$/;
74 qx.renderer.color.Color.r_percent = /^[0-9]{1,3}\.{0,1}[0-9]*%$/;
76 qx.renderer.color.Color.htmlNames =
80 orange : [ 255,165,0 ],
81 yellow : [ 255,255,0 ],
82 olive : [ 128,128,0 ],
83 purple : [ 128,0,128 ],
84 fuchsia : [ 255,0,255 ],
85 white : [ 255,255,255 ],
93 silver : [ 192,192,192 ],
94 gray : [ 128,128,128 ],
95 transparent : [-1,-1,-1]
98 // TODO: Add some IE related colors (IE 4.x)
99 // http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/colors/colors.asp
101 qx.renderer.color.Color.cssNames =
108 ActiveBorder: Active window border.
109 ActiveCaption: Active window caption.
111 AppWorkspace: Background color of multiple document interface.
112 Background: Desktop background.
114 ButtonFace: Face color for three-dimensional display elements.
115 ButtonHighlight: Highlight color for three-dimensional display elements (for edges facing away from the light source).
116 ButtonShadow: Shadow color for three-dimensional display elements.
117 ButtonText: Text on push buttons.
119 CaptionText: Text in caption, size box, and scrollbar arrow box.
120 GrayText: Grayed (disabled) text.
122 Highlight: Item(s) selected in a control.
123 HighlightText: Text of item(s) selected in a control.
125 InactiveBorder: Inactive window border.
126 InactiveCaption: Inactive window caption.
127 InactiveCaptionText: Color of text in an inactive caption.
129 InfoBackground: Background color for tooltip controls.
130 InfoText: Text color for tooltip controls.
132 Menu: Menu background.
133 MenuText: Text in menus.
135 Scrollbar: Scroll bar gray area.
137 ThreeDDarkShadow: Dark shadow for three-dimensional display elements.
138 ThreeDFace: Face color for three-dimensional display elements.
139 ThreeDHighlight: Highlight color for three-dimensional display elements.
140 ThreeDLightShadow: Light color for three-dimensional display elements (for edges facing the light source).
141 ThreeDShadow: Dark shadow for three-dimensional display elements.
143 Window: Window background.
144 WindowFrame: Window frame.
145 WindowText: Text in windows.
148 qx.renderer.color.Color.themedNames =
164 inactivecaptiontext : 1,
170 threeddarkshadow : 1,
173 threedlightshadow : 1,
186 /* ************************************************************************
187 Instance data, properties and methods
188 ************************************************************************ */
191 ---------------------------------------------------------------------------
193 ---------------------------------------------------------------------------
196 qx.renderer.color.Color.fromString = function(vDefString) {
197 return new qx.renderer.color.Color(vDefString);
200 qx.renderer.color.Color.fromRandom = function() {
201 return new qx.renderer.color.Color([Math.round(255*Math.random()), Math.round(255*Math.random()), Math.round(255*Math.random())]);
210 ---------------------------------------------------------------------------
212 ---------------------------------------------------------------------------
215 qx.Proto._value = null;
216 qx.Proto._style = null;
218 qx.Proto._isRgbColor = false;
219 qx.Proto._isHtmlColor = false;
220 qx.Proto._isThemedColor = false;
222 qx.Proto._red = null;
223 qx.Proto._green = null;
224 qx.Proto._blue = null;
226 qx.Proto._hex = null;
233 ---------------------------------------------------------------------------
234 COMPATIBILITY METHODS
235 ---------------------------------------------------------------------------
238 qx.Proto.add = qx.util.Return.returnTrue;
239 qx.Proto.remove = qx.util.Return.returnTrue;
247 ---------------------------------------------------------------------------
249 ---------------------------------------------------------------------------
252 qx.Proto.isRgbColor = function() {
253 return this._isRgbColor;
256 qx.Proto.isHtmlColor = function() {
257 return this._isHtmlColor;
260 qx.Proto.isThemedColor = function() {
261 return this._isThemedColor;
267 qx.Proto.setValue = function(vValue)
269 this._normalize(vValue);
271 if (this._isThemedColor) {
272 throw new Error("Please use qx.renderer.color.ColorObject for themed colors!");
276 qx.Proto.getValue = function() {
277 return this._value || "";
283 qx.Proto.getStyle = function()
285 if (this._style == null) {
292 qx.Proto._evalStyle = function()
294 if (this._isRgbColor)
296 this._style = qx.renderer.color.Color.rgb2style(this._red, this._green, this._blue);
298 else if (this._isThemedColor)
300 this._applyThemedValue();
302 else if (this._isHtmlColor)
304 this._style = this._value;
306 else if (qx.util.Validation.isValid(this._value))
308 throw new Error("_evalStyle could not handle non-rgb colors :" + this.getValue() + "!");
315 qx.Proto.getHex = function()
317 if (this._hex == null) {
324 qx.Proto._evalHex = function()
326 if (this._isRgbColor)
330 var r = this.getRed();
331 a.push(qx.renderer.color.Color.m_hex[Math.floor(r/16)]);
332 a.push(qx.renderer.color.Color.m_hex[Math.floor(r%16)]);
334 var g = this.getGreen();
335 a.push(qx.renderer.color.Color.m_hex[Math.floor(g/16)]);
336 a.push(qx.renderer.color.Color.m_hex[Math.floor(g%16)]);
338 var b = this.getBlue();
339 a.push(qx.renderer.color.Color.m_hex[Math.floor(b/16)]);
340 a.push(qx.renderer.color.Color.m_hex[Math.floor(b%16)]);
342 this._hex = a.join("");
353 qx.Proto.getRed = function()
355 if (this._red == null) {
362 qx.Proto.getGreen = function()
364 if (this._green == null) {
371 qx.Proto.getBlue = function()
373 if (this._blue == null) {
383 qx.Proto._evalRgb = function()
385 if (this._isThemedColor)
387 this._applyThemedValue();
389 else if (this._isHtmlColor)
391 var a = qx.renderer.color.Color.htmlNames[this._value];
399 throw new Error("_evalRgb needs implementation!");
408 ---------------------------------------------------------------------------
410 ---------------------------------------------------------------------------
413 qx.Proto._normalize = function(vInValue)
415 this._isThemedColor = this._isRgbColor = this._isHtmlColor = false;
418 var invalid = new Error("Invalid color: " + vInValue);
420 switch(typeof vInValue)
423 vInValue = vInValue.toLowerCase();
425 if (qx.renderer.color.Color.htmlNames[vInValue])
427 this._isHtmlColor = true;
429 else if (qx.renderer.color.Color.themedNames[vInValue])
431 this._isThemedColor = true;
433 else if (qx.renderer.color.Color.r_cssrgb.test(vInValue))
435 this._red = parseInt(RegExp.$1);
436 this._green = parseInt(RegExp.$2);
437 this._blue = parseInt(RegExp.$3);
439 this._isRgbColor = true;
441 else if (qx.renderer.color.Color.r_hex3.test(vInValue))
443 this._hex = vInValue;
445 this._red = (qx.renderer.color.Color.m_rgb[RegExp.$1] * 16) + qx.renderer.color.Color.m_rgb[RegExp.$1];
446 this._green = (qx.renderer.color.Color.m_rgb[RegExp.$2] * 16) + qx.renderer.color.Color.m_rgb[RegExp.$2];
447 this._blue = (qx.renderer.color.Color.m_rgb[RegExp.$3] * 16) + qx.renderer.color.Color.m_rgb[RegExp.$3];
449 this._isRgbColor = true;
451 else if (qx.renderer.color.Color.r_hex6.test(vInValue))
453 this._hex = vInValue;
455 this._red = (qx.renderer.color.Color.m_rgb[RegExp.$1] * 16) + qx.renderer.color.Color.m_rgb[RegExp.$2];
456 this._green = (qx.renderer.color.Color.m_rgb[RegExp.$3] * 16) + qx.renderer.color.Color.m_rgb[RegExp.$4];
457 this._blue = (qx.renderer.color.Color.m_rgb[RegExp.$5] * 16) + qx.renderer.color.Color.m_rgb[RegExp.$6];
459 this._isRgbColor = true;
469 if (vInValue >= 0 && vInValue <= 255)
471 this._red = this._green = this._blue = vInValue;
472 this._isRgbColor = true;
482 if (qx.util.Validation.isValidArray(vInValue) && vInValue.length == 3)
484 this._red = vInValue[0];
485 this._green = vInValue[1];
486 this._blue = vInValue[2];
488 this._isRgbColor = true;
496 if (!this._isRgbColor)
498 this._red = this._green = this._blue = null;
499 this._style = this._isHtmlColor ? vInValue : null;
505 if (!(this._red >= 0 && this._red <= 255 && this._green >= 0 && this._green <= 255 && this._blue >= 0 && this._blue <= 255)) {
510 return this._value = vInValue;
519 ---------------------------------------------------------------------------
521 ---------------------------------------------------------------------------
524 qx.Proto.dispose = function()
526 if (this.getDisposed()) {
537 delete this._isRgbColor;
538 delete this._isHtmlColor;
539 delete this._isThemedColor;
541 return qx.core.Object.prototype.dispose.call(this);