1 if(!dojo._hasResource["dojo._base.Color"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo._base.Color"] = true;
3 dojo.provide("dojo._base.Color");
4 dojo.require("dojo._base.array");
5 dojo.require("dojo._base.lang");
7 dojo.Color = function(/*Array|String|Object*/ color){
9 // takes a named string, hex string, array of rgb or rgba values,
10 // an object with r, g, b, and a properties, or another dojo.Color object
11 if(color){ this.setColor(color); }
14 // FIXME: there's got to be a more space-efficient way to encode or discover these!! Use hex?
17 silver: [192,192,192],
35 dojo.extend(dojo.Color, {
36 r: 255, g: 255, b: 255, a: 1,
37 _set: function(r, g, b, a){
38 var t = this; t.r = r; t.g = g; t.b = b; t.a = a;
40 setColor: function(/*Array|String|Object*/ color){
42 // takes a named string, hex string, array of rgb or rgba values,
43 // an object with r, g, b, and a properties, or another dojo.Color object
45 if(d.isString(color)){
46 d.colorFromString(color, this);
47 }else if(d.isArray(color)){
48 d.colorFromArray(color, this);
50 this._set(color.r, color.g, color.b, color.a);
51 if(!(color instanceof d.Color)){ this.sanitize(); }
53 return this; // dojo.Color
57 // makes sure that the object has correct attributes
59 // the default implementation does nothing, include dojo.colors to
60 // augment it to real checks
61 return this; // dojo.Color
64 // summary: returns 3 component array of rgb values
66 return [t.r, t.g, t.b]; // Array
69 // summary: returns a 4 component array of rgba values
71 return [t.r, t.g, t.b, t.a]; // Array
74 // summary: returns a css color string in hexadecimal representation
75 var arr = dojo.map(["r", "g", "b"], function(x){
76 var s = this[x].toString(16);
77 return s.length < 2 ? "0" + s : s;
79 return "#" + arr.join(""); // String
81 toCss: function(/*Boolean?*/ includeAlpha){
82 // summary: returns a css color string in rgb(a) representation
83 var t = this, rgb = t.r + ", " + t.g + ", " + t.b;
84 return (includeAlpha ? "rgba(" + rgb + ", " + t.a : "rgb(" + rgb) + ")"; // String
87 // summary: returns a visual representation of the color
88 return this.toCss(true); // String
92 dojo.blendColors = function(
99 // blend colors end and start with weight from 0 to 1, 0.5 being a 50/50 blend,
100 // can reuse a previously allocated dojo.Color object for the result
101 var d = dojo, t = obj || new dojo.Color();
102 d.forEach(["r", "g", "b", "a"], function(x){
103 t[x] = start[x] + (end[x] - start[x]) * weight;
104 if(x != "a"){ t[x] = Math.round(t[x]); }
106 return t.sanitize(); // dojo.Color
109 dojo.colorFromRgb = function(/*String*/ color, /*dojo.Color?*/ obj){
110 // summary: get rgb(a) array from css-style color declarations
111 var m = color.toLowerCase().match(/^rgba?\(([\s\.,0-9]+)\)/);
112 return m && dojo.colorFromArray(m[1].split(/\s*,\s*/), obj); // dojo.Color
115 dojo.colorFromHex = function(/*String*/ color, /*dojo.Color?*/ obj){
116 // summary: converts a hex string with a '#' prefix to a color object.
117 // Supports 12-bit #rgb shorthand.
118 var d = dojo, t = obj || new d.Color(),
119 bits = (color.length == 4) ? 4 : 8,
120 mask = (1 << bits) - 1;
121 color = Number("0x" + color.substr(1));
123 return null; // dojo.Color
125 d.forEach(["b", "g", "r"], function(x){
126 var c = color & mask;
128 t[x] = bits == 4 ? 17 * c : c;
131 return t; // dojo.Color
134 dojo.colorFromArray = function(/*Array*/ a, /*dojo.Color?*/ obj){
135 // summary: builds a color from 1, 2, 3, or 4 element array
136 var t = obj || new dojo.Color();
137 t._set(Number(a[0]), Number(a[1]), Number(a[2]), Number(a[3]));
138 if(isNaN(t.a)){ t.a = 1; }
139 return t.sanitize(); // dojo.Color
142 dojo.colorFromString = function(/*String*/ str, /*dojo.Color?*/ obj){
144 // parses str for a color value.
146 // Acceptable input values for str may include arrays of any form
147 // accepted by dojo.colorFromArray, hex strings such as "#aaaaaa", or
148 // rgb or rgba strings such as "rgb(133, 200, 16)" or "rgba(10, 10,
151 // a dojo.Color object. If obj is passed, it will be the return value.
152 var a = dojo.Color.named[str];
153 return a && dojo.colorFromArray(a, obj) || dojo.colorFromRgb(str, obj) || dojo.colorFromHex(str, obj);