hextoscolor
Posted: Tue Feb 13, 2024 11:20 pm
I spent about an hour coding this because I couldn't see any snippets like it. Upon checking again I saw SColor can be constructed with 0x hex values anyway. SColor must handle this better than I can.
I didn't finish this off for both 6 and 8 inputs. One problem I found was entering "AABB;;" by accident. ctoul16 should ignore the semicolon but doesn't. This calculation gives a value of about 600 billion.
Why would ctoul16 return a u32 if the value can only return 255 max? Neither can ctoul16 handle a string.
Code: Select all
irr::video::SColor hextoscolor(std::string s)
{
irr::video::SColor final_color;
irr::u32 r;
irr::u32 g;
irr::u32 b;
if(s.length() < 6 || s.length() == 7 || s.length() > 8)
return final_color = irr::video::SColor(255.f, 255.f, 255.f, 255.f);
if(s.length() == 6)
{
r = irr::core::ctoul16(s[0]) * 16 + irr::core::ctoul16(s[1]);
g = irr::core::ctoul16(s[2]) * 16 + irr::core::ctoul16(s[3]);
b = irr::core::ctoul16(s[4]) * 16 + irr::core::ctoul16(s[5]);
final_color = irr::video::SColor(255.f, r, g, b);
}
return final_color;
}
Why would ctoul16 return a u32 if the value can only return 255 max? Neither can ctoul16 handle a string.