
dojo.declare("zsite_color", null, {
	/*private int*/	red:0,
	/*private int*/	green:0,
	/*private int*/	blue:0,

	constructor: function(/*:int*/ red,/*:int*/ green ,/*:int*/ blue)
	{
		if(typeof green == "undefined")
		{
			var rgb = this.hex2rgb(red);
			this.red= rgb[0];
			this.green = rgb[1];
			this.blue = rgb[2];
		}
		else
		{
			this.red = red;
			this.green = green;
			this.blue = blue;
		}
	},

	getHTMLStyle: function()
	{
		return "rgb("+this.red+","+this.green+","+this.blue+")";
	},

	/**
	 * Return the [red] part of the color.
	 * @type int
	 **/
	getRed: function()
	{
		return this.red;
	},

	/**
	 * Return the [green] part of the color.
	 * @type int
	 **/
	getGreen: function()
	{
		return this.green;
	},

	/**
	 * Return the [blue] part of the color.
	 * @type int
	 **/
	getBlue: function()
	{
		return this.blue;
	},

	/**
	 * Returns the ideal Text Color. Usefull for font color selection by a given background color.
	 *
	 * @returns The <i>ideal</i> inverse color.
	 * @type @NAMESPACE@Color
	 **/
	getIdealTextColor: function()
	{
		var nThreshold = 105;
		var bgDelta = (this.red * 0.299) + (this.green * 0.587) + (this.blue * 0.114);
		return (255 - bgDelta < nThreshold) ? new  zsite_color(0,0,0) : new  zsite_color(255,255,255);
	},

	/**
	 * @private
	 */
	hex2rgb: function(/*:String */hexcolor)
	{
		hexcolor = hexcolor.replace("#","");
		return({0:parseInt(hexcolor.substr(0,2),16),
			1:parseInt(hexcolor.substr(2,2),16),
			2:parseInt(hexcolor.substr(4,2),16)});
	},

	/**
	 * @private
	 **/
	hex: function()
	{ 
		return(this.int2hex(this.red)+this.int2hex(this.green)+this.int2hex(this.blue)); 
	},

	/**
	 * @private
	 */
	int2hex: function(v)
	{
		v=Math.round(Math.min(Math.max(0,v),255));
		return("0123456789ABCDEF".charAt((v-v%16)/16)+"0123456789ABCDEF".charAt(v%16));
	},

	/**
	 * Returns a darker color of the given one..
	 * 
	 * @param {float} fraction  Darkness fraction.
	 * @return        Darker color.
	 * @type @NAMESPACE@Color
	 */
	darker: function(/*float*/fraction)
	{
		var red   = parseInt(Math.round (this.getRed()   * (1.0 - fraction)));
		var green = parseInt(Math.round (this.getGreen() * (1.0 - fraction)));
		var blue  = parseInt(Math.round (this.getBlue()  * (1.0 - fraction)));

		if (red   < 0) red   = 0; else if (red   > 255) red   = 255;
		if (green < 0) green = 0; else if (green > 255) green = 255;
		if (blue  < 0) blue  = 0; else if (blue  > 255) blue  = 255;

		return new zsite_color(red, green, blue);
	},

	/**
	 * Make a color lighter.
	 * 
	 * @param {float} fraction  Darkness fraction.
	 * @type @NAMESPACE@Color
	 * @return          Lighter color.
	 */
	lighter: function(/*float*/ fraction)
	{
		var red   = parseInt(Math.round (this.getRed()   * (1.0 + fraction)));
		var green = parseInt(Math.round (this.getGreen() * (1.0 + fraction)));
		var blue  = parseInt(Math.round (this.getBlue()  * (1.0 + fraction)));

		if (red   < 0) red   = 0; else if (red   > 255) red   = 255;
		if (green < 0) green = 0; else if (green > 255) green = 255;
		if (blue  < 0) blue  = 0; else if (blue  > 255) blue  = 255;

		return new zsite_color(red, green, blue);
	}
});
