Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
//-------------------- ricoColor.js
2
if(typeof(Rico) == "undefined") Rico = {};
3
 
4
Rico.Color = Class.create();
5
 
6
Rico.Color.prototype = {
7
 
8
   initialize: function(red, green, blue) {
9
      this.rgb = { r: red, g : green, b : blue };
10
   },
11
 
12
   setRed: function(r) {
13
      this.rgb.r = r;
14
   },
15
 
16
   setGreen: function(g) {
17
      this.rgb.g = g;
18
   },
19
 
20
   setBlue: function(b) {
21
      this.rgb.b = b;
22
   },
23
 
24
   setHue: function(h) {
25
 
26
      // get an HSB model, and set the new hue...
27
      var hsb = this.asHSB();
28
      hsb.h = h;
29
 
30
      // convert back to RGB...
31
      this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b);
32
   },
33
 
34
   setSaturation: function(s) {
35
      // get an HSB model, and set the new hue...
36
      var hsb = this.asHSB();
37
      hsb.s = s;
38
 
39
      // convert back to RGB and set values...
40
      this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b);
41
   },
42
 
43
   setBrightness: function(b) {
44
      // get an HSB model, and set the new hue...
45
      var hsb = this.asHSB();
46
      hsb.b = b;
47
 
48
      // convert back to RGB and set values...
49
      this.rgb = Rico.Color.HSBtoRGB( hsb.h, hsb.s, hsb.b );
50
   },
51
 
52
   darken: function(percent) {
53
      var hsb  = this.asHSB();
54
      this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.max(hsb.b - percent,0));
55
   },
56
 
57
   brighten: function(percent) {
58
      var hsb  = this.asHSB();
59
      this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.min(hsb.b + percent,1));
60
   },
61
 
62
   blend: function(other) {
63
      this.rgb.r = Math.floor((this.rgb.r + other.rgb.r)/2);
64
      this.rgb.g = Math.floor((this.rgb.g + other.rgb.g)/2);
65
      this.rgb.b = Math.floor((this.rgb.b + other.rgb.b)/2);
66
   },
67
 
68
   isBright: function() {
69
      var hsb = this.asHSB();
70
      return this.asHSB().b > 0.5;
71
   },
72
 
73
   isDark: function() {
74
      return ! this.isBright();
75
   },
76
 
77
   asRGB: function() {
78
      return "rgb(" + this.rgb.r + "," + this.rgb.g + "," + this.rgb.b + ")";
79
   },
80
 
81
   asHex: function() {
82
      return "#" + this.rgb.r.toColorPart() + this.rgb.g.toColorPart() + this.rgb.b.toColorPart();
83
   },
84
 
85
   asHSB: function() {
86
      return Rico.Color.RGBtoHSB(this.rgb.r, this.rgb.g, this.rgb.b);
87
   },
88
 
89
   toString: function() {
90
      return this.asHex();
91
   }
92
 
93
};
94
 
95
Rico.Color.createFromHex = function(hexCode) {
96
 
97
   if ( hexCode.indexOf('#') == 0 )
98
      hexCode = hexCode.substring(1);
99
 
100
   var red = "ff", green = "ff", blue="ff";
101
   if(hexCode.length > 4)
102
	{
103
	   red   = hexCode.substring(0,2);
104
	   green = hexCode.substring(2,4);
105
	   blue  = hexCode.substring(4,6);
106
	}
107
	else if(hexCode.length > 0 & hexCode.length < 4)
108
	{
109
	  var r = hexCode.substring(0,1);
110
	  var g = hexCode.substring(1,2);
111
	  var b = hexCode.substring(2);
112
	  red = r+r;
113
	  green = g+g;
114
	  blue = b+b;
115
	}
116
   return new Rico.Color( parseInt(red,16), parseInt(green,16), parseInt(blue,16) );
117
};
118
 
119
/**
120
 * Factory method for creating a color from the background of
121
 * an HTML element.
122
 */
123
Rico.Color.createColorFromBackground = function(elem) {
124
 
125
   var actualColor = Element.getStyle($(elem), "background-color");
126
  if ( actualColor == "transparent" && elem.parent )
127
      return Rico.Color.createColorFromBackground(elem.parent);
128
 
129
   if ( actualColor == null )
130
      return new Rico.Color(255,255,255);
131
 
132
   if ( actualColor.indexOf("rgb(") == 0 ) {
133
      var colors = actualColor.substring(4, actualColor.length - 1 );
134
      var colorArray = colors.split(",");
135
      return new Rico.Color( parseInt( colorArray[0] ),
136
                            parseInt( colorArray[1] ),
137
                            parseInt( colorArray[2] )  );
138
 
139
   }
140
   else if ( actualColor.indexOf("#") == 0 ) {
141
	  return Rico.Color.createFromHex(actualColor);
142
   }
143
   else
144
      return new Rico.Color(255,255,255);
145
};
146
 
147
Rico.Color.HSBtoRGB = function(hue, saturation, brightness) {
148
 
149
   var red   = 0;
150
	var green = 0;
151
	var blue  = 0;
152
 
153
   if (saturation == 0) {
154
      red = parseInt(brightness * 255.0 + 0.5);
155
	   green = red;
156
	   blue = red;
157
	}
158
	else {
159
      var h = (hue - Math.floor(hue)) * 6.0;
160
      var f = h - Math.floor(h);
161
      var p = brightness * (1.0 - saturation);
162
      var q = brightness * (1.0 - saturation * f);
163
      var t = brightness * (1.0 - (saturation * (1.0 - f)));
164
 
165
      switch (parseInt(h)) {
166
         case 0:
167
            red   = (brightness * 255.0 + 0.5);
168
            green = (t * 255.0 + 0.5);
169
            blue  = (p * 255.0 + 0.5);
170
            break;
171
         case 1:
172
            red   = (q * 255.0 + 0.5);
173
            green = (brightness * 255.0 + 0.5);
174
            blue  = (p * 255.0 + 0.5);
175
            break;
176
         case 2:
177
            red   = (p * 255.0 + 0.5);
178
            green = (brightness * 255.0 + 0.5);
179
            blue  = (t * 255.0 + 0.5);
180
            break;
181
         case 3:
182
            red   = (p * 255.0 + 0.5);
183
            green = (q * 255.0 + 0.5);
184
            blue  = (brightness * 255.0 + 0.5);
185
            break;
186
         case 4:
187
            red   = (t * 255.0 + 0.5);
188
            green = (p * 255.0 + 0.5);
189
            blue  = (brightness * 255.0 + 0.5);
190
            break;
191
          case 5:
192
            red   = (brightness * 255.0 + 0.5);
193
            green = (p * 255.0 + 0.5);
194
            blue  = (q * 255.0 + 0.5);
195
            break;
196
	    }
197
	}
198
 
199
   return { r : parseInt(red), g : parseInt(green) , b : parseInt(blue) };
200
};
201
 
202
Rico.Color.RGBtoHSB = function(r, g, b) {
203
 
204
   var hue;
205
   var saturaton;
206
   var brightness;
207
 
208
   var cmax = (r > g) ? r : g;
209
   if (b > cmax)
210
      cmax = b;
211
 
212
   var cmin = (r < g) ? r : g;
213
   if (b < cmin)
214
      cmin = b;
215
 
216
   brightness = cmax / 255.0;
217
   if (cmax != 0)
218
      saturation = (cmax - cmin)/cmax;
219
   else
220
      saturation = 0;
221
 
222
   if (saturation == 0)
223
      hue = 0;
224
   else {
225
      var redc   = (cmax - r)/(cmax - cmin);
226
    	var greenc = (cmax - g)/(cmax - cmin);
227
    	var bluec  = (cmax - b)/(cmax - cmin);
228
 
229
    	if (r == cmax)
230
    	   hue = bluec - greenc;
231
    	else if (g == cmax)
232
    	   hue = 2.0 + redc - bluec;
233
      else
234
    	   hue = 4.0 + greenc - redc;
235
 
236
    	hue = hue / 6.0;
237
    	if (hue < 0)
238
    	   hue = hue + 1.0;
239
   }
240
 
241
   return { h : hue, s : saturation, b : brightness };
242
};
243
 
244
 
245
Prado.WebUI.TColorPicker = Class.create();
246
 
247
Object.extend(Prado.WebUI.TColorPicker,
248
{
249
	palettes:
250
	{
251
		Small : [["fff", "fcc", "fc9", "ff9", "ffc", "9f9", "9ff", "cff", "ccf", "fcf"],
252
				["ccc", "f66", "f96", "ff6", "ff3", "6f9", "3ff", "6ff", "99f", "f9f"],
253
				["c0c0c0", "f00", "f90", "fc6", "ff0", "3f3", "6cc", "3cf", "66c", "c6c"],
254
				["999", "c00", "f60", "fc3", "fc0", "3c0", "0cc", "36f", "63f", "c3c"],
255
				["666", "900", "c60", "c93", "990", "090", "399", "33f", "60c", "939"],
256
				["333", "600", "930", "963", "660", "060", "366", "009", "339", "636"],
257
				["000", "300", "630", "633", "330", "030", "033", "006", "309", "303"]],
258
 
259
		Tiny : [["ffffff"/*white*/, "00ff00"/*lime*/, "008000"/*green*/, "0000ff"/*blue*/],
260
				["c0c0c0"/*silver*/, "ffff00"/*yellow*/, "ff00ff"/*fuchsia*/, "000080"/*navy*/],
261
				["808080"/*gray*/, "ff0000"/*red*/, "800080"/*purple*/, "000000"/*black*/]]
262
	},
263
 
264
	UIImages :
265
	{
266
		'button.gif' : 'button.gif',
267
//		'target_black.gif' : 'target_black.gif',
268
//		'target_white.gif' : 'target_white.gif',
269
		'background.png' : 'background.png'
270
//		'slider.gif' : 'slider.gif',
271
//		'hue.gif' : 'hue.gif'
272
	}
273
});
274
 
275
Object.extend(Prado.WebUI.TColorPicker.prototype,
276
{
277
	initialize : function(options)
278
	{
279
		var basics =
280
		{
281
			Palette : 'Small',
282
			ClassName : 'TColorPicker',
283
			Mode : 'Basic',
284
			OKButtonText : 'OK',
285
			CancelButtonText : 'Cancel',
286
			ShowColorPicker : true
287
		}
288
 
289
		this.element = null;
290
		this.showing = false;
291
 
292
		options = Object.extend(basics, options);
293
		this.options = options;
294
		this.input = $(options['ID']);
295
		this.button = $(options['ID']+'_button');
296
		this._buttonOnClick = this.buttonOnClick.bind(this);
297
		if(options['ShowColorPicker'])
298
			Event.observe(this.button, "click", this._buttonOnClick);
299
		Event.observe(this.input, "change", this.updatePicker.bind(this));
300
	},
301
 
302
	updatePicker : function(e)
303
	{
304
		var color = Rico.Color.createFromHex(this.input.value);
305
		this.button.style.backgroundColor = color.toString();
306
	},
307
 
308
	buttonOnClick : function(event)
309
	{
310
		var mode = this.options['Mode'];
311
		if(this.element == null)
312
		{
313
			var constructor = mode == "Basic" ? "getBasicPickerContainer": "getFullPickerContainer"
314
			this.element = this[constructor](this.options['ID'], this.options['Palette'])
315
			this.input.parentNode.appendChild(this.element);
316
			this.element.style.display = "none";
317
 
318
			if(Prado.Browser().ie)
319
			{
320
				this.iePopUp = document.createElement('iframe');
321
				this.iePopUp.src = Prado.WebUI.TColorPicker.UIImages['button.gif'];
322
				this.iePopUp.style.position = "absolute"
323
				this.iePopUp.scrolling="no"
324
				this.iePopUp.frameBorder="0"
325
				this.input.parentNode.appendChild(this.iePopUp);
326
			}
327
			if(mode == "Full")
328
				this.initializeFullPicker();
329
		}
330
		this.show(mode);
331
	},
332
 
333
	show : function(type)
334
	{
335
		if(!this.showing)
336
		{
337
			var pos = this.input.positionedOffset();
338
			pos[1] += this.input.offsetHeight;
339
 
340
			this.element.style.top = (pos[1]-1) + "px";
341
			this.element.style.left = pos[0] + "px";
342
			this.element.style.display = "block";
343
 
344
			this.ieHack(type);
345
 
346
			//observe for clicks on the document body
347
			this._documentClickEvent = this.hideOnClick.bindEvent(this, type);
348
			this._documentKeyDownEvent = this.keyPressed.bindEvent(this, type);
349
			Event.observe(document.body, "click", this._documentClickEvent);
350
			Event.observe(document,"keydown", this._documentKeyDownEvent);
351
			this.showing = true;
352
 
353
			if(type == "Full")
354
			{
355
				this.observeMouseMovement();
356
				var color = Rico.Color.createFromHex(this.input.value);
357
				this.inputs.oldColor.style.backgroundColor = color.asHex();
358
				this.setColor(color,true);
359
			}
360
		}
361
	},
362
 
363
	hide : function(event)
364
	{
365
		if(this.showing)
366
		{
367
			if(this.iePopUp)
368
				this.iePopUp.style.display = "none";
369
 
370
			this.element.style.display = "none";
371
			this.showing = false;
372
			Event.stopObserving(document.body, "click", this._documentClickEvent);
373
			Event.stopObserving(document,"keydown", this._documentKeyDownEvent);
374
 
375
			if(this._observingMouseMove)
376
			{
377
				Event.stopObserving(document.body, "mousemove", this._onMouseMove);
378
				this._observingMouseMove = false;
379
			}
380
		}
381
	},
382
 
383
	keyPressed : function(event,type)
384
	{
385
		if(Event.keyCode(event) == Event.KEY_ESC)
386
			this.hide(event,type);
387
	},
388
 
389
	hideOnClick : function(ev)
390
	{
391
		if(!this.showing) return;
392
		var el = Event.element(ev);
393
		var within = false;
394
		do
395
		{	within = within || String(el.className).indexOf('FullColorPicker') > -1
396
			within = within || el == this.button;
397
			within = within || el == this.input;
398
			if(within) break;
399
			el = el.parentNode;
400
		}
401
		while(el);
402
		if(!within) this.hide(ev);
403
	},
404
 
405
	ieHack : function()
406
	{
407
		// IE hack
408
		if(this.iePopUp)
409
		{
410
			this.iePopUp.style.display = "block";
411
			this.iePopUp.style.top = (this.element.offsetTop) + "px";
412
			this.iePopUp.style.left = (this.element.offsetLeft)+ "px";
413
			this.iePopUp.style.width = Math.abs(this.element.offsetWidth)+ "px";
414
			this.iePopUp.style.height = (this.element.offsetHeight + 1)+ "px";
415
		}
416
	},
417
 
418
	getBasicPickerContainer : function(pickerID, palette)
419
	{
420
		var table = TABLE({className:'basic_colors palette_'+palette},TBODY());
421
		var colors = Prado.WebUI.TColorPicker.palettes[palette];
422
		var pickerOnClick = this.cellOnClick.bind(this);
423
		colors.each(function(color)
424
		{
425
			var row = document.createElement("tr");
426
			color.each(function(c)
427
			{
428
				var td = document.createElement("td");
429
				var img = IMG({src:Prado.WebUI.TColorPicker.UIImages['button.gif'],width:16,height:16});
430
				img.style.backgroundColor = "#"+c;
431
				Event.observe(img,"click", pickerOnClick);
432
				Event.observe(img,"mouseover", function(e)
433
				{
434
					Element.addClassName(Event.element(e), "pickerhover");
435
				});
436
				Event.observe(img,"mouseout", function(e)
437
				{
438
					Element.removeClassName(Event.element(e), "pickerhover");
439
				});
440
				td.appendChild(img);
441
				row.appendChild(td);
442
			});
443
			table.childNodes[0].appendChild(row);
444
		});
445
		return DIV({className:this.options['ClassName']+" BasicColorPicker",
446
					id:pickerID+"_picker"}, table);
447
	},
448
 
449
	cellOnClick : function(e)
450
	{
451
		var el = Event.element(e);
452
		if(el.tagName.toLowerCase() != "img")
453
			return;
454
		var color = Rico.Color.createColorFromBackground(el);
455
		this.updateColor(color);
456
	},
457
 
458
	updateColor : function(color)
459
	{
460
		this.input.value = color.toString().toUpperCase();
461
		this.button.style.backgroundColor = color.toString();
462
		if(typeof(this.onChange) == "function")
463
			this.onChange(color);
464
		if(this.options.OnColorSelected)
465
			this.options.OnColorSelected(this,color);
466
	},
467
 
468
	getFullPickerContainer : function(pickerID)
469
	{
470
		//create the 3 buttons
471
		this.buttons =
472
		{
473
			//Less   : INPUT({value:'Less Colors', className:'button', type:'button'}),
474
			OK	   : INPUT({value:this.options.OKButtonText, className:'button', type:'button'}),
475
			Cancel : INPUT({value:this.options.CancelButtonText, className:'button', type:'button'})
476
		};
477
 
478
		//create the 6 inputs
479
		var inputs = {};
480
		['H','S','V','R','G','B'].each(function(type)
481
		{
482
			inputs[type] = INPUT({type:'text',size:'3',maxlength:'3'});
483
		});
484
 
485
		//create the HEX input
486
		inputs['HEX'] = INPUT({className:'hex',type:'text',size:'6',maxlength:'6'});
487
		this.inputs = inputs;
488
 
489
		var images = Prado.WebUI.TColorPicker.UIImages;
490
 
491
		this.inputs['currentColor'] = SPAN({className:'currentColor'});
492
		this.inputs['oldColor'] = SPAN({className:'oldColor'});
493
 
494
		var inputsTable =
495
			TABLE({className:'inputs'}, TBODY(null,
496
				TR(null,
497
					TD({className:'currentcolor',colSpan:2},
498
						this.inputs['currentColor'], this.inputs['oldColor'])),
499
 
500
				TR(null,
501
					TD(null,'H:'),
502
					TD(null,this.inputs['H'], '??')),
503
 
504
				TR(null,
505
					TD(null,'S:'),
506
					TD(null,this.inputs['S'], '%')),
507
 
508
				TR(null,
509
					TD(null,'V:'),
510
					TD(null,this.inputs['V'], '%')),
511
 
512
				TR(null,
513
					TD({className:'gap'},'R:'),
514
					TD({className:'gap'},this.inputs['R'])),
515
 
516
				TR(null,
517
					TD(null,'G:'),
518
					TD(null, this.inputs['G'])),
519
 
520
				TR(null,
521
					TD(null,'B:'),
522
					TD(null, this.inputs['B'])),
523
 
524
				TR(null,
525
					TD({className:'gap'},'#'),
526
					TD({className:'gap'},this.inputs['HEX']))
527
			));
528
 
529
		var UIimages =
530
		{
531
			selector : SPAN({className:'selector'}),
532
			background : SPAN({className:'colorpanel'}),
533
			slider : SPAN({className:'slider'}),
534
			hue : SPAN({className:'strip'})
535
		}
536
 
537
		//png alpha channels for IE
538
		if(Prado.Browser().ie)
539
		{
540
			var filter = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader";
541
			UIimages['background'] = SPAN({className:'colorpanel',style:filter+"(src='"+images['background.png']+"' sizingMethod=scale);"})
542
		}
543
 
544
		this.inputs = Object.extend(this.inputs, UIimages);
545
 
546
		var pickerTable =
547
			TABLE(null,TBODY(null,
548
				TR({className:'selection'},
549
					TD({className:'colors'},UIimages['selector'],UIimages['background']),
550
					TD({className:'hue'},UIimages['slider'],UIimages['hue']),
551
					TD({className:'inputs'}, inputsTable)
552
				),
553
				TR({className:'options'},
554
					TD({colSpan:3},
555
						this.buttons['OK'],
556
						this.buttons['Cancel'])
557
				)
558
			));
559
 
560
		return DIV({className:this.options['ClassName']+" FullColorPicker",
561
						id:pickerID+"_picker"},pickerTable);
562
	},
563
 
564
	initializeFullPicker : function()
565
	{
566
		var color = Rico.Color.createFromHex(this.input.value);
567
		this.inputs.oldColor.style.backgroundColor = color.asHex();
568
		this.setColor(color,true);
569
 
570
		var i = 0;
571
		for(var type in this.inputs)
572
		{
573
			Event.observe(this.inputs[type], "change",
574
				this.onInputChanged.bindEvent(this,type));
575
			i++;
576
 
577
			if(i > 6) break;
578
		}
579
 
580
		this.isMouseDownOnColor = false;
581
		this.isMouseDownOnHue = false;
582
 
583
		this._onColorMouseDown = this.onColorMouseDown.bind(this);
584
		this._onHueMouseDown = this.onHueMouseDown.bind(this);
585
		this._onMouseUp = this.onMouseUp.bind(this);
586
		this._onMouseMove = this.onMouseMove.bind(this);
587
 
588
		Event.observe(this.inputs.background, "mousedown", this._onColorMouseDown);
589
		Event.observe(this.inputs.selector, "mousedown", this._onColorMouseDown);
590
		Event.observe(this.inputs.hue, "mousedown", this._onHueMouseDown);
591
		Event.observe(this.inputs.slider, "mousedown", this._onHueMouseDown);
592
 
593
		Event.observe(document.body, "mouseup", this._onMouseUp);
594
 
595
		this.observeMouseMovement();
596
 
597
		Event.observe(this.buttons.Cancel, "click", this.hide.bindEvent(this,this.options['Mode']));
598
		Event.observe(this.buttons.OK, "click", this.onOKClicked.bind(this));
599
	},
600
 
601
	observeMouseMovement : function()
602
	{
603
		if(!this._observingMouseMove)
604
		{
605
			Event.observe(document.body, "mousemove", this._onMouseMove);
606
			this._observingMouseMove = true;
607
		}
608
	},
609
 
610
	onColorMouseDown : function(ev)
611
	{
612
		this.isMouseDownOnColor = true;
613
		this.onMouseMove(ev);
614
		Event.stop(ev);
615
	},
616
 
617
	onHueMouseDown : function(ev)
618
	{
619
		this.isMouseDownOnHue = true;
620
		this.onMouseMove(ev);
621
		Event.stop(ev);
622
	},
623
 
624
	onMouseUp : function(ev)
625
	{
626
		this.isMouseDownOnColor = false;
627
		this.isMouseDownOnHue = false;
628
		Event.stop(ev);
629
	},
630
 
631
	onMouseMove : function(ev)
632
	{
633
		if(this.isMouseDownOnColor)
634
			this.changeSV(ev);
635
		if(this.isMouseDownOnHue)
636
			this.changeH(ev);
637
		Event.stop(ev);
638
	},
639
 
640
	changeSV : function(ev)
641
	{
642
		var px = Event.pointerX(ev);
643
		var py = Event.pointerY(ev);
644
		var pos = this.inputs.background.cumulativeOffset();
645
 
646
		var x = this.truncate(px - pos[0],0,255);
647
		var y = this.truncate(py - pos[1],0,255);
648
 
649
 
650
		var s = x/255;
651
		var b = (255-y)/255;
652
 
653
		var current_s = parseInt(this.inputs.S.value);
654
		var current_b = parseInt(this.inputs.V.value);
655
 
656
		if(current_s == parseInt(s*100) && current_b == parseInt(b*100)) return;
657
 
658
		var h = this.truncate(this.inputs.H.value,0,360)/360;
659
 
660
		var color = new Rico.Color();
661
		color.rgb = Rico.Color.HSBtoRGB(h,s,b);
662
 
663
 
664
		this.inputs.selector.style.left = x+"px";
665
		this.inputs.selector.style.top = y+"px";
666
 
667
		this.inputs.currentColor.style.backgroundColor = color.asHex();
668
 
669
		return this.setColor(color);
670
	},
671
 
672
	changeH : function(ev)
673
	{
674
		var py = Event.pointerY(ev);
675
		var pos = this.inputs.background.cumulativeOffset();
676
		var y = this.truncate(py - pos[1],0,255);
677
 
678
		var h = (255-y)/255;
679
		var current_h = this.truncate(this.inputs.H.value,0,360);
680
		current_h = current_h == 0 ? 360 : current_h;
681
		if(current_h == parseInt(h*360)) return;
682
 
683
		var s = parseInt(this.inputs.S.value)/100;
684
		var b = parseInt(this.inputs.V.value)/100;
685
		var color = new Rico.Color();
686
		color.rgb = Rico.Color.HSBtoRGB(h,s,b);
687
 
688
		var hue = new Rico.Color(color.rgb.r,color.rgb.g,color.rgb.b);
689
		hue.setSaturation(1); hue.setBrightness(1);
690
 
691
		this.inputs.background.style.backgroundColor = hue.asHex();
692
		this.inputs.currentColor.style.backgroundColor = color.asHex();
693
 
694
		this.inputs.slider.style.top = this.truncate(y,0,255)+"px";
695
		return this.setColor(color);
696
 
697
	},
698
 
699
	onOKClicked : function(ev)
700
	{
701
		var r = this.truncate(this.inputs.R.value,0,255);///255;
702
		var g = this.truncate(this.inputs.G.value,0,255);///255;
703
		var b = this.truncate(this.inputs.B.value,0,255);///255;
704
		var color = new Rico.Color(r,g,b);
705
		this.updateColor(color);
706
		this.inputs.oldColor.style.backgroundColor = color.asHex();
707
		this.hide(ev);
708
	},
709
 
710
	onInputChanged : function(ev, type)
711
	{
712
		if(this.isMouseDownOnColor || isMouseDownOnHue)
713
			return;
714
 
715
 
716
		switch(type)
717
		{
718
			case "H": case "S": case "V":
719
				var h = this.truncate(this.inputs.H.value,0,360)/360;
720
				var s = this.truncate(this.inputs.S.value,0,100)/100;
721
				var b = this.truncate(this.inputs.V.value,0,100)/100;
722
				var color = new Rico.Color();
723
				color.rgb = Rico.Color.HSBtoRGB(h,s,b);
724
				return this.setColor(color,true);
725
			case "R": case "G": case "B":
726
				var r = this.truncate(this.inputs.R.value,0,255);///255;
727
				var g = this.truncate(this.inputs.G.value,0,255);///255;
728
				var b = this.truncate(this.inputs.B.value,0,255);///255;
729
				var color = new Rico.Color(r,g,b);
730
				return this.setColor(color,true);
731
			case "HEX":
732
				var color = Rico.Color.createFromHex(this.inputs.HEX.value);
733
				return this.setColor(color,true);
734
		}
735
	},
736
 
737
	setColor : function(color, update)
738
	{
739
		var hsb = color.asHSB();
740
 
741
		this.inputs.H.value = parseInt(hsb.h*360);
742
		this.inputs.S.value = parseInt(hsb.s*100);
743
		this.inputs.V.value = parseInt(hsb.b*100);
744
		this.inputs.R.value = color.rgb.r;
745
		this.inputs.G.value = color.rgb.g;
746
		this.inputs.B.value = color.rgb.b;
747
		this.inputs.HEX.value = color.asHex().substring(1).toUpperCase();
748
 
749
		var images = Prado.WebUI.TColorPicker.UIImages;
750
 
751
		var changeCss = color.isBright() ? 'removeClassName' : 'addClassName';
752
		Element[changeCss](this.inputs.selector, 'target_white');
753
 
754
		if(update)
755
			this.updateSelectors(color);
756
	},
757
 
758
	updateSelectors : function(color)
759
	{
760
		var hsb = color.asHSB();
761
		var pos = [hsb.s*255, hsb.b*255, hsb.h*255];
762
 
763
		this.inputs.selector.style.left = this.truncate(pos[0],0,255)+"px";
764
		this.inputs.selector.style.top = this.truncate(255-pos[1],0,255)+"px";
765
		this.inputs.slider.style.top = this.truncate(255-pos[2],0,255)+"px";
766
 
767
		var hue = new Rico.Color(color.rgb.r,color.rgb.g,color.rgb.b);
768
		hue.setSaturation(1); hue.setBrightness(1);
769
		this.inputs.background.style.backgroundColor = hue.asHex();
770
		this.inputs.currentColor.style.backgroundColor = color.asHex();
771
	},
772
 
773
	truncate : function(value, min, max)
774
	{
775
		value = parseInt(value);
776
		return value < min ? min : value > max ? max : value;
777
	}
778
});