Skip to content

Instantly share code, notes, and snippets.

@stevetranby
Last active August 29, 2015 14:28
Show Gist options
  • Save stevetranby/0baa7a230fc18c83da04 to your computer and use it in GitHub Desktop.
Save stevetranby/0baa7a230fc18c83da04 to your computer and use it in GitHub Desktop.
///////////////////////////////////////////////////////////////
// MARK: Colors -
Color3B STUtil::c3BFromHex(unsigned int hex)
{
GLubyte r = (hex >> 16) & 0xFF;
GLubyte g = (hex >> 8) & 0xFF;
GLubyte b = (hex ) & 0xFF;
return Color3B(r,g,b);
}
Color4B STUtil::c4BFromHex(unsigned int hex)
{
GLubyte r = (hex >> 24) & 0xFF;
GLubyte g = (hex >> 16) & 0xFF;
GLubyte b = (hex >> 8) & 0xFF;
GLubyte a = (hex ) & 0xFF;
return Color4B(r,g,b,a);
}
Color4B STUtil::c4BFromHexString(const std::string& pHexStr)
{
auto hexStr = std::string(pHexStr);
// check if 6 char hex or 8? or neither
if(hexStr.find("#") != std::string::npos) {
hexStr = hexStr.substr(1);
}
if(hexStr.length() == 8) {
unsigned int hex = std::stoi(hexStr, 0, 16);
return c4BFromHex(hex);
} else if(hexStr.length() == 6) {
unsigned int hex = std::stoi(hexStr, 0, 16);
return Color4B(c3BFromHex(hex));
}
return Color4B::BLACK;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment