]>
Commit | Line | Data |
---|---|---|
2f01fe57 | 1 | /* |
81bea17a | 2 | Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved. |
2f01fe57 AD |
3 | Available via Academic Free License >= 2.1 OR the modified BSD license. |
4 | see: http://dojotoolkit.org/license for details | |
5 | */ | |
6 | ||
7 | ||
a089699c AD |
8 | if(!dojo._hasResource["dojo._base.Color"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
9 | dojo._hasResource["dojo._base.Color"] = true; | |
2f01fe57 AD |
10 | dojo.provide("dojo._base.Color"); |
11 | dojo.require("dojo._base.array"); | |
12 | dojo.require("dojo._base.lang"); | |
a089699c | 13 | |
81bea17a | 14 | |
2f01fe57 | 15 | (function(){ |
a089699c AD |
16 | |
17 | var d = dojo; | |
18 | ||
19 | dojo.Color = function(/*Array|String|Object*/ color){ | |
20 | // summary: | |
21 | // Takes a named string, hex string, array of rgb or rgba values, | |
22 | // an object with r, g, b, and a properties, or another `dojo.Color` object | |
23 | // and creates a new Color instance to work from. | |
24 | // | |
25 | // example: | |
26 | // Work with a Color instance: | |
27 | // | var c = new dojo.Color(); | |
28 | // | c.setColor([0,0,0]); // black | |
29 | // | var hex = c.toHex(); // #000000 | |
30 | // | |
31 | // example: | |
32 | // Work with a node's color: | |
33 | // | var color = dojo.style("someNode", "backgroundColor"); | |
34 | // | var n = new dojo.Color(color); | |
35 | // | // adjust the color some | |
36 | // | n.r *= .5; | |
37 | // | console.log(n.toString()); // rgb(128, 255, 255); | |
38 | if(color){ this.setColor(color); } | |
39 | }; | |
40 | ||
41 | // FIXME: | |
42 | // there's got to be a more space-efficient way to encode or discover | |
43 | // these!! Use hex? | |
44 | dojo.Color.named = { | |
45 | black: [0,0,0], | |
46 | silver: [192,192,192], | |
47 | gray: [128,128,128], | |
48 | white: [255,255,255], | |
49 | maroon: [128,0,0], | |
50 | red: [255,0,0], | |
51 | purple: [128,0,128], | |
52 | fuchsia: [255,0,255], | |
53 | green: [0,128,0], | |
54 | lime: [0,255,0], | |
55 | olive: [128,128,0], | |
56 | yellow: [255,255,0], | |
57 | navy: [0,0,128], | |
58 | blue: [0,0,255], | |
59 | teal: [0,128,128], | |
60 | aqua: [0,255,255], | |
61 | transparent: d.config.transparentColor || [255,255,255] | |
62 | }; | |
63 | ||
64 | dojo.extend(dojo.Color, { | |
65 | r: 255, g: 255, b: 255, a: 1, | |
66 | _set: function(r, g, b, a){ | |
67 | var t = this; t.r = r; t.g = g; t.b = b; t.a = a; | |
68 | }, | |
69 | setColor: function(/*Array|String|Object*/ color){ | |
70 | // summary: | |
71 | // Takes a named string, hex string, array of rgb or rgba values, | |
72 | // an object with r, g, b, and a properties, or another `dojo.Color` object | |
73 | // and sets this color instance to that value. | |
74 | // | |
75 | // example: | |
76 | // | var c = new dojo.Color(); // no color | |
77 | // | c.setColor("#ededed"); // greyish | |
78 | if(d.isString(color)){ | |
79 | d.colorFromString(color, this); | |
80 | }else if(d.isArray(color)){ | |
81 | d.colorFromArray(color, this); | |
82 | }else{ | |
83 | this._set(color.r, color.g, color.b, color.a); | |
84 | if(!(color instanceof d.Color)){ this.sanitize(); } | |
85 | } | |
86 | return this; // dojo.Color | |
87 | }, | |
88 | sanitize: function(){ | |
89 | // summary: | |
90 | // Ensures the object has correct attributes | |
91 | // description: | |
92 | // the default implementation does nothing, include dojo.colors to | |
93 | // augment it with real checks | |
94 | return this; // dojo.Color | |
95 | }, | |
96 | toRgb: function(){ | |
97 | // summary: | |
98 | // Returns 3 component array of rgb values | |
99 | // example: | |
100 | // | var c = new dojo.Color("#000000"); | |
101 | // | console.log(c.toRgb()); // [0,0,0] | |
102 | var t = this; | |
103 | return [t.r, t.g, t.b]; // Array | |
104 | }, | |
105 | toRgba: function(){ | |
106 | // summary: | |
107 | // Returns a 4 component array of rgba values from the color | |
108 | // represented by this object. | |
109 | var t = this; | |
110 | return [t.r, t.g, t.b, t.a]; // Array | |
111 | }, | |
112 | toHex: function(){ | |
113 | // summary: | |
114 | // Returns a CSS color string in hexadecimal representation | |
115 | // example: | |
116 | // | console.log(new dojo.Color([0,0,0]).toHex()); // #000000 | |
117 | var arr = d.map(["r", "g", "b"], function(x){ | |
118 | var s = this[x].toString(16); | |
119 | return s.length < 2 ? "0" + s : s; | |
120 | }, this); | |
121 | return "#" + arr.join(""); // String | |
122 | }, | |
123 | toCss: function(/*Boolean?*/ includeAlpha){ | |
124 | // summary: | |
125 | // Returns a css color string in rgb(a) representation | |
126 | // example: | |
127 | // | var c = new dojo.Color("#FFF").toCss(); | |
128 | // | console.log(c); // rgb('255','255','255') | |
129 | var t = this, rgb = t.r + ", " + t.g + ", " + t.b; | |
130 | return (includeAlpha ? "rgba(" + rgb + ", " + t.a : "rgb(" + rgb) + ")"; // String | |
131 | }, | |
132 | toString: function(){ | |
133 | // summary: | |
134 | // Returns a visual representation of the color | |
135 | return this.toCss(true); // String | |
136 | } | |
137 | }); | |
138 | ||
139 | dojo.blendColors = function( | |
140 | /*dojo.Color*/ start, | |
141 | /*dojo.Color*/ end, | |
142 | /*Number*/ weight, | |
143 | /*dojo.Color?*/ obj | |
144 | ){ | |
145 | // summary: | |
146 | // Blend colors end and start with weight from 0 to 1, 0.5 being a 50/50 blend, | |
147 | // can reuse a previously allocated dojo.Color object for the result | |
148 | var t = obj || new d.Color(); | |
149 | d.forEach(["r", "g", "b", "a"], function(x){ | |
150 | t[x] = start[x] + (end[x] - start[x]) * weight; | |
151 | if(x != "a"){ t[x] = Math.round(t[x]); } | |
152 | }); | |
153 | return t.sanitize(); // dojo.Color | |
154 | }; | |
155 | ||
156 | dojo.colorFromRgb = function(/*String*/ color, /*dojo.Color?*/ obj){ | |
157 | // summary: | |
158 | // Returns a `dojo.Color` instance from a string of the form | |
159 | // "rgb(...)" or "rgba(...)". Optionally accepts a `dojo.Color` | |
160 | // object to update with the parsed value and return instead of | |
161 | // creating a new object. | |
162 | // returns: | |
163 | // A dojo.Color object. If obj is passed, it will be the return value. | |
164 | var m = color.toLowerCase().match(/^rgba?\(([\s\.,0-9]+)\)/); | |
165 | return m && dojo.colorFromArray(m[1].split(/\s*,\s*/), obj); // dojo.Color | |
166 | }; | |
167 | ||
168 | dojo.colorFromHex = function(/*String*/ color, /*dojo.Color?*/ obj){ | |
169 | // summary: | |
170 | // Converts a hex string with a '#' prefix to a color object. | |
171 | // Supports 12-bit #rgb shorthand. Optionally accepts a | |
172 | // `dojo.Color` object to update with the parsed value. | |
173 | // | |
174 | // returns: | |
175 | // A dojo.Color object. If obj is passed, it will be the return value. | |
176 | // | |
177 | // example: | |
178 | // | var thing = dojo.colorFromHex("#ededed"); // grey, longhand | |
179 | // | |
180 | // example: | |
181 | // | var thing = dojo.colorFromHex("#000"); // black, shorthand | |
182 | var t = obj || new d.Color(), | |
183 | bits = (color.length == 4) ? 4 : 8, | |
184 | mask = (1 << bits) - 1; | |
185 | color = Number("0x" + color.substr(1)); | |
186 | if(isNaN(color)){ | |
187 | return null; // dojo.Color | |
188 | } | |
189 | d.forEach(["b", "g", "r"], function(x){ | |
190 | var c = color & mask; | |
191 | color >>= bits; | |
192 | t[x] = bits == 4 ? 17 * c : c; | |
193 | }); | |
194 | t.a = 1; | |
195 | return t; // dojo.Color | |
196 | }; | |
197 | ||
198 | dojo.colorFromArray = function(/*Array*/ a, /*dojo.Color?*/ obj){ | |
199 | // summary: | |
200 | // Builds a `dojo.Color` from a 3 or 4 element array, mapping each | |
201 | // element in sequence to the rgb(a) values of the color. | |
202 | // example: | |
81bea17a | 203 | // | var myColor = dojo.colorFromArray([237,237,237,0.5]); // grey, 50% alpha |
a089699c AD |
204 | // returns: |
205 | // A dojo.Color object. If obj is passed, it will be the return value. | |
206 | var t = obj || new d.Color(); | |
207 | t._set(Number(a[0]), Number(a[1]), Number(a[2]), Number(a[3])); | |
208 | if(isNaN(t.a)){ t.a = 1; } | |
209 | return t.sanitize(); // dojo.Color | |
210 | }; | |
211 | ||
212 | dojo.colorFromString = function(/*String*/ str, /*dojo.Color?*/ obj){ | |
213 | // summary: | |
214 | // Parses `str` for a color value. Accepts hex, rgb, and rgba | |
215 | // style color values. | |
216 | // description: | |
217 | // Acceptable input values for str may include arrays of any form | |
218 | // accepted by dojo.colorFromArray, hex strings such as "#aaaaaa", or | |
219 | // rgb or rgba strings such as "rgb(133, 200, 16)" or "rgba(10, 10, | |
220 | // 10, 50)" | |
221 | // returns: | |
222 | // A dojo.Color object. If obj is passed, it will be the return value. | |
223 | var a = d.Color.named[str]; | |
224 | return a && d.colorFromArray(a, obj) || d.colorFromRgb(str, obj) || d.colorFromHex(str, obj); | |
225 | }; | |
2f01fe57 | 226 | })(); |
a089699c | 227 | |
2f01fe57 | 228 | } |