/*
*	Package
*	color.class.js		2000.09.01
*	Victor A.Spirin,	victor_aspirin@mail.ru
*/




function color()
	{
	var R;
	var G;
	var B;

	var ColorObject = {};

	ColorObject.setFromRGB = function ( Red, Green, Blue )
		{
		R = Red & 255;
		G = Green & 255;
		B = Blue & 255;
		}

	ColorObject.getRed = function ()
		{
		return R;
		}

	ColorObject.getGreen = function ()
		{
		return G;
		}

	ColorObject.getBlue = function ()
		{
		return B;
		}

	ColorObject.setFromInteger = function ( Color )
		{
		R = (Color>>16) & 255;
		G = (Color>>8) & 255;
		B = Color & 255;
		}

	ColorObject.getInteger = function()
		{
		return (R<<16) + (G<<8) + B;
		}

	ColorObject.setFromCSSString = function ( Color )
		{
		var Components = Color.substring( Color.indexOf('(') + 1, Color.indexOf(')') ).split( ',' );
		R = parseInt( Components[0] ) & 255;
		G = parseInt( Components[1] ) & 255;
		B = parseInt( Components[2] ) & 255;
		}

	ColorObject.getCSSString = function()
		{
		return 'rgb(' + R + ', ' + G + ', ' + B + ')';
		}

	ColorObject.setFromHexadecimalString = function ( Color )
		{
		var Ciphers = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15};

		var Hexadecimal = Color.substring( Color.indexOf('#') + 1 ).substring( 0, 6 );
		var Values = [];
			for( var i = 0 ; i < 6 ; i++ )
			{
			Values[i] = Ciphers[Hexadecimal.charAt(i)];
			}
		R = (Values[0]<<4) + Values[1];
		G = (Values[2]<<4) + Values[3];
		B = (Values[4]<<4) + Values[5];
		}

	ColorObject.getHexadecimalString = function ()
		{
		var Ciphers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
		return '#' + Ciphers[R>>4 & 15] + Ciphers[R & 15] + Ciphers[G>>4 & 15] + Ciphers[G & 15] + Ciphers[B>>4 & 15] + Ciphers[B & 15];
		}

		if( arguments.length == 1 )
		{
			switch( typeof arguments[0] )
			{
			case 'number':
				ColorObject.setFromInteger( arguments[0] );
				break;
			case 'string':
					if( arguments[0].indexOf('rgb') != -1 ) ColorObject.setFromCSSString( arguments[0] );
					else if( arguments[0].indexOf('#') != -1 ) ColorObject.setFromHexadecimalString( arguments[0] );
					else return null;
				break;
			default:
				return null;
			}		
		}
		else if( arguments.length == 3 )
		{
			if( typeof arguments[0] == 'number' && typeof arguments[1] == 'number' && typeof arguments[2] == 'number' )
			{
			ColorObject.setFromRGB( arguments[0], arguments[1], arguments[2] );
			}
			else if( typeof arguments[0] == 'string' && arguments[0] == '' + parseInt(arguments[0]) && typeof arguments[1] == 'string' && arguments[1] == '' + parseInt(arguments[1]) && typeof arguments[2] == 'string' && arguments[2] == '' + parseInt(arguments[2]) )
			{
			ColorObject.setFromRGB( parseInt(arguments[0]), parseInt(arguments[1]), parseInt(arguments[2]) );
			}
			else return null;
		}
		else return null;

	return ColorObject;
	}
