/*	
,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._

File Name:
	gradient.js
Requires:

Summary:
	A utility used to produce a color gradient.

History:
	07/08/2001 - Keith Gasper - initial development
	12/06/2002 - Keith Gasper - updated gradient & hex2dec
	01/29/2003 - Keith Gasper - fixed problem in hex2dec to correct
		Netscape bug.

Copyright 2001-2003 - AOL,Inc. All rights reserved.
,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._,-._
*/
	
// change a hexidecimal color string to an array of 3 decimal numbers
function hex2dec (hexColor) {
	var arraySize = 3;
	var decColor = new Array(arraySize);
	var check = /^#[\dABCDEF]{6}$/i;
	
	// check to see if hexColor is a valid hex color
	if (!check.test (hexColor))
		return new Array(3);
	
	// make array of 3 dec numbers using the hex string
	for (var i = 0; i < arraySize; i++)
		decColor[i] = parseInt (hexColor.substring ((i * 2) + 1, (i * 2) + 3), 16);

	return decColor;
}

// change an array of 3 numbers to a hex color string
function dec2hex(decColor) {
	hexColor = "#";
	for(loop=0;loop<3;loop++) {
		high = Math.floor(decColor[loop] / 16) + 48;
		low = (decColor[loop] % 16) + 48;
		high += (high<=57 ? 0 : 7);
		low += (low<=57 ? 0 : 7);
		hexColor += String.fromCharCode(high) + String.fromCharCode(low);
	}
	return hexColor;
}

function gradient (startHex, endHex, steps) {
	// return an empty array if the number of steps is inadequate
	if (steps < 2)
		new Array(0);

	// define the colors & identify the delta factors
	var startDec = hex2dec (startHex);
	var endDec = hex2dec (endHex);
	var colorArray = new Array(steps);

	var changeR = (endDec[0] - startDec[0]) / (steps - 1);
	var changeG = (endDec[1] - startDec[1]) / (steps - 1);
	var changeB = (endDec[2] - startDec[2]) / (steps - 1);
	

	// set the first and last array indices to the initial and final color
	colorArray[0] = startHex.toUpperCase();
	colorArray[steps-1] = endHex.toUpperCase();

	// fill the array with colors
	var changeArray = new Array(3);
	for (countG = 1; countG < (steps - 1); countG++) {
		changeArray[0] = Math.round (startDec[0] + changeR * countG);
		changeArray[1] = Math.round (startDec[1] + changeG * countG);
		changeArray[2] = Math.round (startDec[2] + changeB * countG);
		colorArray[countG] = dec2hex (changeArray);
	}	
	return colorArray;
}
