/*****************************************************************************************
Script to calculate the colour a certain portion of the way between one colour and another
                     Written by Mark Wilton-Jones, 23/06/2002
******************************************************************************************

Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use

To use:
_________________________________________________________________________

Inbetween the <head> tags, put:

	<script src="PATH TO SCRIPT/fadecolour.js" type="text/javascript" language="javascript"></script>

Syntax to call the function:

	newColour = fadeColour( colour_from, colour_to, portion_of_the_way_between_them )

Colours may be inputted in '#rrggbb' or 'rrggbb' format. The return value is always in the
format '#rrggbb'. The portion must be a number between 0 and 1 inclusive.
_______________________________________________________________________________________*/

function fadeColour( fromcol, tocol, fadePortion ) {
	if( fadePortion < 0 || fadePortion > 1 || isNaN( fadePortion ) ) {
		window.defaultStatus = 'Fatal error: colour fader passed invalid portion - must be between 0 and 1';
		return '#000000';
	}
	if( typeof( fromcol ) != 'string' || typeof( tocol ) != 'string' || fromcol.length < 6 || fromcol.length > 7 || tocol.length < 6 || tocol.length > 7 ) {
		window.defaultStatus = 'Fatal error: colour fader passed invalid rrggbb hex colour value';
		return '#000000';
	}
	if( fromcol.length == 7 ) { fromcol = fromcol.substring( 1, 7 ); }
	if( tocol.length == 7 ) { tocol = tocol.substring( 1, 7 ); }
	if( typeof( eval( '0x' + fromcol ) ) != 'number' || typeof( eval( '0x' + tocol ) ) != 'number' ) {
		window.defaultStatus = 'Fatal error: colour fader passed invalid rrggbb hex colour value';
		return '#000000';
	}
	for( var x = 0, oF = [], oT = [], oP = [], oH = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']; x < 3; x++ ) {
		oF[x] = eval( '0x' + fromcol.substring( 2 * x, ( 2 * x ) + 2 ) ); oT[x] = eval( '0x' + tocol.substring( 2 * x, ( 2 * x ) + 2 ) );
		oP[x] = Math.round( oF[x] + ( ( oT[x] - oF[x] ) * fadePortion ) ); oP[x] = oH[ ( oP[x] - ( oP[x] % 16 ) ) / 16 ] + oH[ oP[x] % 16 ];
	}
	return '#' + oP.join('');
}