Debian Codegraph @

dojox/color/Palette.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/array", "./_base"],
2 function(lang, arr, dxc){
3
4 /***************************************************************
5 * dojox.color.Palette
6 *
7 * The Palette object is loosely based on the color palettes
8 * at Kuler (http://kuler.adobe.com). They are 5 color palettes
9 * with the base color considered to be the third color in the
10 * palette (for generation purposes).
11 *
12 * Palettes can be generated from well-known algorithms or they
13 * can be manually created by passing an array to the constructor.
14 *
15 * Palettes can be transformed, using a set of specific params
16 * similar to the way shapes can be transformed with dojox.gfx.
17 * However, unlike with transformations in dojox.gfx, transforming
18 * a palette will return you a new Palette object, in effect
19 * a clone of the original.
20 ***************************************************************/
21
22 // ctor ----------------------------------------------------------------------------
23 dxc.Palette = function(/* String|Array|dojox.color.Color|dojox.color.Palette */base){
24 // summary:
25 // An object that represents a palette of colors.
26 // description:
27 // A Palette is a representation of a set of colors. While the standard
28 // number of colors contained in a palette is 5, it can really handle any
29 // number of colors.
30 //
31 // A palette is useful for the ability to transform all the colors in it
32 // using a simple object-based approach. In addition, you can generate
33 // palettes using dojox.color.Palette.generate; these generated palettes
34 // are based on the palette generators at http://kuler.adobe.com.
35
36 // colors: dojox.color.Color[]
37 // The actual color references in this palette.
38 this.colors = [];
39 if(base instanceof dxc.Palette){
40 this.colors = base.colors.slice(0);
41 }
42 else if(base instanceof dxc.Color){
43 this.colors = [ null, null, base, null, null ];
44 }
45 else if(lang.isArray(base)){
46 this.colors = arr.map(base.slice(0), function(item){
47 if(lang.isString(item)){ return new dxc.Color(item); }
48 return item;
49 });
50 }
51 else if (lang.isString(base)){
52 this.colors = [ null, null, new dxc.Color(base), null, null ];
53 }
54 }
55
56 // private functions ---------------------------------------------------------------
57
58 // transformations
59 function tRGBA(p, param, val){
60 var ret = new dxc.Palette();
61 ret.colors = [];
62 arr.forEach(p.colors, function(item){
63 var r=(param=="dr")?item.r+val:item.r,
64 g=(param=="dg")?item.g+val:item.g,
65 b=(param=="db")?item.b+val:item.b,
66 a=(param=="da")?item.a+val:item.a
67 ret.colors.push(new dxc.Color({
68 r: Math.min(255, Math.max(0, r)),
69 g: Math.min(255, Math.max(0, g)),
70 b: Math.min(255, Math.max(0, b)),
71 a: Math.min(1, Math.max(0, a))
72 }));
73 });
74 return ret;
75 }
76
77 function tCMY(p, param, val){
78 var ret = new dxc.Palette();
79 ret.colors = [];
80 arr.forEach(p.colors, function(item){
81 var o=item.toCmy(),
82 c=(param=="dc")?o.c+val:o.c,
83 m=(param=="dm")?o.m+val:o.m,
84 y=(param=="dy")?o.y+val:o.y;
85 ret.colors.push(dxc.fromCmy(
86 Math.min(100, Math.max(0, c)),
87 Math.min(100, Math.max(0, m)),
88 Math.min(100, Math.max(0, y))
89 ));
90 });
91 return ret;
92 }
93
94 function tCMYK(p, param, val){
95 var ret = new dxc.Palette();
96 ret.colors = [];
97 arr.forEach(p.colors, function(item){
98 var o=item.toCmyk(),
99 c=(param=="dc")?o.c+val:o.c,
100 m=(param=="dm")?o.m+val:o.m,
101 y=(param=="dy")?o.y+val:o.y,
102 k=(param=="dk")?o.b+val:o.b;
103 ret.colors.push(dxc.fromCmyk(
104 Math.min(100, Math.max(0, c)),
105 Math.min(100, Math.max(0, m)),
106 Math.min(100, Math.max(0, y)),
107 Math.min(100, Math.max(0, k))
108 ));
109 });
110 return ret;
111 }
112
113 function tHSL(p, param, val){
114 var ret = new dxc.Palette();
115 ret.colors = [];
116 arr.forEach(p.colors, function(item){
117 var o=item.toHsl(),
118 h=(param=="dh")?o.h+val:o.h,
119 s=(param=="ds")?o.s+val:o.s,
120 l=(param=="dl")?o.l+val:o.l;
121 ret.colors.push(dxc.fromHsl(h%360, Math.min(100, Math.max(0, s)), Math.min(100, Math.max(0, l))));
122 });
123 return ret;
124 }
125
126 function tHSV(p, param, val){
127 var ret = new dxc.Palette();
128 ret.colors = [];
129 arr.forEach(p.colors, function(item){
130 var o=item.toHsv(),
131 h=(param=="dh")?o.h+val:o.h,
132 s=(param=="ds")?o.s+val:o.s,
133 v=(param=="dv")?o.v+val:o.v;
134 ret.colors.push(dxc.fromHsv(h%360, Math.min(100, Math.max(0, s)), Math.min(100, Math.max(0, v))));
135 });
136 return ret;
137 }
138
139 // helper functions
140 function rangeDiff(val, low, high){
141 // given the value in a range from 0 to high, find the equiv
142 // using the range low to high.
143 return high-((high-val)*((high-low)/high));
144 }
145
146 /*=====
147 var __transformArgs = {
148 // summary:
149 // The keywords argument to be passed to the dojox.color.Palette.transform function. Note that
150 // while all arguments are optional, *some* arguments must be passed. The basic concept is that
151 // you pass a delta value for a specific aspect of a color model (or multiple aspects of the same
152 // color model); for instance, if you wish to transform a palette based on the HSV color model,
153 // you would pass one of "dh", "ds", or "dv" as a value.
154 // use: String?
155 // Specify the color model to use for the transformation. Can be "rgb", "rgba", "hsv", "hsl", "cmy", "cmyk".
156 // dr: Number?
157 // The delta to be applied to the red aspect of the RGB/RGBA color model.
158 // dg: Number?
159 // The delta to be applied to the green aspect of the RGB/RGBA color model.
160 // db: Number?
161 // The delta to be applied to the blue aspect of the RGB/RGBA color model.
162 // da: Number?
163 // The delta to be applied to the alpha aspect of the RGBA color model.
164 // dc: Number?
165 // The delta to be applied to the cyan aspect of the CMY/CMYK color model.
166 // dm: Number?
167 // The delta to be applied to the magenta aspect of the CMY/CMYK color model.
168 // dy: Number?
169 // The delta to be applied to the yellow aspect of the CMY/CMYK color model.
170 // dk: Number?
171 // The delta to be applied to the black aspect of the CMYK color model.
172 // dh: Number?
173 // The delta to be applied to the hue aspect of the HSL/HSV color model.
174 // ds: Number?
175 // The delta to be applied to the saturation aspect of the HSL/HSV color model.
176 // dl: Number?
177 // The delta to be applied to the luminosity aspect of the HSL color model.
178 // dv: Number?
179 // The delta to be applied to the value aspect of the HSV color model.
180 };
181 var __generatorArgs = {
182 // summary:
183 // The keyword arguments object used to create a palette based on a base color.
184 // base: dojo/_base/Color
185 // The base color to be used to generate the palette.
186 };
187 var __analogousArgs = {
188 // summary:
189 // The keyword arguments object that is used to create a 5 color palette based on the
190 // analogous rules as implemented at http://kuler.adobe.com, using the HSV color model.
191 // base: dojo/_base/Color
192 // The base color to be used to generate the palette.
193 // high: Number?
194 // The difference between the hue of the base color and the highest hue. In degrees, default is 60.
195 // low: Number?
196 // The difference between the hue of the base color and the lowest hue. In degrees, default is 18.
197 };
198 var __splitComplementaryArgs = {
199 // summary:
200 // The keyword arguments object used to create a palette based on the split complementary rules
201 // as implemented at http://kuler.adobe.com.
202 // base: dojo/_base/Color
203 // The base color to be used to generate the palette.
204 // da: Number?
205 // The delta angle to be used to determine where the split for the complementary rules happen.
206 // In degrees, the default is 30.
207 };
208 =====*/
209
210 // object methods ---------------------------------------------------------------
211 lang.extend(dxc.Palette, {
212 transform: function(/*__transformArgs*/kwArgs){
213 // summary:
214 // Transform the palette using a specific transformation function
215 // and a set of transformation parameters.
216 // description:
217 // {palette}.transform is a simple way to uniformly transform
218 // all of the colors in a palette using any of 5 formulae:
219 // RGBA, HSL, HSV, CMYK or CMY.
220 //
221 // Once the forumula to be used is determined, you can pass any
222 // number of parameters based on the formula "d"[param]; for instance,
223 // { use: "rgba", dr: 20, dg: -50 } will take all of the colors in
224 // palette, add 20 to the R value and subtract 50 from the G value.
225 //
226 // Unlike other types of transformations, transform does *not* alter
227 // the original palette but will instead return a new one.
228 var fn=tRGBA; // the default transform function.
229 if(kwArgs.use){
230 // we are being specific about the algo we want to use.
231 var use=kwArgs.use.toLowerCase();
232 if(use.indexOf("hs")==0){
233 if(use.charAt(2)=="l"){ fn=tHSL; }
234 else { fn=tHSV; }
235 }
236 else if(use.indexOf("cmy")==0){
237 if(use.charAt(3)=="k"){ fn=tCMYK; }
238 else { fn=tCMY; }
239 }
240 }
241 // try to guess the best choice.
242 else if("dc" in kwArgs || "dm" in kwArgs || "dy" in kwArgs){
243 if("dk" in kwArgs){ fn = tCMYK; }
244 else { fn = tCMY; }
245 }
246 else if("dh" in kwArgs || "ds" in kwArgs){
247 if("dv" in kwArgs){ fn = tHSV; }
248 else { fn = tHSL; }
249 }
250
251 var palette = this;
252 for(var p in kwArgs){
253 // ignore use
254 if(p=="use"){ continue; }
255 palette = fn(palette, p, kwArgs[p]);
256 }
257 return palette; // dojox.color.Palette
258 },
259 clone: function(){
260 // summary:
261 // Clones the current palette.
262 return new dxc.Palette(this); // dojox.color.Palette
263 }
264 });
265
266 lang.mixin(dxc.Palette, {
267 generators: {
268 analogous:function(/* __analogousArgs */args){
269 // summary:
270 // Create a 5 color palette based on the analogous rules as implemented at
271 // http://kuler.adobe.com.
272 var high=args.high||60, // delta between base hue and highest hue (subtracted from base)
273 low=args.low||18, // delta between base hue and lowest hue (added to base)
274 base = lang.isString(args.base)?new dxc.Color(args.base):args.base,
275 hsv=base.toHsv();
276
277 // generate our hue angle differences
278 var h=[
279 (hsv.h+low+360)%360,
280 (hsv.h+Math.round(low/2)+360)%360,
281 hsv.h,
282 (hsv.h-Math.round(high/2)+360)%360,
283 (hsv.h-high+360)%360
284 ];
285
286 var s1=Math.max(10, (hsv.s<=95)?hsv.s+5:(100-(hsv.s-95))),
287 s2=(hsv.s>1)?hsv.s-1:21-hsv.s,
288 v1=(hsv.v>=92)?hsv.v-9:Math.max(hsv.v+9, 20),
289 v2=(hsv.v<=90)?Math.max(hsv.v+5, 20):(95+Math.ceil((hsv.v-90)/2)),
290 s=[ s1, s2, hsv.s, s1, s1 ],
291 v=[ v1, v2, hsv.v, v1, v2 ]
292
293 return new dxc.Palette(arr.map(h, function(hue, i){
294 return dxc.fromHsv(hue, s[i], v[i]);
295 })); // dojox.color.Palette
296 },
297
298 monochromatic: function(/* __generatorArgs */args){
299 // summary:
300 // Create a 5 color palette based on the monochromatic rules as implemented at
301 // http://kuler.adobe.com.
302 var base = lang.isString(args.base)?new dxc.Color(args.base):args.base,
303 hsv = base.toHsv();
304
305 // figure out the saturation and value
306 var s1 = (hsv.s-30>9)?hsv.s-30:hsv.s+30,
307 s2 = hsv.s,
308 v1 = rangeDiff(hsv.v, 20, 100),
309 v2 = (hsv.v-20>20)?hsv.v-20:hsv.v+60,
310 v3 = (hsv.v-50>20)?hsv.v-50:hsv.v+30;
311
312 return new dxc.Palette([
313 dxc.fromHsv(hsv.h, s1, v1),
314 dxc.fromHsv(hsv.h, s2, v3),
315 base,
316 dxc.fromHsv(hsv.h, s1, v3),
317 dxc.fromHsv(hsv.h, s2, v2)
318 ]); // dojox.color.Palette
319 },
320
321 triadic: function(/* __generatorArgs */args){
322 // summary:
323 // Create a 5 color palette based on the triadic rules as implemented at
324 // http://kuler.adobe.com.
325 var base = lang.isString(args.base)?new dxc.Color(args.base):args.base,
326 hsv = base.toHsv();
327
328 var h1 = (hsv.h+57+360)%360,
329 h2 = (hsv.h-157+360)%360,
330 s1 = (hsv.s>20)?hsv.s-10:hsv.s+10,
331 s2 = (hsv.s>90)?hsv.s-10:hsv.s+10,
332 s3 = (hsv.s>95)?hsv.s-5:hsv.s+5,
333 v1 = (hsv.v-20>20)?hsv.v-20:hsv.v+20,
334 v2 = (hsv.v-30>20)?hsv.v-30:hsv.v+30,
335 v3 = (hsv.v-30>70)?hsv.v-30:hsv.v+30;
336
337 return new dxc.Palette([
338 dxc.fromHsv(h1, s1, hsv.v),
339 dxc.fromHsv(hsv.h, s2, v2),
340 base,
341 dxc.fromHsv(h2, s2, v1),
342 dxc.fromHsv(h2, s3, v3)
343 ]); // dojox.color.Palette
344 },
345
346 complementary: function(/* __generatorArgs */args){
347 // summary:
348 // Create a 5 color palette based on the complementary rules as implemented at
349 // http://kuler.adobe.com.
350 var base = lang.isString(args.base)?new dxc.Color(args.base):args.base,
351 hsv = base.toHsv();
352
353 var h1 = ((hsv.h*2)+137<360)?(hsv.h*2)+137:Math.floor(hsv.h/2)-137,
354 s1 = Math.max(hsv.s-10, 0),
355 s2 = rangeDiff(hsv.s, 10, 100),
356 s3 = Math.min(100, hsv.s+20),
357 v1 = Math.min(100, hsv.v+30),
358 v2 = (hsv.v>20)?hsv.v-30:hsv.v+30;
359
360 return new dxc.Palette([
361 dxc.fromHsv(hsv.h, s1, v1),
362 dxc.fromHsv(hsv.h, s2, v2),
363 base,
364 dxc.fromHsv(h1, s3, v2),
365 dxc.fromHsv(h1, hsv.s, hsv.v)
366 ]); // dojox.color.Palette
367 },
368
369 splitComplementary: function(/* __splitComplementaryArgs */args){
370 // summary:
371 // Create a 5 color palette based on the split complementary rules as implemented at
372 // http://kuler.adobe.com.
373 var base = lang.isString(args.base)?new dxc.Color(args.base):args.base,
374 dangle = args.da || 30,
375 hsv = base.toHsv();
376
377 var baseh = ((hsv.h*2)+137<360)?(hsv.h*2)+137:Math.floor(hsv.h/2)-137,
378 h1 = (baseh-dangle+360)%360,
379 h2 = (baseh+dangle)%360,
380 s1 = Math.max(hsv.s-10, 0),
381 s2 = rangeDiff(hsv.s, 10, 100),
382 s3 = Math.min(100, hsv.s+20),
383 v1 = Math.min(100, hsv.v+30),
384 v2 = (hsv.v>20)?hsv.v-30:hsv.v+30;
385
386 return new dxc.Palette([
387 dxc.fromHsv(h1, s1, v1),
388 dxc.fromHsv(h1, s2, v2),
389 base,
390 dxc.fromHsv(h2, s3, v2),
391 dxc.fromHsv(h2, hsv.s, hsv.v)
392 ]); // dojox.color.Palette
393 },
394
395 compound: function(/* __generatorArgs */args){
396 // summary:
397 // Create a 5 color palette based on the compound rules as implemented at
398 // http://kuler.adobe.com.
399 var base = lang.isString(args.base)?new dxc.Color(args.base):args.base,
400 hsv = base.toHsv();
401
402 var h1 = ((hsv.h*2)+18<360)?(hsv.h*2)+18:Math.floor(hsv.h/2)-18,
403 h2 = ((hsv.h*2)+120<360)?(hsv.h*2)+120:Math.floor(hsv.h/2)-120,
404 h3 = ((hsv.h*2)+99<360)?(hsv.h*2)+99:Math.floor(hsv.h/2)-99,
405 s1 = (hsv.s-40>10)?hsv.s-40:hsv.s+40,
406 s2 = (hsv.s-10>80)?hsv.s-10:hsv.s+10,
407 s3 = (hsv.s-25>10)?hsv.s-25:hsv.s+25,
408 v1 = (hsv.v-40>10)?hsv.v-40:hsv.v+40,
409 v2 = (hsv.v-20>80)?hsv.v-20:hsv.v+20,
410 v3 = Math.max(hsv.v, 20);
411
412 return new dxc.Palette([
413 dxc.fromHsv(h1, s1, v1),
414 dxc.fromHsv(h1, s2, v2),
415 base,
416 dxc.fromHsv(h2, s3, v3),
417 dxc.fromHsv(h3, s2, v2)
418 ]); // dojox.color.Palette
419 },
420
421 shades: function(/* __generatorArgs */args){
422 // summary:
423 // Create a 5 color palette based on the shades rules as implemented at
424 // http://kuler.adobe.com.
425 var base = lang.isString(args.base)?new dxc.Color(args.base):args.base,
426 hsv = base.toHsv();
427
428 var s = (hsv.s==100 && hsv.v==0)?0:hsv.s,
429 v1 = (hsv.v-50>20)?hsv.v-50:hsv.v+30,
430 v2 = (hsv.v-25>=20)?hsv.v-25:hsv.v+55,
431 v3 = (hsv.v-75>=20)?hsv.v-75:hsv.v+5,
432 v4 = Math.max(hsv.v-10, 20);
433
434 return new dxc.Palette([
435 new dxc.fromHsv(hsv.h, s, v1),
436 new dxc.fromHsv(hsv.h, s, v2),
437 base,
438 new dxc.fromHsv(hsv.h, s, v3),
439 new dxc.fromHsv(hsv.h, s, v4)
440 ]); // dojox.color.Palette
441 }
442 },
443 generate: function(/* String|dojox.color.Color */base, /* Function|String */type){
444 // summary:
445 // Generate a new Palette using any of the named functions in
446 // dojox.color.Palette.generators or an optional function definition. Current
447 // generators include "analogous", "monochromatic", "triadic", "complementary",
448 // "splitComplementary", and "shades".
449 if(lang.isFunction(type)){
450 return type({ base: base }); // dojox.color.Palette
451 }
452 else if(dxc.Palette.generators[type]){
453 return dxc.Palette.generators[type]({ base: base }); // dojox.color.Palette
454 }
455 throw new Error("dojox.color.Palette.generate: the specified generator ('" + type + "') does not exist.");
456 }
457 });
458
459 return dxc.Palette;
460 });