| 1 |
define(["../main", "dojo/_base/lang", "dojo/_base/Color", "dojo/colors"], |
| 2 |
function(dojox, lang, Color, colors){ |
| 3 |
|
| 4 |
var cx = lang.getObject("color", true, dojox); |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
cx.Color=Color; |
| 9 |
cx.blend=Color.blendColors; |
| 10 |
cx.fromRgb=Color.fromRgb; |
| 11 |
cx.fromHex=Color.fromHex; |
| 12 |
cx.fromArray=Color.fromArray; |
| 13 |
cx.fromString=Color.fromString; |
| 14 |
|
| 15 |
|
| 16 |
cx.greyscale=colors.makeGrey; |
| 17 |
|
| 18 |
lang.mixin(cx,{ |
| 19 |
fromCmy: function(cyan, magenta, yellow){ |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
if(lang.isArray(cyan)){ |
| 25 |
magenta=cyan[1], yellow=cyan[2], cyan=cyan[0]; |
| 26 |
} else if(lang.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 Color({ r:Math.round(r*255), g:Math.round(g*255), b:Math.round(b*255) }); |
| 33 |
}, |
| 34 |
|
| 35 |
fromCmyk: function(cyan, magenta, yellow, black){ |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
if(lang.isArray(cyan)){ |
| 41 |
magenta=cyan[1], yellow=cyan[2], black=cyan[3], cyan=cyan[0]; |
| 42 |
} else if(lang.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 Color({ r:Math.round(r*255), g:Math.round(g*255), b:Math.round(b*255) }); |
| 51 |
}, |
| 52 |
|
| 53 |
fromHsl: function(hue, saturation, luminosity){ |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
if(lang.isArray(hue)){ |
| 59 |
saturation=hue[1], luminosity=hue[2], hue=hue[0]; |
| 60 |
} else if(lang.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 Color({ r:Math.round(r*255), g:Math.round(g*255), b:Math.round(b*255) }); |
| 89 |
} |
| 90 |
}); |
| 91 |
|
| 92 |
cx.fromHsv = function(hue, saturation, value){ |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
if(lang.isArray(hue)){ |
| 98 |
saturation=hue[1], value=hue[2], hue=hue[0]; |
| 99 |
} else if (lang.isObject(hue)){ |
| 100 |
saturation=hue.s, value=hue.v, hue=hue.h; |
| 101 |
} |
| 102 |
|
| 103 |
if(hue==360){ hue=0; } |
| 104 |
saturation/=100; |
| 105 |
value/=100; |
| 106 |
|
| 107 |
var r, g, b; |
| 108 |
if(saturation==0){ |
| 109 |
r=value, b=value, g=value; |
| 110 |
}else{ |
| 111 |
var hTemp=hue/60, i=Math.floor(hTemp), f=hTemp-i; |
| 112 |
var p=value*(1-saturation); |
| 113 |
var q=value*(1-(saturation*f)); |
| 114 |
var t=value*(1-(saturation*(1-f))); |
| 115 |
switch(i){ |
| 116 |
case 0:{ r=value, g=t, b=p; break; } |
| 117 |
case 1:{ r=q, g=value, b=p; break; } |
| 118 |
case 2:{ r=p, g=value, b=t; break; } |
| 119 |
case 3:{ r=p, g=q, b=value; break; } |
| 120 |
case 4:{ r=t, g=p, b=value; break; } |
| 121 |
case 5:{ r=value, g=p, b=q; break; } |
| 122 |
} |
| 123 |
} |
| 124 |
return new Color({ r:Math.round(r*255), g:Math.round(g*255), b:Math.round(b*255) }); |
| 125 |
}; |
| 126 |
lang.extend(Color,{ |
| 127 |
toCmy: function(){ |
| 128 |
|
| 129 |
|
| 130 |
var cyan=1-(this.r/255), magenta=1-(this.g/255), yellow=1-(this.b/255); |
| 131 |
return { c:Math.round(cyan*100), m:Math.round(magenta*100), y:Math.round(yellow*100) }; |
| 132 |
}, |
| 133 |
|
| 134 |
toCmyk: function(){ |
| 135 |
|
| 136 |
|
| 137 |
var cyan, magenta, yellow, black; |
| 138 |
var r=this.r/255, g=this.g/255, b=this.b/255; |
| 139 |
black = Math.min(1-r, 1-g, 1-b); |
| 140 |
cyan = (1-r-black)/(1-black); |
| 141 |
magenta = (1-g-black)/(1-black); |
| 142 |
yellow = (1-b-black)/(1-black); |
| 143 |
return { c:Math.round(cyan*100), m:Math.round(magenta*100), y:Math.round(yellow*100), b:Math.round(black*100) }; |
| 144 |
}, |
| 145 |
|
| 146 |
toHsl: function(){ |
| 147 |
|
| 148 |
|
| 149 |
var r=this.r/255, g=this.g/255, b=this.b/255; |
| 150 |
var min = Math.min(r, b, g), max = Math.max(r, g, b); |
| 151 |
var delta = max-min; |
| 152 |
var h=0, s=0, l=(min+max)/2; |
| 153 |
if(l>0 && l<1){ |
| 154 |
s = delta/((l<0.5)?(2*l):(2-2*l)); |
| 155 |
} |
| 156 |
if(delta>0){ |
| 157 |
if(max==r && max!=g){ |
| 158 |
h+=(g-b)/delta; |
| 159 |
} |
| 160 |
if(max==g && max!=b){ |
| 161 |
h+=(2+(b-r)/delta); |
| 162 |
} |
| 163 |
if(max==b && max!=r){ |
| 164 |
h+=(4+(r-g)/delta); |
| 165 |
} |
| 166 |
h*=60; |
| 167 |
} |
| 168 |
return { h:h, s:Math.round(s*100), l:Math.round(l*100) }; |
| 169 |
}, |
| 170 |
|
| 171 |
toHsv: function(){ |
| 172 |
|
| 173 |
|
| 174 |
var r=this.r/255, g=this.g/255, b=this.b/255; |
| 175 |
var min = Math.min(r, b, g), max = Math.max(r, g, b); |
| 176 |
var delta = max-min; |
| 177 |
var h = null, s = (max==0)?0:(delta/max); |
| 178 |
if(s==0){ |
| 179 |
h = 0; |
| 180 |
}else{ |
| 181 |
if(r==max){ |
| 182 |
h = 60*(g-b)/delta; |
| 183 |
}else if(g==max){ |
| 184 |
h = 120 + 60*(b-r)/delta; |
| 185 |
}else{ |
| 186 |
h = 240 + 60*(r-g)/delta; |
| 187 |
} |
| 188 |
|
| 189 |
if(h<0){ h+=360; } |
| 190 |
} |
| 191 |
return { h:h, s:Math.round(s*100), v:Math.round(max*100) }; |
| 192 |
} |
| 193 |
}); |
| 194 |
|
| 195 |
return cx; |
| 196 |
}); |