@@ -39,10 +39,30 @@ struct HSV;
3939 */
4040struct 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