/* Color object */

function Color()
{
	this.name = "";
	this.r = 0;
	this.g = 0;
	this.b = 0;
	this.formula = "";
	this.isbase = 1;
	this.id = 0;
}

Color.prototype.HexValue = function()
{
	return "#" + toHex(this.r) + toHex(this.g) + toHex(this.b);
}

Color.prototype.Update = function(red, green, blue)
{
	this.r = Math.max(0,Math.min(255,red));
	this.g = Math.max(0,Math.min(255,green));
	this.b = Math.max(0,Math.min(255,blue));
}

Color.prototype.UpdateFormula = function(formula)
{
	this.formula = formula;
	try
	{
		var e = eval(formula);
		if (typeof(e) == typeof(new Color))
		{
			this.r = e.r;
			this.g = e.g;
			this.b = e.b;
		}
	}
	catch(e)
	{
		this.r = 0;
		this.g = 0;
		this.b = 0;
	}
}

Color.prototype.SetBase = function()
{
	this.isbase = 1;
}

Color.prototype.SetDerived = function()
{
	this.isbase = 0;
}

Color.prototype.red = function()
{
	return this.r;
}

Color.prototype.green = function()
{
	return this.g;
}

Color.prototype.blue = function()
{
	return this.b;
}

Color.prototype.hue = function()
{
	var diff = Math.max(Math.max(this.r, this.g), this.b) - Math.min(Math.min(this.r, this.g), this.b);
	if (this.b > this.g && this.b > this.r)
	{
		return 60 * (this.r - this.g) / diff + 240;
	}
	else if (this.g > this.b && this.g > this.r)
	{
		return 60 * (this.b - this.r) / diff + 120;
	}
	else
	{
		if (this.g < this.b)
		{
			return 60 * (this.g - this.b) / diff + 360;
		}
		else
		{
			return 60 * (this.g - this.b) / diff;
		}
	}
}

Color.prototype.saturation = function()
{
	var max = Math.max(Math.max(this.r, this.g), this.b);
	var min = Math.min(Math.min(this.r, this.g), this.b);
	if (max > 0) return 100 - 100 * min / max;
	return 0;
}

Color.prototype.value = function()
{
	return Math.max(Math.max(this.r, this.g), this.b) * 100 / 255;
}

