initial import
[roojs1] / Roo / lib / ColorAnim.js
1 /*
2  * Portions of this file are based on pieces of Yahoo User Interface Library
3  * Copyright (c) 2007, Yahoo! Inc. All rights reserved.
4  * YUI licensed under the BSD License:
5  * http://developer.yahoo.net/yui/license.txt
6  * <script type="text/javascript">
7  *
8  */
9 (function() {
10
11     Roo.lib.ColorAnim = function(el, attributes, duration, method) {
12         Roo.lib.ColorAnim.superclass.constructor.call(this, el, attributes, duration, method);
13     };
14
15     Roo.extend(Roo.lib.ColorAnim, Roo.lib.AnimBase);
16
17     var fly = Roo.lib.AnimBase.fly;
18     var Y = Roo.lib;
19     var superclass = Y.ColorAnim.superclass;
20     var proto = Y.ColorAnim.prototype;
21
22     proto.toString = function() {
23         var el = this.getEl();
24         var id = el.id || el.tagName;
25         return ("ColorAnim " + id);
26     };
27
28     proto.patterns.color = /color$/i;
29     proto.patterns.rgb = /^rgb\(([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\)$/i;
30     proto.patterns.hex = /^#?([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i;
31     proto.patterns.hex3 = /^#?([0-9A-F]{1})([0-9A-F]{1})([0-9A-F]{1})$/i;
32     proto.patterns.transparent = /^transparent|rgba\(0, 0, 0, 0\)$/;
33
34
35     proto.parseColor = function(s) {
36         if (s.length == 3) {
37             return s;
38         }
39
40         var c = this.patterns.hex.exec(s);
41         if (c && c.length == 4) {
42             return [ parseInt(c[1], 16), parseInt(c[2], 16), parseInt(c[3], 16) ];
43         }
44
45         c = this.patterns.rgb.exec(s);
46         if (c && c.length == 4) {
47             return [ parseInt(c[1], 10), parseInt(c[2], 10), parseInt(c[3], 10) ];
48         }
49
50         c = this.patterns.hex3.exec(s);
51         if (c && c.length == 4) {
52             return [ parseInt(c[1] + c[1], 16), parseInt(c[2] + c[2], 16), parseInt(c[3] + c[3], 16) ];
53         }
54
55         return null;
56     };
57     // since this uses fly! - it cant be in ColorAnim (which does not have fly yet..)
58     proto.getAttribute = function(attr) {
59         var el = this.getEl();
60         if (this.patterns.color.test(attr)) {
61             var val = fly(el).getStyle(attr);
62
63             if (this.patterns.transparent.test(val)) {
64                 var parent = el.parentNode;
65                 val = fly(parent).getStyle(attr);
66
67                 while (parent && this.patterns.transparent.test(val)) {
68                     parent = parent.parentNode;
69                     val = fly(parent).getStyle(attr);
70                     if (parent.tagName.toUpperCase() == 'HTML') {
71                         val = '#fff';
72                     }
73                 }
74             }
75         } else {
76             val = superclass.getAttribute.call(this, attr);
77         }
78
79         return val;
80     };
81     proto.getAttribute = function(attr) {
82         var el = this.getEl();
83         if (this.patterns.color.test(attr)) {
84             var val = fly(el).getStyle(attr);
85
86             if (this.patterns.transparent.test(val)) {
87                 var parent = el.parentNode;
88                 val = fly(parent).getStyle(attr);
89
90                 while (parent && this.patterns.transparent.test(val)) {
91                     parent = parent.parentNode;
92                     val = fly(parent).getStyle(attr);
93                     if (parent.tagName.toUpperCase() == 'HTML') {
94                         val = '#fff';
95                     }
96                 }
97             }
98         } else {
99             val = superclass.getAttribute.call(this, attr);
100         }
101
102         return val;
103     };
104
105     proto.doMethod = function(attr, start, end) {
106         var val;
107
108         if (this.patterns.color.test(attr)) {
109             val = [];
110             for (var i = 0, len = start.length; i < len; ++i) {
111                 val[i] = superclass.doMethod.call(this, attr, start[i], end[i]);
112             }
113
114             val = 'rgb(' + Math.floor(val[0]) + ',' + Math.floor(val[1]) + ',' + Math.floor(val[2]) + ')';
115         }
116         else {
117             val = superclass.doMethod.call(this, attr, start, end);
118         }
119
120         return val;
121     };
122
123     proto.setRuntimeAttribute = function(attr) {
124         superclass.setRuntimeAttribute.call(this, attr);
125
126         if (this.patterns.color.test(attr)) {
127             var attributes = this.attributes;
128             var start = this.parseColor(this.runtimeAttributes[attr].start);
129             var end = this.parseColor(this.runtimeAttributes[attr].end);
130
131             if (typeof attributes[attr]['to'] === 'undefined' && typeof attributes[attr]['by'] !== 'undefined') {
132                 end = this.parseColor(attributes[attr].by);
133
134                 for (var i = 0, len = start.length; i < len; ++i) {
135                     end[i] = start[i] + end[i];
136                 }
137             }
138
139             this.runtimeAttributes[attr].start = start;
140             this.runtimeAttributes[attr].end = end;
141         }
142     };
143 })();
144