1
/* This notice must be untouched at all times.
4
The latest version is available at
5
http://www.openjacob.org
7
Copyright (c) 2006 Andreas Herz. All rights reserved.
8
Created 5. 11. 2006 by Andreas Herz (Web: http://www.freegroup.de )
12
This library is free software; you can redistribute it and/or
13
modify it under the terms of the GNU Lesser General Public
14
License (LGPL) as published by the Free Software Foundation; either
15
version 2.1 of the License, or (at your option) any later version.
17
This library is distributed in the hope that it will be useful,
18
but WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
Lesser General Public License for more details.
22
You should have received a copy of the GNU Lesser General Public
23
License along with this library; if not, write to the Free Software
24
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA,
25
or see http://www.gnu.org/copyleft/lesser.html
31
* @author Andreas Herz
34
draw2d.ColorDialog=function()
37
this.maxValue={'h':'359','s':'100','v':'100'};
39
this.HSV={0:359,1:100,2:100};
41
this.slideHSV={0:359,1:100,2:100};
50
draw2d.Dialog.call(this,"Color Chooser");
53
this.setColor(new draw2d.Color(255,0,0));
55
this.setDimension(219,244);
58
draw2d.ColorDialog.prototype = new draw2d.Dialog;
60
draw2d.ColorDialog.prototype.type="ColorDialog";
66
draw2d.ColorDialog.prototype.createHTMLElement=function()
69
var item = draw2d.Dialog.prototype.createHTMLElement.call(this);
71
this.outerDiv = document.createElement("div");
72
this.outerDiv.id="plugin";
73
this.outerDiv.style.top ="15px";
74
this.outerDiv.style.left ="0px";
75
this.outerDiv.style.width="201px";
76
this.outerDiv.style.position="absolute";
77
this.outerDiv.style.padding="9px";
78
this.outerDiv.display="block";
79
this.outerDiv.style.background="#0d0d0d";
81
this.plugHEX = document.createElement("div");
82
this.plugHEX.id = "plugHEX";
83
this.plugHEX.innerHTML="F1FFCC";
84
this.plugHEX.style.color="white";
85
this.plugHEX.style.font="normal 10px verdana";
86
this.outerDiv.appendChild(this.plugHEX);
89
this.SV = document.createElement("div");
90
this.SV.onmousedown=function(event){oThis.mouseDownSV(oThis.SVslide,event);};
92
this.SV.style.cursor="crosshair";
93
this.SV.style.background="#FF0000 url(SatVal.png)";
94
this.SV.style.position="absolute";
95
this.SV.style.height="166px";
96
this.SV.style.width="167px";
97
this.SV.style.marginRight="10px";
98
this.SV.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='SatVal.png', sizingMethod='scale')";
99
this.SV.style['float']="left";
100
this.outerDiv.appendChild(this.SV);
102
this.SVslide = document.createElement("div");
103
this.SVslide.onmousedown=function(event){oThis.mouseDownSV(event)};
104
this.SVslide.style.top = "40px";
105
this.SVslide.style.left= "40px";
106
this.SVslide.style.position="absolute";
107
this.SVslide.style.cursor="crosshair";
108
this.SVslide.style.background="url(slide.gif)";
109
this.SVslide.style.height="9px";
110
this.SVslide.style.width="9px";
111
this.SVslide.style.lineHeight="1px";
112
this.outerDiv.appendChild(this.SVslide);
114
this.H = document.createElement("form");
116
this.H.onmousedown=function(event){oThis.mouseDownH(event)};
117
this.H.style.border="1px solid #000000";
118
this.H.style.cursor="crosshair";
119
this.H.style.position="absolute";
120
this.H.style.width="19px";
121
this.H.style.top="28px";
122
this.H.style.left="191px";
123
this.outerDiv.appendChild(this.H);
125
this.Hslide = document.createElement("div");
126
this.Hslide.style.top = "-7px";
127
this.Hslide.style.left= "-8px";
128
this.Hslide.style.background= "url(slideHue.gif)";
129
this.Hslide.style.height= "5px";
130
this.Hslide.style.width = "33px";
131
this.Hslide.style.position= "absolute";
132
this.Hslide.style.lineHeight= "1px";
133
this.H.appendChild(this.Hslide);
135
this.Hmodel = document.createElement("div");
136
this.Hmodel.style.height= "1px";
137
this.Hmodel.style.width= "19px";
138
this.Hmodel.style.lineHeight= "1px";
139
this.Hmodel.style.margin= "0px";
140
this.Hmodel.style.padding= "0px";
141
this.Hmodel.style.fontSize= "1px";
142
this.H.appendChild(this.Hmodel);
144
item.appendChild(this.outerDiv);
152
draw2d.ColorDialog.prototype.onOk=function()
154
draw2d.Dialog.prototype.onOk.call(this);
159
draw2d.browser=function(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
164
draw2d.ColorDialog.prototype.showColor=function(c)
166
this.plugHEX.style.background="#"+c;
167
this.plugHEX.innerHTML=c;
173
draw2d.ColorDialog.prototype.getSelectedColor=function()
175
var rgb = this.hex2rgb(this.plugHEX.innerHTML);
176
return new draw2d.Color(rgb[0],rgb[1],rgb[2]);
182
draw2d.ColorDialog.prototype.setColor=function(/*:draw2d.Color*/ color)
185
color = new draw2d.Color(100,100,100);
186
var hex = this.rgb2hex(Array(color.getRed(),color.getGreen(),color.getBlue()));
193
draw2d.ColorDialog.prototype.XY=function(e,v)
195
var z=draw2d.browser('msie')?Array(event.clientX+document.body.scrollLeft,event.clientY+document.body.scrollTop):Array(e.pageX,e.pageY);
202
draw2d.ColorDialog.prototype.mkHSV=function(a,b,c)
204
return(Math.min(a,Math.max(0,Math.ceil((parseInt(c)/b)*a))));
210
draw2d.ColorDialog.prototype.ckHSV=function(a,b)
223
draw2d.ColorDialog.prototype.mouseDownH=function(e)
225
this.slideHSV[0]=this.HSV[0];
227
this.H.onmousemove=function(e){oThis.dragH(e)};
228
this.H.onmouseup=function(e){oThis.H.onmousemove=''; oThis.H.onmouseup='';};
235
draw2d.ColorDialog.prototype.dragH=function(e)
237
var y=this.XY(e,1)-this.getY()-40;
238
this.Hslide.style.top=(this.ckHSV(y,this.wH)-5)+'px';
239
this.slideHSV[0]=this.mkHSV(359,this.wH,this.Hslide.style.top);
241
this.showColor(this.commit());
242
this.SV.style.backgroundColor='#'+this.hsv2hex(Array(this.HSV[0],100,100));
248
draw2d.ColorDialog.prototype.mouseDownSV=function(o,e)
250
this.slideHSV[0]=this.HSV[0];
254
oThis.SV.onmousemove='';
255
oThis.SV.onmouseup='';
256
oThis.SVslide.onmousemove='';
257
oThis.SVslide.onmouseup='';
259
this.SV.onmousemove=function(e){oThis.dragSV(e)};
260
this.SV.onmouseup=reset;
261
this.SVslide.onmousemove=function(e){oThis.dragSV(e)};
262
this.SVslide.onmouseup=reset;
270
draw2d.ColorDialog.prototype.dragSV=function( e)
272
var x=this.XY(e,0)-this.getX()-1;
273
var y=this.XY(e,1)-this.getY()-20;
274
this.SVslide.style.left=this.ckHSV(x,this.wSV)+'px';
275
this.SVslide.style.top=this.ckHSV(y,this.wSV)+'px';
276
this.slideHSV[1]=this.mkHSV(100,this.wSV,this.SVslide.style.left);
277
this.slideHSV[2]=100-this.mkHSV(100,this.wSV,this.SVslide.style.top);
285
draw2d.ColorDialog.prototype.commit=function()
290
for(var i=0; i<=r.length-1; i++)
293
z[i]=(j=='h')?this.maxValue[j]-this.mkHSV(this.maxValue[j],this.wH,this.Hslide.style.top):this.HSV[i];
295
return(this.updateSV(this.hsv2hex(z)));
302
draw2d.ColorDialog.prototype.updateSV=function(v)
304
this.HSV=v?this.hex2hsv(v):Array(this.slideHSV[0],this.slideHSV[1],this.slideHSV[2]);
306
v=this.hsv2hex(Array(this.slideHSV[0],this.slideHSV[1],this.slideHSV[2]));
314
draw2d.ColorDialog.prototype.loadSV=function()
317
for(var i=this.SVHeight; i>=0; i--)
318
z+="<div style=\"background:#"+this.hsv2hex(Array(Math.round((359/this.SVHeight)*i),100,100))+";\"><br/><\/div>";
319
this.Hmodel.innerHTML=z;
325
draw2d.ColorDialog.prototype.updateH=function(v)
327
this.plugHEX.innerHTML=v;
328
this.HSV=this.hex2hsv(v);
329
this.SV.style.backgroundColor='#'+this.hsv2hex(Array(this.HSV[0],100,100));
330
this.SVslide.style.top= (parseInt(this.wSV-this.wSV*(this.HSV[1]/100))+20)+'px';
331
this.SVslide.style.left=(parseInt(this.wSV*(this.HSV[1]/100))+5)+'px';
332
this.Hslide.style.top=(parseInt(this.wH*((this.maxValue['h']-this.HSV[0])/this.maxValue['h']))-7)+'px';
339
draw2d.ColorDialog.prototype.toHex=function(v)
341
v=Math.round(Math.min(Math.max(0,v),255));
342
return("0123456789ABCDEF".charAt((v-v%16)/16)+"0123456789ABCDEF".charAt(v%16));
348
draw2d.ColorDialog.prototype.hex2rgb=function(r)
351
{0:parseInt(r.substr(0,2),16),
352
1:parseInt(r.substr(2,2),16),
353
2:parseInt(r.substr(4,2),16)}
360
draw2d.ColorDialog.prototype.rgb2hex=function(r)
362
return(this.toHex(r[0])+this.toHex(r[1])+this.toHex(r[2]));
368
draw2d.ColorDialog.prototype.hsv2hex=function(h)
370
return(this.rgb2hex(this.hsv2rgb(h)));
376
draw2d.ColorDialog.prototype.hex2hsv=function(v)
378
return(this.rgb2hsv(this.hex2rgb(v)));
384
draw2d.ColorDialog.prototype.rgb2hsv=function(r)
386
var max=Math.max(r[0],r[1],r[2]);
387
var delta=max-Math.min(r[0],r[1],r[2]);
394
S=Math.round(delta/max*100);
398
H=2+(r[2]-r[0])/delta;
400
H=4+(r[0]-r[1])/delta;
401
var H=Math.min(Math.round(H*60),360);
405
return({0:H?H:0,1:S?S:0,2:Math.round((max/255)*100)});
411
draw2d.ColorDialog.prototype.hsv2rgb=function(r)
426
A=Math.round(255*V*(1.0-S));
427
B=Math.round(255*V*(1.0-(S*F)));
428
C=Math.round(255*V*(1.0-(S*(1.0-F))));
431
switch(Math.floor(H))
433
case 0: R=V; G=C; B=A; break;
434
case 1: R=B; G=V; B=A; break;
435
case 2: R=A; G=V; B=C; break;
436
case 3: R=A; G=B; B=V; break;
437
case 4: R=C; G=A; B=V; break;
438
case 5: R=V; G=A; B=B; break;
440
return({0:R?R:0,1:G?G:0,2:B?B:0});
443
return({0:(V=Math.round(V*255)),1:V,2:V});
b'\\ No newline at end of file'