]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/color/_base.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / color / _base.js
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");
5
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;
13
14 //      alias the dojo.colors mechanisms
15 dojox.color.greyscale=dojo.colors.makeGrey;
16
17 //      static methods
18 dojo.mixin(dojox.color, {
19         fromCmy: function(/* Object|Array|int */cyan, /*int*/magenta, /*int*/yellow){
20                 //      summary
21                 //      Create a dojox.color.Color from a CMY defined color.
22                 //      All colors should be expressed as 0-100 (percentage)
23
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;
28                 }
29                 cyan/=100, magenta/=100, yellow/=100;
30
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
33         },
34
35         fromCmyk: function(/* Object|Array|int */cyan, /*int*/magenta, /*int*/yellow, /*int*/black){
36                 //      summary
37                 //      Create a dojox.color.Color from a CMYK defined color.
38                 //      All colors should be expressed as 0-100 (percentage)
39
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;
44                 }
45                 cyan/=100, magenta/=100, yellow/=100, black/=100;
46                 var r,g,b;
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
51         },
52         
53         fromHsl: function(/* Object|Array|int */hue, /* int */saturation, /* int */luminosity){
54                 //      summary
55                 //      Create a dojox.color.Color from an HSL defined color.
56                 //      hue from 0-359 (degrees), saturation and luminosity 0-100.
57
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;
62                 }
63                 saturation/=100;
64                 luminosity/=100;
65
66                 while(hue<0){ hue+=360; }
67                 while(hue>=360){ hue-=360; }
68                 
69                 var r, g, b;
70                 if(hue<120){
71                         r=(120-hue)/60, g=hue/60, b=0;
72                 } else if (hue<240){
73                         r=0, g=(240-hue)/60, b=(hue-120)/60;
74                 } else {
75                         r=(hue-240)/60, g=0, b=(360-hue)/60;
76                 }
77                 
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);
81                 if(luminosity<0.5){
82                         r*=luminosity, g*=luminosity, b*=luminosity;
83                 }else{
84                         r=(1-luminosity)*r+2*luminosity-1;
85                         g=(1-luminosity)*g+2*luminosity-1;
86                         b=(1-luminosity)*b+2*luminosity-1;
87                 }
88                 return new dojox.color.Color({ r:Math.round(r*255), g:Math.round(g*255), b:Math.round(b*255) });        //      dojox.color.Color
89         },
90         
91         fromHsv: function(/* Object|Array|int */hue, /* int */saturation, /* int */value){
92                 //      summary
93                 //      Create a dojox.color.Color from an HSV defined color.
94                 //      hue from 0-359 (degrees), saturation and value 0-100.
95
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;
100                 }
101                 
102                 if(hue==360){ hue=0; }
103                 saturation/=100;
104                 value/=100;
105                 
106                 var r, g, b;
107                 if(saturation==0){
108                         r=value, b=value, g=value;
109                 }else{
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)));
114                         switch(i){
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; }
121                         }
122                 }
123                 return new dojox.color.Color({ r:Math.round(r*255), g:Math.round(g*255), b:Math.round(b*255) });        //      dojox.color.Color
124         }
125 });
126
127 //      Conversions directly on dojox.color.Color
128 dojo.extend(dojox.color.Color, {
129         toCmy: function(){
130                 //      summary
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
134         },
135         
136         toCmyk: function(){
137                 //      summary
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
146         },
147         
148         toHsl: function(){
149                 //      summary
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);
153                 var delta = max-min;
154                 var h=0, s=0, l=(min+max)/2;
155                 if(l>0 && l<1){
156                         s = delta/((l<0.5)?(2*l):(2-2*l));
157                 }
158                 if(delta>0){
159                         if(max==r && max!=g){
160                                 h+=(g-b)/delta;
161                         }
162                         if(max==g && max!=b){
163                                 h+=(2+(b-r)/delta);
164                         }
165                         if(max==b && max!=r){
166                                 h+=(4+(r-g)/delta);
167                         }
168                         h*=60;
169                 }
170                 return { h:h, s:Math.round(s*100), l:Math.round(l*100) };       //      Object
171         },
172
173         toHsv: function(){
174                 //      summary
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);
178                 var delta = max-min;
179                 var h = null, s = (max==0)?0:(delta/max);
180                 if(s==0){
181                         h = 0;
182                 }else{
183                         if(r==max){
184                                 h = 60*(g-b)/delta;
185                         }else if(g==max){
186                                 h = 120 + 60*(b-r)/delta;
187                         }else{
188                                 h = 240 + 60*(r-g)/delta;
189                         }
190
191                         if(h<0){ h+=360; }
192                 }
193                 return { h:h, s:Math.round(s*100), v:Math.round(max*100) };     //      Object
194         }
195 });
196
197 }