#include "tk_coloroption.h"

const char* TkColorOption::type="TkColorOption";

const char* TkColorOption::getType() const
{
	return type;
}

TkColorOption::TkColorOption():
red(0),green(0),blue(0),isReferencedAsString(false)
{
}

TkColorOption::TkColorOption(int red,int green,int blue):
	red(red),green(green),blue(blue),isReferencedAsString(false)
{
}

string& TkColorOption::toString() const
{
	if (isReferencedAsString)
		return *(new string(value));
	char buf[8];
	sprintf(buf,"#%02X%02X%02X",red,green,blue);
	return *(new string(buf));
}

void TkColorOption::fromObject(Tcl_Obj* object)
{
	isReferencedAsString=true;
	this->value=Tcl_GetString(object);
}

void TkColorOption::doSerialize(TiXmlElement& element) const
{
	int intIsReferencedAsString=(isReferencedAsString?1:0);
	element.SetAttribute("isReferencedAsString",intIsReferencedAsString);
	if (isReferencedAsString)
	{
		element.SetAttribute("value",value.c_str());
	}
	else
	{
		element.SetAttribute("red",red);
		element.SetAttribute("green",green);
		element.SetAttribute("blue",blue);
	}
}

void TkColorOption::doDeserialize(const TiXmlElement& element)
{
	int intIsReferencedAsString;
	element.Attribute("isReferencedAsString",&intIsReferencedAsString);
	isReferencedAsString=(intIsReferencedAsString!=0);
	if (isReferencedAsString)
	{
		const char* value=element.Attribute("value");
		this->value=string(value);
	}
	else
	{
		element.Attribute("red",&red);
		element.Attribute("green",&green);
		element.Attribute("blue",&blue);
	}
}