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