Fix Color4::FromHex, FromHexA ToHex() format.

This commit is contained in:
2025-06-04 13:11:32 -05:00
parent cc4773abba
commit 954b8cb4b5

View File

@@ -114,13 +114,13 @@ Color4 Color4::FromHex(const std::string &hexCode, u8 alpha) {
// TODO: Support 9-character hex codes (with alpha), but raise a warning that suggests using FromHexA explicitly instead.
if (hexCode.length() == 7) {
u8 r, g, b;
std::sscanf(hexCode.c_str(), "#%02x%02x%02x", &r, &g, &b);
std::sscanf(hexCode.c_str(), "#%02hhx%02hhx%02hhx", &r, &g, &b);
return {r, g, b, alpha};
}
if (hexCode.length() == 9) {
u8 r, g, b, a;
std::sscanf(hexCode.c_str(), "#%02x%02x%02x%02x", &r, &g, &b, &a);
std::sscanf(hexCode.c_str(), "#%02hhx%02hhx%02hhx%02hhx", &r, &g, &b, &a);
return {r, g, b, a};
}
@@ -130,7 +130,7 @@ Color4 Color4::FromHex(const std::string &hexCode, u8 alpha) {
Color4 Color4::FromHexA(const std::string &hexACode) {
if (hexACode.length() == 9) {
u8 r, g, b, a;
std::sscanf(hexACode.c_str(), "#%02x%02x%02x%02x", &r, &g, &b, &a);
std::sscanf(hexACode.c_str(), "#%02hhx%02hhx%02hhx%02hhx", &r, &g, &b, &a);
return {r, g, b, a};
}
@@ -327,18 +327,18 @@ LCH Color4::ToLCH() const {
return {};
}
std::string Color4::EscapeCode(bool bg, bool bold) {
std::string Color4::EscapeCode(bool bg, bool bold) const {
if (bg)
return std::format("\033[48;2;{};{};{}m", r, g, b);
return std::format("\033[38;2;{};{};{}m", r, g, b);
}
std::string Color4::FGEscapeCode(bool bold) {
std::string Color4::FGEscapeCode(bool bold) const {
return EscapeCode(false, bold);
}
std::string Color4::BGEscapeCode(bool bold) {
std::string Color4::BGEscapeCode(bool bold) const {
return EscapeCode(true, bold);
}
@@ -355,16 +355,16 @@ bool Color4::operator!=(const Color4& rhs) const
return !(*this == rhs);
}
std::string Color4::ToEscapeCode(bool bg) {
std::string Color4::ToEscapeCode(bool bg) const {
if (bg)
return std::format("\033[48;2;{};{};{}m", r, g, b);
return std::format("\033[38;2;{};{};{}m", r, g, b);
}
static std::string decimal_to_hex(int dec)
static std::string decimal_to_hex(int dec, int digits = 0)
{
if (dec < 1) return "00";
if (dec < 1) return std::string(digits, '0');
int hex = dec;
std::string hexStr = "";
@@ -379,22 +379,33 @@ static std::string decimal_to_hex(int dec)
dec /= 16;
}
if (digits > 0)
{
while (hexStr.length() < digits)
{
hexStr = "0" + hexStr;
digits--;
}
}
return hexStr;
}
std::string Color4::ToHex() const {
std::string rs = decimal_to_hex(r);
std::string gs = decimal_to_hex(g);
std::string bs = decimal_to_hex(b);
std::string rs = decimal_to_hex(r, 2);
std::string gs = decimal_to_hex(g, 2);
std::string bs = decimal_to_hex(b, 2);
return "#" + rs + gs + bs;
}
std::string Color4::ToHexAlpha() const {
std::string rs = decimal_to_hex(r);
std::string gs = decimal_to_hex(g);
std::string bs = decimal_to_hex(b);
std::string as = decimal_to_hex(a);
std::string rs = decimal_to_hex(r, 2);
std::string gs = decimal_to_hex(g, 2);
std::string bs = decimal_to_hex(b, 2);
std::string as = decimal_to_hex(a, 2);
return "#" + rs + gs + bs + as;
}