/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* coca::show.js                                                            */
/* 'The DHTML Color Calculator' javascript source                           */
/* Copyright (c) 2002 Sebastian Böthin <boethin@math.fu-berlin.de>          */
/*                                                                          */
/* Permission to use, copy, and distribute for non-commercial purposes,     */
/* is hereby granted without fee, providing that the above copyright        */ 
/* notice appear in all copies and that both the copyright notice and       */
/* this permission notice appear in supporting documentation.               */
/*                                                                          */
/* This software is provided "as is" without any expressed or implied       */
/* warranty. The author shall not be liable for any damages suffered by     */
/* users of this software. USE AT YOUR OWN RISK.                            */
/*                                                                          */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* requires                                                                 */
/*   coca::convert.js                                                       */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */

// ---------------------------------------------------------------------------
// ColorRect
// dynamic colored rect (browser depended)
// tested with NS4, NS6, IE5, IE6, Opera6; doesn't work in Opera5 :-(
function ColorRect(id, nest) {
	this.id = id;
	nest = (!nest) ? '' : 'document.'+nest+'.'; // NS4
	this.object = document.all ? document.all[id] : // IE
		document.getElementById ? document.getElementById(id) : // DOM
		eval(nest+'document.'+id); // NS4
	this.css = this.object.style ? this.object.style :  // DOM
		eval(nest+'document.layers.'+id); // NS4
	this.update = ColorRect_update;
	return this;
}

function ColorRect_update(c) {
	if (!isNaN(Number(this.css.bgColor))) this.css.bgColor = c; //NS4
	else this.css.backgroundColor = c;
}

// ---------------------------------------------------------------------------
// ColorShow
function ColorShow(id) {
	this.color = null;
	this.r = new ColorRect(id+'R', id);
	this.g = new ColorRect(id+'G', id);
	this.b = new ColorRect(id+'B', id);
	this.rg = new ColorRect(id+'RG', id);
	this.gb = new ColorRect(id+'GB', id);
	this.rb = new ColorRect(id+'RB', id);
	this.rgb = new ColorRect(id+'RGB', id);
	this.update = ColorShow_update;
	return this;
}

function ColorShow_update(h) {
	if (this.color == h) return;
	if (!validhex(h)) return null;
	this.color = h;
	var r = h.substr(1,2);
	var g = h.substr(3,2);
	var b = h.substr(5,2);
	this.r.update('#'+r+'00'+'00');
	this.g.update('#'+'00'+g+'00');
	this.b.update('#'+'00'+'00'+b);
	this.rg.update('#'+r+g+'00');
	this.gb.update('#'+'00'+g+b);
	this.rb.update('#'+r+'00'+b);
	this.rgb.update(h);
}

// ---------------------------------------------------------------------------
// BarShow
function BarShow(id, size) {
	this.color = null;
	this.size = size;
	this.rects = new Array(size);
	this.colors = new Array(size);
	for (var i = 0; i < size; i++) this.rects[i] = new ColorRect(id+i, id);
	this.update = BarShow_update;
	this.get = BarShow_get;
	this.status = BarShow_status;
	return this;
}

function BarShow_update(h) {
	if (this.color == h) return;
	if (!validhex(h)) return null;
	this.color = h;
	var w = hex2rgb(h);
	var v = rgb2hsb(w);
	for (var i = 0; i < this.size; i++) {
		var b = Math.round((this.size - i - 1)/(this.size - 1)*100);
		var c = rgb2hex(hsb2rgb(new Array(v[0], v[1], b)));
		this.colors[i] = c;
		this.rects[i].update(this.colors[i]);
	}
}

function BarShow_get(i) {
	return this.colors[i];
}

function BarShow_status(b) {
	return 'brightness: '+b;
}
