1 if(!dojo._hasResource["dojo.colors"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo.colors"] = true;
3 dojo.provide("dojo.colors");
5 //TODO: this module appears to break naming conventions
9 // summary: Color utilities
14 // this is a standard conversion prescribed by the CSS3 Color Module
15 var hue2rgb = function(m1, m2, h){
19 if(h6 < 1){ return m1 + (m2 - m1) * h6; }
20 if(2 * h < 1){ return m2; }
21 if(3 * h < 2){ return m1 + (m2 - m1) * (2 / 3 - h) * 6; }
25 dojo.colorFromRgb = function(/*String*/ color, /*dojo.Color?*/ obj){
27 // get rgb(a) array from css-style color declarations
29 // this function can handle all 4 CSS3 Color Module formats: rgb,
30 // rgba, hsl, hsla, including rgb(a) with percentage values.
31 var m = color.toLowerCase().match(/^(rgba?|hsla?)\(([\s\.\-,%0-9]+)\)/);
33 var c = m[2].split(/\s*,\s*/), l = c.length, t = m[1];
34 if((t == "rgb" && l == 3) || (t == "rgba" && l == 4)){
36 if(r.charAt(r.length - 1) == "%"){
37 // 3 rgb percentage values
38 var a = dojo.map(c, function(x){
39 return parseFloat(x) * 2.56;
41 if(l == 4){ a[3] = c[3]; }
42 return dojo.colorFromArray(a, obj); // dojo.Color
44 return dojo.colorFromArray(c, obj); // dojo.Color
46 if((t == "hsl" && l == 3) || (t == "hsla" && l == 4)){
47 // normalize hsl values
48 var H = ((parseFloat(c[0]) % 360) + 360) % 360 / 360,
49 S = parseFloat(c[1]) / 100,
50 L = parseFloat(c[2]) / 100,
51 // calculate rgb according to the algorithm
52 // recommended by the CSS3 Color Module
53 m2 = L <= 0.5 ? L * (S + 1) : L + S - L * S,
55 a = [hue2rgb(m1, m2, H + 1 / 3) * 256,
56 hue2rgb(m1, m2, H) * 256, hue2rgb(m1, m2, H - 1 / 3) * 256, 1];
57 if(l == 4){ a[3] = c[3]; }
58 return dojo.colorFromArray(a, obj); // dojo.Color
61 return null; // dojo.Color
64 var confine = function(c, low, high){
66 // sanitize a color component by making sure it is a number,
67 // and clamping it to valid values
69 return isNaN(c) ? high : c < low ? low : c > high ? high : c; // Number
72 dojo.Color.prototype.sanitize = function(){
73 // summary: makes sure that the object has correct attributes
75 t.r = Math.round(confine(t.r, 0, 255));
76 t.g = Math.round(confine(t.g, 0, 255));
77 t.b = Math.round(confine(t.b, 0, 255));
78 t.a = confine(t.a, 0, 1);
79 return this; // dojo.Color
84 dojo.colors.makeGrey = function(/*Number*/ g, /*Number?*/ a){
85 // summary: creates a greyscale color with an optional alpha
86 return dojo.colorFromArray([g, g, g, a]);
89 // mixin all CSS3 named colors not already in _base, along with SVG 1.0 variant spellings
90 dojo.Color.named = dojo.mixin({
91 aliceblue: [240,248,255],
92 antiquewhite: [250,235,215],
93 aquamarine: [127,255,212],
96 bisque: [255,228,196],
97 blanchedalmond: [255,235,205],
98 blueviolet: [138,43,226],
100 burlywood: [222,184,135],
101 cadetblue: [95,158,160],
102 chartreuse: [127,255,0],
103 chocolate: [210,105,30],
105 cornflowerblue: [100,149,237],
106 cornsilk: [255,248,220],
107 crimson: [220,20,60],
110 darkcyan: [0,139,139],
111 darkgoldenrod: [184,134,11],
112 darkgray: [169,169,169],
113 darkgreen: [0,100,0],
114 darkgrey: [169,169,169],
115 darkkhaki: [189,183,107],
116 darkmagenta: [139,0,139],
117 darkolivegreen: [85,107,47],
118 darkorange: [255,140,0],
119 darkorchid: [153,50,204],
121 darksalmon: [233,150,122],
122 darkseagreen: [143,188,143],
123 darkslateblue: [72,61,139],
124 darkslategray: [47,79,79],
125 darkslategrey: [47,79,79],
126 darkturquoise: [0,206,209],
127 darkviolet: [148,0,211],
128 deeppink: [255,20,147],
129 deepskyblue: [0,191,255],
130 dimgray: [105,105,105],
131 dimgrey: [105,105,105],
132 dodgerblue: [30,144,255],
133 firebrick: [178,34,34],
134 floralwhite: [255,250,240],
135 forestgreen: [34,139,34],
136 gainsboro: [220,220,220],
137 ghostwhite: [248,248,255],
139 goldenrod: [218,165,32],
140 greenyellow: [173,255,47],
142 honeydew: [240,255,240],
143 hotpink: [255,105,180],
144 indianred: [205,92,92],
146 ivory: [255,255,240],
147 khaki: [240,230,140],
148 lavender: [230,230,250],
149 lavenderblush: [255,240,245],
150 lawngreen: [124,252,0],
151 lemonchiffon: [255,250,205],
152 lightblue: [173,216,230],
153 lightcoral: [240,128,128],
154 lightcyan: [224,255,255],
155 lightgoldenrodyellow: [250,250,210],
156 lightgray: [211,211,211],
157 lightgreen: [144,238,144],
158 lightgrey: [211,211,211],
159 lightpink: [255,182,193],
160 lightsalmon: [255,160,122],
161 lightseagreen: [32,178,170],
162 lightskyblue: [135,206,250],
163 lightslategray: [119,136,153],
164 lightslategrey: [119,136,153],
165 lightsteelblue: [176,196,222],
166 lightyellow: [255,255,224],
167 limegreen: [50,205,50],
168 linen: [250,240,230],
169 magenta: [255,0,255],
170 mediumaquamarine: [102,205,170],
171 mediumblue: [0,0,205],
172 mediumorchid: [186,85,211],
173 mediumpurple: [147,112,219],
174 mediumseagreen: [60,179,113],
175 mediumslateblue: [123,104,238],
176 mediumspringgreen: [0,250,154],
177 mediumturquoise: [72,209,204],
178 mediumvioletred: [199,21,133],
179 midnightblue: [25,25,112],
180 mintcream: [245,255,250],
181 mistyrose: [255,228,225],
182 moccasin: [255,228,181],
183 navajowhite: [255,222,173],
184 oldlace: [253,245,230],
185 olivedrab: [107,142,35],
187 orangered: [255,69,0],
188 orchid: [218,112,214],
189 palegoldenrod: [238,232,170],
190 palegreen: [152,251,152],
191 paleturquoise: [175,238,238],
192 palevioletred: [219,112,147],
193 papayawhip: [255,239,213],
194 peachpuff: [255,218,185],
198 powderblue: [176,224,230],
199 rosybrown: [188,143,143],
200 royalblue: [65,105,225],
201 saddlebrown: [139,69,19],
202 salmon: [250,128,114],
203 sandybrown: [244,164,96],
204 seagreen: [46,139,87],
205 seashell: [255,245,238],
207 skyblue: [135,206,235],
208 slateblue: [106,90,205],
209 slategray: [112,128,144],
210 slategrey: [112,128,144],
212 springgreen: [0,255,127],
213 steelblue: [70,130,180],
215 thistle: [216,191,216],
217 transparent: [0, 0, 0, 0],
218 turquoise: [64,224,208],
219 violet: [238,130,238],
220 wheat: [245,222,179],
221 whitesmoke: [245,245,245],
222 yellowgreen: [154,205,50]
223 }, dojo.Color.named);