1 if(!dojo._hasResource["dojox.color._base"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.color._base"] = true;
3 dojo.provide("dojox.color._base");
4 dojo.require("dojo.colors");
6 // alias all the dojo.Color mechanisms
7 dojox.color.Color=dojo.Color;
8 dojox.color.blend=dojo.blendColors;
9 dojox.color.fromRgb=dojo.colorFromRgb;
10 dojox.color.fromHex=dojo.colorFromHex;
11 dojox.color.fromArray=dojo.colorFromArray;
12 dojox.color.fromString=dojo.colorFromString;
14 // alias the dojo.colors mechanisms
15 dojox.color.greyscale=dojo.colors.makeGrey;
18 dojo.mixin(dojox.color, {
19 fromCmy: function(/* Object|Array|int */cyan, /*int*/magenta, /*int*/yellow){
21 // Create a dojox.color.Color from a CMY defined color.
22 // All colors should be expressed as 0-100 (percentage)
24 if(dojo.isArray(cyan)){
25 magenta=cyan[1], yellow=cyan[2], cyan=cyan[0];
26 } else if(dojo.isObject(cyan)){
27 magenta=cyan.m, yellow=cyan.y, cyan=cyan.c;
29 cyan/=100, magenta/=100, yellow/=100;
31 var r=1-cyan, g=1-magenta, b=1-yellow;
32 return new dojox.color.Color({ r:Math.round(r*255), g:Math.round(g*255), b:Math.round(b*255) }); // dojox.color.Color
35 fromCmyk: function(/* Object|Array|int */cyan, /*int*/magenta, /*int*/yellow, /*int*/black){
37 // Create a dojox.color.Color from a CMYK defined color.
38 // All colors should be expressed as 0-100 (percentage)
40 if(dojo.isArray(cyan)){
41 magenta=cyan[1], yellow=cyan[2], black=cyan[3], cyan=cyan[0];
42 } else if(dojo.isObject(cyan)){
43 magenta=cyan.m, yellow=cyan.y, black=cyan.b, cyan=cyan.c;
45 cyan/=100, magenta/=100, yellow/=100, black/=100;
47 r = 1-Math.min(1, cyan*(1-black)+black);
48 g = 1-Math.min(1, magenta*(1-black)+black);
49 b = 1-Math.min(1, yellow*(1-black)+black);
50 return new dojox.color.Color({ r:Math.round(r*255), g:Math.round(g*255), b:Math.round(b*255) }); // dojox.color.Color
53 fromHsl: function(/* Object|Array|int */hue, /* int */saturation, /* int */luminosity){
55 // Create a dojox.color.Color from an HSL defined color.
56 // hue from 0-359 (degrees), saturation and luminosity 0-100.
58 if(dojo.isArray(hue)){
59 saturation=hue[1], luminosity=hue[2], hue=hue[0];
60 } else if(dojo.isObject(hue)){
61 saturation=hue.s, luminosity=hue.l, hue=hue.h;
66 while(hue<0){ hue+=360; }
67 while(hue>=360){ hue-=360; }
71 r=(120-hue)/60, g=hue/60, b=0;
73 r=0, g=(240-hue)/60, b=(hue-120)/60;
75 r=(hue-240)/60, g=0, b=(360-hue)/60;
78 r=2*saturation*Math.min(r, 1)+(1-saturation);
79 g=2*saturation*Math.min(g, 1)+(1-saturation);
80 b=2*saturation*Math.min(b, 1)+(1-saturation);
82 r*=luminosity, g*=luminosity, b*=luminosity;
84 r=(1-luminosity)*r+2*luminosity-1;
85 g=(1-luminosity)*g+2*luminosity-1;
86 b=(1-luminosity)*b+2*luminosity-1;
88 return new dojox.color.Color({ r:Math.round(r*255), g:Math.round(g*255), b:Math.round(b*255) }); // dojox.color.Color
91 fromHsv: function(/* Object|Array|int */hue, /* int */saturation, /* int */value){
93 // Create a dojox.color.Color from an HSV defined color.
94 // hue from 0-359 (degrees), saturation and value 0-100.
96 if(dojo.isArray(hue)){
97 saturation=hue[1], value=hue[2], hue=hue[0];
98 } else if (dojo.isObject(hue)){
99 saturation=hue.s, value=hue.v, hue=hue.h;
102 if(hue==360){ hue=0; }
108 r=value, b=value, g=value;
110 var hTemp=hue/60, i=Math.floor(hTemp), f=hTemp-i;
111 var p=value*(1-saturation);
112 var q=value*(1-(saturation*f));
113 var t=value*(1-(saturation*(1-f)));
115 case 0:{ r=value, g=t, b=p; break; }
116 case 1:{ r=q, g=value, b=p; break; }
117 case 2:{ r=p, g=value, b=t; break; }
118 case 3:{ r=p, g=q, b=value; break; }
119 case 4:{ r=t, g=p, b=value; break; }
120 case 5:{ r=value, g=p, b=q; break; }
123 return new dojox.color.Color({ r:Math.round(r*255), g:Math.round(g*255), b:Math.round(b*255) }); // dojox.color.Color
127 // Conversions directly on dojox.color.Color
128 dojo.extend(dojox.color.Color, {
131 // Convert this Color to a CMY definition.
132 var cyan=1-(this.r/255), magenta=1-(this.g/255), yellow=1-(this.b/255);
133 return { c:Math.round(cyan*100), m:Math.round(magenta*100), y:Math.round(yellow*100) }; // Object
138 // Convert this Color to a CMYK definition.
139 var cyan, magenta, yellow, black;
140 var r=this.r/255, g=this.g/255, b=this.b/255;
141 black = Math.min(1-r, 1-g, 1-b);
142 cyan = (1-r-black)/(1-black);
143 magenta = (1-g-black)/(1-black);
144 yellow = (1-b-black)/(1-black);
145 return { c:Math.round(cyan*100), m:Math.round(magenta*100), y:Math.round(yellow*100), b:Math.round(black*100) }; // Object
150 // Convert this Color to an HSL definition.
151 var r=this.r/255, g=this.g/255, b=this.b/255;
152 var min = Math.min(r, b, g), max = Math.max(r, g, b);
154 var h=0, s=0, l=(min+max)/2;
156 s = delta/((l<0.5)?(2*l):(2-2*l));
159 if(max==r && max!=g){
162 if(max==g && max!=b){
165 if(max==b && max!=r){
170 return { h:h, s:Math.round(s*100), l:Math.round(l*100) }; // Object
175 // Convert this Color to an HSV definition.
176 var r=this.r/255, g=this.g/255, b=this.b/255;
177 var min = Math.min(r, b, g), max = Math.max(r, g, b);
179 var h = null, s = (max==0)?0:(delta/max);
186 h = 120 + 60*(b-r)/delta;
188 h = 240 + 60*(r-g)/delta;
193 return { h:h, s:Math.round(s*100), v:Math.round(max*100) }; // Object