From b033af3b593f536397628c44c9ff3c6ce6ed3628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 3 Oct 2016 16:41:42 +0200 Subject: [PATCH] Math: ability to convert Color from/to external representation. --- src/Magnum/Math/Color.h | 20 +++++++++ src/Magnum/Math/Test/ColorTest.cpp | 69 +++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/Magnum/Math/Color.h b/src/Magnum/Math/Color.h index 2d7094cec..6269587d0 100644 --- a/src/Magnum/Math/Color.h +++ b/src/Magnum/Math/Color.h @@ -294,6 +294,16 @@ template class Color3: public Vector3 { */ template constexpr explicit Color3(const Vector<3, U>& other) noexcept: Vector3(other) {} + /** @brief Construct color from external representation */ + template::from(std::declval())) + #else + decltype(Implementation::VectorConverter<3, T, U>()) + #endif + > + constexpr explicit Color3(const U& other): Vector3(Implementation::VectorConverter<3, T, U>::from(other)) {} + /** @brief Copy constructor */ constexpr /*implicit*/ Color3(const Vector<3, T>& other) noexcept: Vector3(other) {} @@ -504,6 +514,16 @@ class Color4: public Vector4 { */ template constexpr explicit Color4(const Vector<4, U>& other) noexcept: Vector4(other) {} + /** @brief Construct color from external representation */ + template::from(std::declval())) + #else + decltype(Implementation::VectorConverter<4, T, U>()) + #endif + > + constexpr explicit Color4(const U& other): Vector4(Implementation::VectorConverter<4, T, U>::from(other)) {} + /** @brief Copy constructor */ constexpr /*implicit*/ Color4(const Vector<4, T>& other) noexcept: Vector4(other) {} diff --git a/src/Magnum/Math/Test/ColorTest.cpp b/src/Magnum/Math/Test/ColorTest.cpp index 85fef7b08..105d32daa 100644 --- a/src/Magnum/Math/Test/ColorTest.cpp +++ b/src/Magnum/Math/Test/ColorTest.cpp @@ -29,7 +29,41 @@ #include "Magnum/Math/Color.h" -namespace Magnum { namespace Math { namespace Test { +struct Vec3 { + float x, y, z; +}; + +struct Vec4 { + float x, y, z, w; +}; + +namespace Magnum { namespace Math { + +namespace Implementation { + +template<> struct VectorConverter<3, float, Vec3> { + constexpr static Vector<3, Float> from(const Vec3& other) { + return {other.x, other.y, other.z}; + } + + constexpr static Vec3 to(const Vector<3, Float>& other) { + return {other[0], other[1], other[2]}; + } +}; + +template<> struct VectorConverter<4, float, Vec4> { + constexpr static Vector<4, Float> from(const Vec4& other) { + return {other.x, other.y, other.z, other.w}; + } + + constexpr static Vec4 to(const Vector<4, Float>& other) { + return {other[0], other[1], other[2], other[3]}; + } +}; + +} + +namespace Test { struct ColorTest: Corrade::TestSuite::Tester { explicit ColorTest(); @@ -42,6 +76,7 @@ struct ColorTest: Corrade::TestSuite::Tester { void constructConversion(); void constructNormalization(); void constructCopy(); + void convert(); void literals(); @@ -85,6 +120,7 @@ ColorTest::ColorTest() { &ColorTest::constructConversion, &ColorTest::constructNormalization, &ColorTest::constructCopy, + &ColorTest::convert, &ColorTest::literals, @@ -241,6 +277,37 @@ void ColorTest::constructCopy() { CORRADE_VERIFY(std::is_nothrow_copy_assignable::value); } +void ColorTest::convert() { + constexpr Vec3 a3{1.5f, 2.0f, -3.5f}; + constexpr Color3 b3{1.5f, 2.0f, -3.5f}; + + constexpr Color3 c3(a3); + CORRADE_COMPARE(c3, b3); + + constexpr Vec3 d3(b3); + CORRADE_COMPARE(d3.x, a3.x); + CORRADE_COMPARE(d3.y, a3.y); + CORRADE_COMPARE(d3.z, a3.z); + + constexpr Vec4 a4{1.5f, 2.0f, -3.5f, -0.5f}; + constexpr Color4 b4{1.5f, 2.0f, -3.5f, -0.5f}; + + constexpr Vector4 c4(a4); + CORRADE_COMPARE(c4, b4); + + constexpr Vec4 d4(b4); + CORRADE_COMPARE(d4.x, a4.x); + CORRADE_COMPARE(d4.y, a4.y); + CORRADE_COMPARE(d4.z, a4.z); + CORRADE_COMPARE(d4.w, a4.w); + + /* Implicit conversion is not allowed */ + CORRADE_VERIFY(!(std::is_convertible::value)); + CORRADE_VERIFY(!(std::is_convertible::value)); + CORRADE_VERIFY(!(std::is_convertible::value)); + CORRADE_VERIFY(!(std::is_convertible::value)); +} + void ColorTest::literals() { using namespace Literals;