/*
 * ColorUtil
 * Version 1.0, 20/11/2001
 * 
 * Copyright (c) 2001 by Netzministerium.de
 * Written by René Sander and Till Nagel.
 * Distributed under the terms of the GNU Lesser General Public. (See licence.txt for details)
 */


// a string constant used for decimal-hex-conversion (see helper functions at the bottom)
var HEXVALUES = "0123456789ABCDEF";

// ---------------------------------------------------------------------------

/*
 * Color
 * A simple object to store a color and perform Hex/RGB conversions.
 *
 */


/*
 * Color
 * Constructs a new Color object to store the color you provide
 *
 * Parameters
 *   String hex - Hex representation of the color, e.g. #FF0000 for pure red
 *
 * Returns 
 *   A new Color object with the specified color
 * 
 */
function Color(hex) {
	// if the hex parameter is omitted, black is used
	this.hex = (Color.arguments.length > 0) ? hex : "000000";
	
	// validate and assign color
	rgb = hexToRGB(this.hex);
	this.r = rgb[0];
	this.g = rgb[1];
	this.b = rgb[2];
	
	// assign object methods
	this.setHex = ColorSetHex;
	this.getHex = ColorGetHex;
	this.setRGB = ColorSetRGB;
	this.toString = ColorToString;
	
	return this;
}

/*
 * Color.setRGB
 * Sets the color using RGB values (0-255).
 *
 * Parameters
 *   byte r - red value
 *   byte g - green value
 *   byte b - blue value
 *
 */
function ColorSetRGB(r,g, b) {
	if (r >= 0 && r < 256
	 && g >= 0 && g < 256
	 &&	b >= 0 && b < 256 ) {
		this.r = r;
		this.g = g;
		this.b = b;
		this.hex = RGBToHex(r,g,b);
	}
}

/*
 * Color.getHex
 * Returns the current color in hex format.
 *
 * Returns 
 *   String - the hex color, with a leading #, e.g. #FF0000
 * 
 */
function ColorGetHex() {
	return "#" + this.hex;
}

/*
 * Color.setHex
 * Sets the current color in hex format
 *
 * Parameters
 *   String hex - the new color, works with or without a leading #
 *
 */
function ColorSetHex(hex) {
	rgb = hexToRGB(hex);
	
	// set color using the setRGB() method which does the errorchecking as well
	this.setRGB(rgb[0], rgb[1], rgb[2]);
}

/*
 * Color.toString
 * Returns a string representation of the object
 *
 * Returns 
 *   a string representation of the color as both Hex and RGB.
 * 
 */
function ColorToString() {
	return "#" + this.hex + " = (" + this.r + ", " + this.g + ", " + this.b + ")";
}

// ---------------------------------------------------------------------------

/*
 * ColorBlend
 * Blends between two colors
 */

/*
 * ColorBlend
 * Constructs a new ColorBlend object
 *
 * Parameters
 *   Color colorFrom - color to blend from
 *   Color colorTo - color to blend to
 *
 * Returns 
 *   A ColorBlend object with the two colors
 * 
 */
function ColorBlend(colorFrom, colorTo) {
	// assign object variables
	this.from = colorFrom;
	this.to = colorTo;
	
	// assign object methods
	this.getColor = ColorBlendGetColor;
	this.toString = ColorBlendToString;

	return this;
}

/*
 * ColorBlend.getColor
 * Calculates the color mixed by colorForm and colorTo.
 * 
 * Parameters
 *   double alpha - The blend factor (a real value between 0 and 1)
 *
 */
function ColorBlendGetColor(alpha) {
	a = alpha;
	b = 1 - alpha;
	c = new Color();
		
	c.setRGB(Math.round(this.from.r * a + this.to.r * b),
		Math.round(this.from.g * a + this.to.g * b),
		Math.round(this.from.b * a + this.to.b * b));	

	return c;
}

/*
 * Color.toString
 * Returns a string representation of the object
 *
 * Returns 
 *   a string representation of the ColorBlend object, 
 *   e.g. ColorBlend(#FF0000, #000000) (blending from
 *   pure red to black).
 *   
 */
function ColorBlendToString() {
	return "ColorBlend(" + this.from + ", " + this.to + ")";
}

// ---------------------------------------------------------------------------

/*
 * RGBToHex
 * Rectangle with color and size depending on depth (z value)
 *
 * Input Parameters
 *   byte r - red value
 *   byte g - green value
 *   byte b - blue value
 *
 * Returns 
 *   A hex representation of the given RGB color
 * 
 */
function RGBToHex(r, g, b) {
	return "" + HEXVALUES.charAt(Math.floor(r / 16)) + HEXVALUES.charAt(r % 16) +
		HEXVALUES.charAt(Math.floor(g / 16)) + HEXVALUES.charAt(g % 16) +
		HEXVALUES.charAt(Math.floor(b / 16)) + HEXVALUES.charAt(b % 16);
}

/*
 * hexToRGB
 * returns a RGB representation of the hex value in a 3-element array (r,g,b)
 *
 * Input Parameters
 *   String hex - the new color, works with or without a leading #
 *
 * Returns 
 *   A new preconfigured Material.
 */
function hexToRGB(hex) {
	hex += "";
	// turn hex into uppercase and cut out the interesting part
	hex = hex.toUpperCase().substr( (hex.charAt(0) == "#") ? 1 : 0, 6);
	
	var rgb = new Array(0,0,0);
	// convert the values
	for (j = 0; j < 3; j++) {
		rgb[j] = HEXVALUES.indexOf( hex.charAt(j*2) ) * 16 + HEXVALUES.indexOf( hex.charAt(j*2+1) );
	}
	return rgb;
}


Syntax highlighted by Code2HTML, v. 0.9, modified by Netzministerium, 2001.