Browse Source

Math: ability to convert Color from/to external representation.

pull/179/head
Vladimír Vondruš 10 years ago
parent
commit
b033af3b59
  1. 20
      src/Magnum/Math/Color.h
  2. 69
      src/Magnum/Math/Test/ColorTest.cpp

20
src/Magnum/Math/Color.h

@ -294,6 +294,16 @@ template<class T> class Color3: public Vector3<T> {
*/
template<class U> constexpr explicit Color3(const Vector<3, U>& other) noexcept: Vector3<T>(other) {}
/** @brief Construct color from external representation */
template<class U, class V =
#ifndef CORRADE_MSVC2015_COMPATIBILITY /* Causes ICE */
decltype(Implementation::VectorConverter<3, T, U>::from(std::declval<U>()))
#else
decltype(Implementation::VectorConverter<3, T, U>())
#endif
>
constexpr explicit Color3(const U& other): Vector3<T>(Implementation::VectorConverter<3, T, U>::from(other)) {}
/** @brief Copy constructor */
constexpr /*implicit*/ Color3(const Vector<3, T>& other) noexcept: Vector3<T>(other) {}
@ -504,6 +514,16 @@ class Color4: public Vector4<T> {
*/
template<class U> constexpr explicit Color4(const Vector<4, U>& other) noexcept: Vector4<T>(other) {}
/** @brief Construct color from external representation */
template<class U, class V =
#ifndef CORRADE_MSVC2015_COMPATIBILITY /* Causes ICE */
decltype(Implementation::VectorConverter<4, T, U>::from(std::declval<U>()))
#else
decltype(Implementation::VectorConverter<4, T, U>())
#endif
>
constexpr explicit Color4(const U& other): Vector4<T>(Implementation::VectorConverter<4, T, U>::from(other)) {}
/** @brief Copy constructor */
constexpr /*implicit*/ Color4(const Vector<4, T>& other) noexcept: Vector4<T>(other) {}

69
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<Color4>::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<Vec3, Color3>::value));
CORRADE_VERIFY(!(std::is_convertible<Vec4, Color4>::value));
CORRADE_VERIFY(!(std::is_convertible<Color3, Vec3>::value));
CORRADE_VERIFY(!(std::is_convertible<Color4, Vec4>::value));
}
void ColorTest::literals() {
using namespace Literals;

Loading…
Cancel
Save