ok, I added it to the stupid XmlNode class so I can include it in 0.2.12 at some point later, when I'm done with the networking code:
shared/include/util/conversion.hint hexChar2Int(char c) {
if(c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
} else {
throw runtime_error("bad hex value");
}
}
inline int hexPair2Int(const char *hex) {
return (hexChar2Int(hex[0]) << 4) + hexChar2Int(hex[1]);
}
shared/include/xml/xml_parser.h// in XmlNode class definition, these functions were inlined, they are now outlined
Vec3f getColor3Value() const;
Vec4f getColor4Value() const;
shared/sources/xml/xml_parser.cppVec3f XmlNode::getColor3Value() {
XmlAttribute *rgbAttr = getAttribute("rgb", false);
if(rgbAttr) {
const string &str = rgbAttr->getValue();
if(str.size != 6) {
throw runtime_error("rgb attribute does not contain 6 digits");
}
return Vec3f(
static_cast<float>(hexPair2Int(str.c_str())) / 255.f,
static_cast<float>(hexPair2Int(str.c_str() + 2)) / 255.f,
static_cast<float>(hexPair2Int(str.c_str() + 4)) / 255.f);
} else {
return Vec3f(getFloatAttribute("red", 0.f, 1.0f),
getFloatAttribute("green", 0.f, 1.0f),
getFloatAttribute("blue", 0.f, 1.0f));
}
}
Vec4f XmlNode::getColor4Value() {
XmlAttribute *rgbaAttr = getAttribute("rgba", false);
if(rgbAttr) {
const string &str = rgbaAttr->getValue();
if(str.size != 8) {
throw runtime_error("rgba attribute does not contain 8 digits");
}
return Vec4f(
static_cast<float>(hexPair2Int(str.c_str())) / 255.f,
static_cast<float>(hexPair2Int(str.c_str() + 2)) / 255.f,
static_cast<float>(hexPair2Int(str.c_str() + 4)) / 255.f,
static_cast<float>(hexPair2Int(str.c_str() + 6)) / 255.f);
} else {
return Vec4f(getFloatAttribute("red", 0.f, 1.0f),
getFloatAttribute("green", 0.f, 1.0f),
getFloatAttribute("blue", 0.f, 1.0f),
getFloatAttribute("alpha", 0.f, 1.0f));
}
}
EDIT: Ok, I have this in now so in 0.2.12, you'll be able to specify colors in either style. For RGB
<my-color red="1" blue="0.5" green="0.125"/>
or
<my-color rgb="ff8020"/>
for red, green, blue, alpha:
<my-color red="1" blue="0.5" green="0.125" alpha="0.5"/>
or
<my-color rgba="ff802080"/>