/**
 * Import EffectHandler.js first
 * (c) 2005 Mat Gessel
 */

/**
 * ColorTools
 */
function ColorTools() {} // define a "class"

// 3-component array accessors
ColorTools.RED = 0;
ColorTools.GREEN = 1;
ColorTools.BLUE = 2;

// RegEx for color string recognition
ColorTools.FORMAT_HEX_LONG = "^#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$";

ColorTools.HTML_COLORS = 
{
	black:"#000000", silver:"#c0c0c0", maroon:"#800000", red:"#ff0000", 
	navy:"#000080", blue:"#0000ff", purple:"#800080", fuchsia:"#ff00ff", 
	green:"#008000", lime:"#00ff00", olive:"#808000", yellow:"#ffff00", 
	teal:"#008080", aqua:"#00ffff", gray:"#808080", white:"#ffffff"
};

ColorTools.toRGBString = function(rgbArray)
{
	return "rgb(" + rgbArray[ColorTools.RED] + "," + rgbArray[ColorTools.GREEN] + "," + rgbArray[ColorTools.BLUE] + ")";
}

/**
 * Creates a new color by blending the two specified colors. 
 * @param percentage a value between 0 and 100 to specify how much of each color to use
 * @param color1Arr an array in [r{0-255},g{0-255},b{0-255}] format
 * @param color2Arr an array in [r{0-255},g{0-255},b{0-255}] format
 * @return an array in [r{0-255},g{0-255},b{0-255}] format
 */
ColorTools.blendColorsAt = function(percentage, color1Arr, color2Arr)
{
	var result = new Array(3);
	
	result[ColorTools.RED] = (color2Arr[ColorTools.RED] - color1Arr[ColorTools.RED]) * percentage + color1Arr[ColorTools.RED];
	result[ColorTools.GREEN] = (color2Arr[ColorTools.GREEN] - color1Arr[ColorTools.GREEN]) * percentage + color1Arr[ColorTools.GREEN];
	result[ColorTools.BLUE] = (color2Arr[ColorTools.BLUE] - color1Arr[ColorTools.BLUE]) * percentage + color1Arr[ColorTools.BLUE];
	
	return result;
}

/**
 * Creates an array of values representing a multi-step blend between the two colors
 * @param steps the number of color steps used to blend the specified colors (including starting and ending steps)
 * @param color1Arr an array in [r{0-255},g{0-255},b{0-255}] format
 * @param color2Arr an array in [r{0-255},g{0-255},b{0-255}] format
 * @return an array in the format of [Red[0,,,steps-1],Blue[0,,,steps-1],Green[0,,,steps-1]]
 */
ColorTools.blendColors = function(steps, color1Arr, color2Arr)
{
	var result = new Array(3);
	
	result[ColorTools.RED] = ColorTools.interpolate(steps, color1Arr[ColorTools.RED], color2Arr[ColorTools.RED]);
	result[ColorTools.GREEN] = ColorTools.interpolate(steps, color1Arr[ColorTools.GREEN], color2Arr[ColorTools.GREEN]);
	result[ColorTools.BLUE] = ColorTools.interpolate(steps, color1Arr[ColorTools.BLUE], color2Arr[ColorTools.BLUE]);
	
	return result;
}

/**
 * @return an array with 3 elements expressing the Red, Green and Blue componenets
 */
ColorTools.parseColor = function(colorString)
{
	if (typeof colorString != "string")
		throw "ColorTools.parseColor: '" + colorString + "' is not a string\n";
	
	var parts = colorString.match(ColorTools.FORMAT_HEX_LONG);
	
	if (parts != null && parts.length == 4)
	{
		return new Array(parseInt(parts[1], 16), parseInt(parts[2], 16), parseInt(parts[3], 16));
	}
	
	throw "ColorTools.parseColor: '" + colorString + "' is an invalid color format\n";
};

/**
 * @param colorArray an array in the format of [Red,Green,Blue]
 * @return a string in the format of "#RRGGBB"
 */
ColorTools.toHex = function(colorArray)
{
	var digits = ['0', '1', '2', '3', 
				   '4', '5', '6', '7', 
				   '8', '9', 'A', 'B', 
				   'C', 'D', 'E', 'F'];
	
	return "#" + digits[colorArray[ColorTools.RED] >>> 4 & 0xF] + 
		digits[colorArray[ColorTools.RED] & 0xF] + 
		digits[colorArray[ColorTools.GREEN] >>> 4 & 0xF] + 
		digits[colorArray[ColorTools.GREEN] & 0xF] + 
		digits[colorArray[ColorTools.BLUE] >>> 4 & 0xF] + 
		digits[colorArray[ColorTools.BLUE] & 0xF];
}

/**
 * @param steps number of steps including starting and ending step
 * @return an array with a value for each step
 */
ColorTools.interpolate = function(steps, number1, number2)
{
	var result = new Array(steps);
	var increment = (number2 - number1) / (steps - 1);
	for (var i = 0; i < steps; i++)
	{
		result[i] = Math.round(i * increment + number1);
	}
	return result;
};

ColorTools.prototype.toString = function()
{
	return "ColorTools[]";
};

