Browse Source

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.
pull/168/head
Vladimír Vondruš 3 years ago
parent
commit
ff312be96e
  1. 19
      src/Magnum/Math/Color.h
  2. 25
      src/Magnum/Math/Test/ColorTest.cpp

19
src/Magnum/Math/Color.h

@ -1393,7 +1393,8 @@ constexpr Vector4<UnsignedByte> operator "" _srgba(unsigned long long value) {
/** @relatesalso Magnum::Math::Color3 /** @relatesalso Magnum::Math::Color3
@brief Float linear RGB literal @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 @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 @see @link operator""_rgbaf() @endlink, @link operator""_rgb() @endlink
@m_keywords{_rgbf rgbf} @m_keywords{_rgbf rgbf}
*/ */
inline Color3<Float> operator "" _rgbf(unsigned long long value) { constexpr Color3<Float> operator "" _rgbf(unsigned long long value) {
return Math::unpack<Color3<Float>>(Color3<UnsignedByte>{UnsignedByte(value >> 16), UnsignedByte(value >> 8), UnsignedByte(value)}); return {((value >> 16) & 0xff)/255.0f,
((value >> 8) & 0xff)/255.0f,
((value >> 0) & 0xff)/255.0f};
} }
/** @relatesalso Magnum::Math::Color3 /** @relatesalso Magnum::Math::Color3
@ -1427,7 +1430,8 @@ inline Color3<Float> operator "" _srgbf(unsigned long long value) {
/** @relatesalso Magnum::Math::Color4 /** @relatesalso Magnum::Math::Color4
@brief Float linear RGBA literal @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 @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 @see @link operator""_rgbf() @endlink, @link operator""_rgba() @endlink
@m_keywords{_rgbaf rgbaf} @m_keywords{_rgbaf rgbaf}
*/ */
inline Color4<Float> operator "" _rgbaf(unsigned long long value) { constexpr Color4<Float> operator "" _rgbaf(unsigned long long value) {
return Math::unpack<Color4<Float>>(Color4<UnsignedByte>{UnsignedByte(value >> 24), UnsignedByte(value >> 16), UnsignedByte(value >> 8), UnsignedByte(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 /** @relatesalso Magnum::Math::Color4

25
src/Magnum/Math/Test/ColorTest.cpp

@ -626,15 +626,26 @@ void ColorTest::data() {
} }
void ColorTest::literals() { void ColorTest::literals() {
constexpr Color3ub a = 0x33b27f_rgb; CORRADE_COMPARE(0x33b27f_rgb, (Color3ub{0x33, 0xb2, 0x7f}));
CORRADE_COMPARE(a, (Color3ub{0x33, 0xb2, 0x7f})); CORRADE_COMPARE(0x33b27fcc_rgba, (Color4ub{0x33, 0xb2, 0x7f, 0xcc}));
constexpr Color4ub b = 0x33b27fcc_rgba;
CORRADE_COMPARE(b, (Color4ub{0x33, 0xb2, 0x7f, 0xcc}));
/* Not constexpr yet */
CORRADE_COMPARE(0x33b27f_rgbf, (Color3{0.2f, 0.698039f, 0.498039f})); CORRADE_COMPARE(0x33b27f_rgbf, (Color3{0.2f, 0.698039f, 0.498039f}));
CORRADE_COMPARE(0x33b27fcc_rgbaf, (Color4{0.2f, 0.698039f, 0.498039f, 0.8f})); 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() { void ColorTest::colors() {

Loading…
Cancel
Save