r20514: implement idl for DsGetNT4ChangeLog() which transferres the meta data
[ira/wip.git] / webapps / qooxdoo-0.6.3-sdk / frontend / framework / source / class / qx / renderer / color / Color.js
1 /* ************************************************************************
2
3    qooxdoo - the new era of web development
4
5    http://qooxdoo.org
6
7    Copyright:
8      2004-2006 by 1&1 Internet AG, Germany, http://www.1and1.org
9
10    License:
11      LGPL 2.1: http://www.gnu.org/licenses/lgpl.html
12
13    Authors:
14      * Sebastian Werner (wpbasti)
15      * Andreas Ecker (ecker)
16
17 ************************************************************************ */
18
19 /* ************************************************************************
20
21 #module(ui_core)
22 #require(qx.util.Return)
23 #load(qx.renderer.color.ColorObject)
24
25 ************************************************************************ */
26
27 qx.OO.defineClass("qx.renderer.color.Color", qx.core.Object,
28 function(vValue)
29 {
30   if (qx.util.Validation.isValid(vValue)) {
31     this.setValue(vValue);
32   }
33
34   qx.core.Object.call(this);
35 });
36
37
38
39
40
41 /* ************************************************************************
42    Class data, properties and methods
43 ************************************************************************ */
44
45 /*
46 ---------------------------------------------------------------------------
47   CORE METHODS
48 ---------------------------------------------------------------------------
49 */
50
51 qx.renderer.color.Color.rgb2style = function(r, g, b) {
52   return "rgb(" + r + "," + g + "," + b + ")";
53 }
54
55
56
57
58
59 /*
60 ---------------------------------------------------------------------------
61   CORE DATA
62 ---------------------------------------------------------------------------
63 */
64
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 }
67
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*\)$/;
71
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]*%$/;
75
76 qx.renderer.color.Color.htmlNames =
77 {
78   maroon : [ 128,0,0 ],
79   red : [ 255,0,0 ],
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 ],
86   lime : [ 0,255,0 ],
87   green : [ 0,128,0 ],
88   navy : [ 0,0,128 ],
89   blue : [ 0,0,255 ],
90   aqua : [ 0,255,255 ],
91   teal : [ 0,128,128 ],
92   black : [ 0,0,0 ],
93   silver : [ 192,192,192 ],
94   gray : [ 128,128,128 ],
95   transparent : [-1,-1,-1]
96 }
97
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
100 /*
101 qx.renderer.color.Color.cssNames =
102 {
103
104 }
105 */
106
107 /*
108   ActiveBorder: Active window border.
109   ActiveCaption: Active window caption.
110
111   AppWorkspace: Background color of multiple document interface.
112   Background: Desktop background.
113
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.
118
119   CaptionText: Text in caption, size box, and scrollbar arrow box.
120   GrayText: Grayed (disabled) text.
121
122   Highlight: Item(s) selected in a control.
123   HighlightText: Text of item(s) selected in a control.
124
125   InactiveBorder: Inactive window border.
126   InactiveCaption: Inactive window caption.
127   InactiveCaptionText: Color of text in an inactive caption.
128
129   InfoBackground: Background color for tooltip controls.
130   InfoText: Text color for tooltip controls.
131
132   Menu: Menu background.
133   MenuText: Text in menus.
134
135   Scrollbar: Scroll bar gray area.
136
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.
142
143   Window: Window background.
144   WindowFrame: Window frame.
145   WindowText: Text in windows.
146 */
147
148 qx.renderer.color.Color.themedNames =
149 {
150   activeborder : 1,
151   activecaption : 1,
152   appworkspace : 1,
153   background : 1,
154   buttonface : 1,
155   buttonhighlight : 1,
156   buttonshadow : 1,
157   buttontext : 1,
158   captiontext : 1,
159   graytext : 1,
160   highlight : 1,
161   highlighttext : 1,
162   inactiveborder : 1,
163   inactivecaption : 1,
164   inactivecaptiontext : 1,
165   infobackground : 1,
166   infotext : 1,
167   menu : 1,
168   menutext : 1,
169   scrollbar : 1,
170   threeddarkshadow : 1,
171   threedface : 1,
172   threedhighlight : 1,
173   threedlightshadow : 1,
174   threedshadow : 1,
175   window : 1,
176   windowframe : 1,
177   windowtext : 1
178 }
179
180
181
182
183
184
185
186 /* ************************************************************************
187    Instance data, properties and methods
188 ************************************************************************ */
189
190 /*
191 ---------------------------------------------------------------------------
192   UTILITY
193 ---------------------------------------------------------------------------
194 */
195
196 qx.renderer.color.Color.fromString = function(vDefString) {
197   return new qx.renderer.color.Color(vDefString);
198 }
199
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())]);
202 }
203
204
205
206
207
208
209 /*
210 ---------------------------------------------------------------------------
211   DATA
212 ---------------------------------------------------------------------------
213 */
214
215 qx.Proto._value = null;
216 qx.Proto._style = null;
217
218 qx.Proto._isRgbColor = false;
219 qx.Proto._isHtmlColor = false;
220 qx.Proto._isThemedColor = false;
221
222 qx.Proto._red = null;
223 qx.Proto._green = null;
224 qx.Proto._blue = null;
225
226 qx.Proto._hex = null;
227
228
229
230
231
232 /*
233 ---------------------------------------------------------------------------
234   COMPATIBILITY METHODS
235 ---------------------------------------------------------------------------
236 */
237
238 qx.Proto.add = qx.util.Return.returnTrue;
239 qx.Proto.remove = qx.util.Return.returnTrue;
240
241
242
243
244
245
246 /*
247 ---------------------------------------------------------------------------
248   PUBLIC METHODS
249 ---------------------------------------------------------------------------
250 */
251
252 qx.Proto.isRgbColor = function() {
253   return this._isRgbColor;
254 }
255
256 qx.Proto.isHtmlColor = function() {
257   return this._isHtmlColor;
258 }
259
260 qx.Proto.isThemedColor = function() {
261   return this._isThemedColor;
262 }
263
264
265
266
267 qx.Proto.setValue = function(vValue)
268 {
269   this._normalize(vValue);
270
271   if (this._isThemedColor) {
272     throw new Error("Please use qx.renderer.color.ColorObject for themed colors!");
273   }
274 }
275
276 qx.Proto.getValue = function() {
277   return this._value || "";
278 }
279
280
281
282
283 qx.Proto.getStyle = function()
284 {
285   if (this._style == null) {
286     this._evalStyle();
287   }
288
289   return this._style;
290 }
291
292 qx.Proto._evalStyle = function()
293 {
294   if (this._isRgbColor)
295   {
296     this._style = qx.renderer.color.Color.rgb2style(this._red, this._green, this._blue);
297   }
298   else if (this._isThemedColor)
299   {
300     this._applyThemedValue();
301   }
302   else if (this._isHtmlColor)
303   {
304     this._style = this._value;
305   }
306   else if (qx.util.Validation.isValid(this._value))
307   {
308     throw new Error("_evalStyle could not handle non-rgb colors :" + this.getValue() + "!");
309   }
310 }
311
312
313
314
315 qx.Proto.getHex = function()
316 {
317   if (this._hex == null) {
318     this._evalHex();
319   }
320
321   return this._hex;
322 }
323
324 qx.Proto._evalHex = function()
325 {
326   if (this._isRgbColor)
327   {
328     var a = ["#"];
329
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)]);
333
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)]);
337
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)]);
341
342     this._hex = a.join("");
343   }
344   else
345   {
346     // TODO
347   }
348 }
349
350
351
352
353 qx.Proto.getRed = function()
354 {
355   if (this._red == null) {
356     this._evalRgb();
357   }
358
359   return this._red;
360 }
361
362 qx.Proto.getGreen = function()
363 {
364   if (this._green == null) {
365     this._evalRgb();
366   }
367
368   return this._green;
369 }
370
371 qx.Proto.getBlue = function()
372 {
373   if (this._blue == null) {
374     this._evalRgb();
375   }
376
377   return this._blue;
378 }
379
380
381
382
383 qx.Proto._evalRgb = function()
384 {
385   if (this._isThemedColor)
386   {
387     this._applyThemedValue();
388   }
389   else if (this._isHtmlColor)
390   {
391     var a = qx.renderer.color.Color.htmlNames[this._value];
392
393     this._red = a[0];
394     this._green = a[1];
395     this._blue = a[2];
396   }
397   else
398   {
399     throw new Error("_evalRgb needs implementation!");
400   }
401 }
402
403
404
405
406
407 /*
408 ---------------------------------------------------------------------------
409   PRIVATE METHODS
410 ---------------------------------------------------------------------------
411 */
412
413 qx.Proto._normalize = function(vInValue)
414 {
415   this._isThemedColor = this._isRgbColor = this._isHtmlColor = false;
416   this._hex = null;
417
418   var invalid = new Error("Invalid color: " + vInValue);
419
420   switch(typeof vInValue)
421   {
422     case "string":
423       vInValue = vInValue.toLowerCase();
424
425       if (qx.renderer.color.Color.htmlNames[vInValue])
426       {
427         this._isHtmlColor = true;
428       }
429       else if (qx.renderer.color.Color.themedNames[vInValue])
430       {
431         this._isThemedColor = true;
432       }
433       else if (qx.renderer.color.Color.r_cssrgb.test(vInValue))
434       {
435         this._red   = parseInt(RegExp.$1);
436         this._green = parseInt(RegExp.$2);
437         this._blue  = parseInt(RegExp.$3);
438
439         this._isRgbColor = true;
440       }
441       else if (qx.renderer.color.Color.r_hex3.test(vInValue))
442       {
443         this._hex = vInValue;
444
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];
448
449         this._isRgbColor = true;
450       }
451       else if (qx.renderer.color.Color.r_hex6.test(vInValue))
452       {
453         this._hex = vInValue;
454
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];
458
459         this._isRgbColor = true;
460       }
461       else
462       {
463         throw invalid;
464       }
465
466       break;
467
468     case "number":
469       if (vInValue >= 0 && vInValue <= 255)
470       {
471         this._red = this._green = this._blue = vInValue;
472         this._isRgbColor = true;
473       }
474       else
475       {
476         throw invalid;
477       }
478
479       break;
480
481     case "object":
482       if (qx.util.Validation.isValidArray(vInValue) && vInValue.length == 3)
483       {
484         this._red = vInValue[0];
485         this._green = vInValue[1];
486         this._blue = vInValue[2];
487
488         this._isRgbColor = true;
489         break;
490       }
491
492     default:
493       throw invalid;
494   }
495
496   if (!this._isRgbColor)
497   {
498     this._red = this._green = this._blue = null;
499     this._style = this._isHtmlColor ? vInValue : null;
500   }
501   else
502   {
503     this._style = null;
504
505     if (!(this._red >= 0 && this._red <= 255 && this._green >= 0 && this._green <= 255 && this._blue >= 0 && this._blue <= 255)) {
506       throw invalid;
507     }
508   }
509
510   return this._value = vInValue;
511 }
512
513
514
515
516
517
518 /*
519 ---------------------------------------------------------------------------
520   DISPOSER
521 ---------------------------------------------------------------------------
522 */
523
524 qx.Proto.dispose = function()
525 {
526   if (this.getDisposed()) {
527     return true;
528   }
529
530   delete this._value;
531   delete this._style;
532
533   delete this._red;
534   delete this._green;
535   delete this._blue;
536
537   delete this._isRgbColor;
538   delete this._isHtmlColor;
539   delete this._isThemedColor;
540
541   return qx.core.Object.prototype.dispose.call(this);
542 }