From ff312be96ef8905d81d9be857e7a30bb3acd6d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 14 Nov 2023 15:41:55 +0100 Subject: [PATCH] Math: make _rgbf and _rgbaf literals constexpr. Need them for the UI. Will eventually need the sRGB literals too, not sure what to do there yet, std::pow() is only constexpr in C++26. I'll be probably long retired when *that* version becomes the min spec for Magnum. --- src/Magnum/Math/Color.h | 19 +++++++++++++------ src/Magnum/Math/Test/ColorTest.cpp | 25 ++++++++++++++++++------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Magnum/Math/Color.h b/src/Magnum/Math/Color.h index 14ac592bc..6eb7327d6 100644 --- a/src/Magnum/Math/Color.h +++ b/src/Magnum/Math/Color.h @@ -1393,7 +1393,8 @@ constexpr Vector4 operator "" _srgba(unsigned long long value) { /** @relatesalso Magnum::Math::Color3 @brief Float linear RGB literal -Calls @ref Color3::fromLinearRgbInt() on the literal value. Example usage: +Equivalent to calling @ref Color3::fromLinearRgbInt() on the literal value. +Example usage: @snippet MagnumMath.cpp _rgbf @@ -1405,8 +1406,10 @@ Calls @ref Color3::fromLinearRgbInt() on the literal value. Example usage: @see @link operator""_rgbaf() @endlink, @link operator""_rgb() @endlink @m_keywords{_rgbf rgbf} */ -inline Color3 operator "" _rgbf(unsigned long long value) { - return Math::unpack>(Color3{UnsignedByte(value >> 16), UnsignedByte(value >> 8), UnsignedByte(value)}); +constexpr Color3 operator "" _rgbf(unsigned long long value) { + return {((value >> 16) & 0xff)/255.0f, + ((value >> 8) & 0xff)/255.0f, + ((value >> 0) & 0xff)/255.0f}; } /** @relatesalso Magnum::Math::Color3 @@ -1427,7 +1430,8 @@ inline Color3 operator "" _srgbf(unsigned long long value) { /** @relatesalso Magnum::Math::Color4 @brief Float linear RGBA literal -Calls @ref Color4::fromLinearRgbaInt() on the literal value. Example usage: +Equivalent to calling @ref Color4::fromLinearRgbaInt() on the literal value. +Example usage: @snippet MagnumMath.cpp _rgbaf @@ -1439,8 +1443,11 @@ Calls @ref Color4::fromLinearRgbaInt() on the literal value. Example usage: @see @link operator""_rgbf() @endlink, @link operator""_rgba() @endlink @m_keywords{_rgbaf rgbaf} */ -inline Color4 operator "" _rgbaf(unsigned long long value) { - return Math::unpack>(Color4{UnsignedByte(value >> 24), UnsignedByte(value >> 16), UnsignedByte(value >> 8), UnsignedByte(value)}); +constexpr Color4 operator "" _rgbaf(unsigned long long value) { + return {((value >> 24) & 0xff)/255.0f, + ((value >> 16) & 0xff)/255.0f, + ((value >> 8) & 0xff)/255.0f, + ((value >> 0) & 0xff)/255.0f}; } /** @relatesalso Magnum::Math::Color4 diff --git a/src/Magnum/Math/Test/ColorTest.cpp b/src/Magnum/Math/Test/ColorTest.cpp index adf57a9ce..c51fdb6f9 100644 --- a/src/Magnum/Math/Test/ColorTest.cpp +++ b/src/Magnum/Math/Test/ColorTest.cpp @@ -626,15 +626,26 @@ void ColorTest::data() { } void ColorTest::literals() { - constexpr Color3ub a = 0x33b27f_rgb; - CORRADE_COMPARE(a, (Color3ub{0x33, 0xb2, 0x7f})); - - constexpr Color4ub b = 0x33b27fcc_rgba; - CORRADE_COMPARE(b, (Color4ub{0x33, 0xb2, 0x7f, 0xcc})); - - /* Not constexpr yet */ + CORRADE_COMPARE(0x33b27f_rgb, (Color3ub{0x33, 0xb2, 0x7f})); + CORRADE_COMPARE(0x33b27fcc_rgba, (Color4ub{0x33, 0xb2, 0x7f, 0xcc})); CORRADE_COMPARE(0x33b27f_rgbf, (Color3{0.2f, 0.698039f, 0.498039f})); CORRADE_COMPARE(0x33b27fcc_rgbaf, (Color4{0.2f, 0.698039f, 0.498039f, 0.8f})); + + /* As the implementation doesn't delegate into unpack() etc in order to be + constexpr, test also boundary values to be sure */ + CORRADE_COMPARE(0xffffff_rgbf, Color3{1.0f}); + CORRADE_COMPARE(0x000000_rgbf, Color3{0.0f}); + CORRADE_COMPARE(0xffffffff_rgbaf, Color4{1.0f}); + CORRADE_COMPARE(0x00000000_rgbaf, (Color4{0.0f, 0.0f})); + + constexpr Color3ub ca = 0x33b27f_rgb; + constexpr Color4ub cb = 0x33b27fcc_rgba; + constexpr Color3 cc = 0x33b27f_rgbf; + constexpr Color4 cd = 0x33b27fcc_rgbaf; + CORRADE_COMPARE(ca, (Color3ub{0x33, 0xb2, 0x7f})); + CORRADE_COMPARE(cb, (Color4ub{0x33, 0xb2, 0x7f, 0xcc})); + CORRADE_COMPARE(cc, (Color3{0.2f, 0.698039f, 0.498039f})); + CORRADE_COMPARE(cd, (Color4{0.2f, 0.698039f, 0.498039f, 0.8f})); } void ColorTest::colors() {