Skip to content

Commit 523c5c3

Browse files
halx99aismann
andcommitted
Add API: Color32::fromHex and unify Color::fromHex
Co-authored-by: aismann <aismann@users.noreply.github.com>
1 parent 8a017e4 commit 523c5c3

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

axmol/base/Properties.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,15 +1198,18 @@ bool Properties::parseColor(std::string_view str, Color* out)
11981198
{
11991199
if (!str.empty())
12001200
{
1201-
if (str.length() == 9 && str[0] == '#')
1201+
if (str.length() == 9 && str[0] == '#') // '#RRGGBBAA'
12021202
{
12031203
// Read the string into an int as hex.
12041204
unsigned int color;
12051205
auto [_, ec] = tlx::from_chars(str.substr(1), color, 16);
12061206
if (ec == std::errc{})
12071207
{
12081208
if (out)
1209-
out->set(Color::fromHex(color));
1209+
{
1210+
const uint32_t argb = (color >> 8) | (color << 24);
1211+
out->set(Color::fromHex(argb));
1212+
}
12101213
return true;
12111214
}
12121215
else

axmol/math/Color.h

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,30 @@ struct HSV;
3939
*/
4040
struct AX_DLL Color32
4141
{
42-
constexpr Color32() : value(0) {}
43-
constexpr Color32(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a) : r(_r), g(_g), b(_b), a(_a) {}
42+
/**
43+
* Creates a color from a hexadecimal value.
44+
*
45+
* Supports 0xRRGGBB and 0xAARRGGBB formats.
46+
* Alpha defaults to 255 when omitted.
47+
*/
48+
static constexpr Color32 fromHex(uint32_t v) noexcept
49+
{
50+
if (v > 0x00FFFFFFu)
51+
{
52+
return Color32{static_cast<uint8_t>((v >> 16) & 0xFF), static_cast<uint8_t>((v >> 8) & 0xFF),
53+
static_cast<uint8_t>(v & 0xFF), static_cast<uint8_t>((v >> 24) & 0xFF)};
54+
}
55+
else // 24-bit
56+
{
57+
return Color32{static_cast<uint8_t>((v >> 16) & 0xFF), static_cast<uint8_t>((v >> 8) & 0xFF),
58+
static_cast<uint8_t>(v & 0xFF), 255};
59+
}
60+
}
4461

45-
constexpr Color32(uint8_t _r, uint8_t _g, uint8_t _b) : r(_r), g(_g), b(_b), a(255) {}
62+
constexpr Color32() noexcept : value(0) {}
63+
constexpr Color32(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a) noexcept : r(_r), g(_g), b(_b), a(_a) {}
64+
65+
constexpr Color32(uint8_t _r, uint8_t _g, uint8_t _b) noexcept : r(_r), g(_g), b(_b), a(255) {}
4666

4767
template <class _Other,
4868
typename = std::enable_if_t<std::is_unsigned_v<decltype(_Other{}.r)> &&
@@ -81,7 +101,7 @@ struct AX_DLL Color32
81101
operator Color() const;
82102

83103
template <typename T>
84-
Color32 withAlpha(T alpha) const
104+
Color32 withAlpha(T alpha) const noexcept
85105
{
86106
static_assert(std::is_floating_point_v<T> || std::is_unsigned_v<T>,
87107
"withAlpha: alpha must be float (0~1) or unsigned integer (0~255)");
@@ -150,21 +170,14 @@ struct AX_DLL Color : public Vec4Adapter<Color>
150170
constexpr Color() {}
151171
constexpr Color(float _r, float _g, float _b, float _a) : Vec4Adapter(_r, _g, _b, _a) {}
152172
constexpr Color(float _r, float _g, float _b) : Vec4Adapter(_r, _g, _b, 1.0f) {}
153-
explicit Color(const Color32& color)
173+
explicit constexpr Color(const Color32& color)
154174
: Vec4Adapter(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f)
155175
{}
156176
template <typename _Other>
157177
explicit Color(const _Other& color) : Vec4Adapter(color.r, color.g, color.b, color.a)
158178
{}
159179

160-
static Color fromHex(unsigned int v)
161-
{
162-
auto r = (v >> 24) & 0xff;
163-
auto g = (v >> 16) & 0xff;
164-
auto b = (v >> 8) & 0xff;
165-
auto a = v & 0xff;
166-
return Color{r / 255.f, g / 255.f, b / 255.f, a / 255.f};
167-
}
180+
static constexpr Color fromHex(uint32_t v) { return Color{Color32::fromHex(v)}; }
168181

169182
inline Color& premultiplyAlpha()
170183
{

0 commit comments

Comments
 (0)