2150 |
mathias |
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");
|
|
|
6 |
|
|
|
7 |
dojo.Color = function(/*Array|String|Object*/ color){
|
|
|
8 |
// summary:
|
|
|
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); }
|
|
|
12 |
};
|
|
|
13 |
|
|
|
14 |
// FIXME: there's got to be a more space-efficient way to encode or discover these!! Use hex?
|
|
|
15 |
dojo.Color.named = {
|
|
|
16 |
black: [0,0,0],
|
|
|
17 |
silver: [192,192,192],
|
|
|
18 |
gray: [128,128,128],
|
|
|
19 |
white: [255,255,255],
|
|
|
20 |
maroon: [128,0,0],
|
|
|
21 |
red: [255,0,0],
|
|
|
22 |
purple: [128,0,128],
|
|
|
23 |
fuchsia: [255,0,255],
|
|
|
24 |
green: [0,128,0],
|
|
|
25 |
lime: [0,255,0],
|
|
|
26 |
olive: [128,128,0],
|
|
|
27 |
yellow: [255,255,0],
|
|
|
28 |
navy: [0,0,128],
|
|
|
29 |
blue: [0,0,255],
|
|
|
30 |
teal: [0,128,128],
|
|
|
31 |
aqua: [0,255,255]
|
|
|
32 |
};
|
|
|
33 |
|
|
|
34 |
|
|
|
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;
|
|
|
39 |
},
|
|
|
40 |
setColor: function(/*Array|String|Object*/ color){
|
|
|
41 |
// summary:
|
|
|
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
|
|
|
44 |
var d = dojo;
|
|
|
45 |
if(d.isString(color)){
|
|
|
46 |
d.colorFromString(color, this);
|
|
|
47 |
}else if(d.isArray(color)){
|
|
|
48 |
d.colorFromArray(color, this);
|
|
|
49 |
}else{
|
|
|
50 |
this._set(color.r, color.g, color.b, color.a);
|
|
|
51 |
if(!(color instanceof d.Color)){ this.sanitize(); }
|
|
|
52 |
}
|
|
|
53 |
return this; // dojo.Color
|
|
|
54 |
},
|
|
|
55 |
sanitize: function(){
|
|
|
56 |
// summary:
|
|
|
57 |
// makes sure that the object has correct attributes
|
|
|
58 |
// description:
|
|
|
59 |
// the default implementation does nothing, include dojo.colors to
|
|
|
60 |
// augment it to real checks
|
|
|
61 |
return this; // dojo.Color
|
|
|
62 |
},
|
|
|
63 |
toRgb: function(){
|
|
|
64 |
// summary: returns 3 component array of rgb values
|
|
|
65 |
var t = this;
|
|
|
66 |
return [t.r, t.g, t.b]; // Array
|
|
|
67 |
},
|
|
|
68 |
toRgba: function(){
|
|
|
69 |
// summary: returns a 4 component array of rgba values
|
|
|
70 |
var t = this;
|
|
|
71 |
return [t.r, t.g, t.b, t.a]; // Array
|
|
|
72 |
},
|
|
|
73 |
toHex: function(){
|
|
|
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;
|
|
|
78 |
}, this);
|
|
|
79 |
return "#" + arr.join(""); // String
|
|
|
80 |
},
|
|
|
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
|
|
|
85 |
},
|
|
|
86 |
toString: function(){
|
|
|
87 |
// summary: returns a visual representation of the color
|
|
|
88 |
return this.toCss(true); // String
|
|
|
89 |
}
|
|
|
90 |
});
|
|
|
91 |
|
|
|
92 |
dojo.blendColors = function(
|
|
|
93 |
/*dojo.Color*/ start,
|
|
|
94 |
/*dojo.Color*/ end,
|
|
|
95 |
/*Number*/ weight,
|
|
|
96 |
/*dojo.Color?*/ obj
|
|
|
97 |
){
|
|
|
98 |
// summary:
|
|
|
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]); }
|
|
|
105 |
});
|
|
|
106 |
return t.sanitize(); // dojo.Color
|
|
|
107 |
};
|
|
|
108 |
|
|
|
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
|
|
|
113 |
};
|
|
|
114 |
|
|
|
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));
|
|
|
122 |
if(isNaN(color)){
|
|
|
123 |
return null; // dojo.Color
|
|
|
124 |
}
|
|
|
125 |
d.forEach(["b", "g", "r"], function(x){
|
|
|
126 |
var c = color & mask;
|
|
|
127 |
color >>= bits;
|
|
|
128 |
t[x] = bits == 4 ? 17 * c : c;
|
|
|
129 |
});
|
|
|
130 |
t.a = 1;
|
|
|
131 |
return t; // dojo.Color
|
|
|
132 |
};
|
|
|
133 |
|
|
|
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
|
|
|
140 |
};
|
|
|
141 |
|
|
|
142 |
dojo.colorFromString = function(/*String*/ str, /*dojo.Color?*/ obj){
|
|
|
143 |
// summary:
|
|
|
144 |
// parses str for a color value.
|
|
|
145 |
// description:
|
|
|
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,
|
|
|
149 |
// 10, 50)"
|
|
|
150 |
// returns:
|
|
|
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);
|
|
|
154 |
};
|
|
|
155 |
|
|
|
156 |
}
|