Browse Source

Math: make Color default alpha values work with Half.

There's some fugliness needed for the MSVC 2017 crash workaround but
otherwise all good.
pull/369/head
Vladimír Vondruš 6 years ago
parent
commit
d2513cac3c
  1. 12
      src/Magnum/Math/Color.h
  2. 11
      src/Magnum/Math/Test/ColorTest.cpp

12
src/Magnum/Math/Color.h

@ -37,6 +37,11 @@
#include <tuple> /** @todo remove when Color[34]::Hsv is removed */
#endif
#if defined(CORRADE_MSVC2017_COMPATIBILITY) && !defined(CORRADE_MSVC2015_COMPATIBILITY)
/* Needed by the fullChannel() workaround */
#include "Magnum/Math/Half.h"
#endif
namespace Magnum { namespace Math {
namespace Implementation {
@ -203,15 +208,18 @@ template<class T> inline Vector3<typename Color3<T>::FloatingPointType> toXyz(ty
projects using CMake. Not using SFINAE in this case makes it work. Minimal
repro case here: https://twitter.com/czmosra/status/1039446378248896513 */
template<class T> constexpr typename std::enable_if<IsFloatingPoint<T>::value, T>::type fullChannel() {
return T(1);
return T(1.0);
}
template<class T> constexpr typename std::enable_if<IsIntegral<T>::value, T>::type fullChannel() {
return Implementation::bitMax<T>();
}
#else
template<class T> constexpr T fullChannel() { return bitMax<T>(); }
/** @todo half */
template<> constexpr float fullChannel<float>() { return 1.0f; }
template<> constexpr Half fullChannel<Half>() {
/* This is 1.0_h, but expressible in a constexpr context */
return Half{UnsignedShort{0x3c00}};
}
template<> constexpr double fullChannel<double>() { return 1.0; }
template<> constexpr long double fullChannel<long double>() { return 1.0l; }
#endif

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

@ -33,6 +33,7 @@
#endif
#include "Magnum/Math/Color.h"
#include "Magnum/Math/Half.h"
#include "Magnum/Math/StrictWeakOrdering.h"
struct Vec3 {
@ -75,6 +76,7 @@ struct ColorTest: Corrade::TestSuite::Tester {
explicit ColorTest();
void construct();
void constructDefaultAlphaHalf();
void constructDefault();
void constructNoInit();
void constructOneValue();
@ -152,7 +154,9 @@ typedef Math::Color3<Float> Color3;
typedef Math::Color3<UnsignedByte> Color3ub;
typedef Math::Vector4<Float> Vector4;
typedef Math::Vector4<Half> Vector4h;
typedef Math::Color4<Float> Color4;
typedef Math::Color4<Half> Color4h;
typedef Math::Color4<UnsignedByte> Color4ub;
typedef Math::ColorHsv<Float> ColorHsv;
@ -197,6 +201,7 @@ constexpr struct {
ColorTest::ColorTest() {
addTests({&ColorTest::construct,
&ColorTest::constructDefaultAlphaHalf,
&ColorTest::constructDefault,
&ColorTest::constructNoInit,
&ColorTest::constructOneValue,
@ -292,6 +297,12 @@ void ColorTest::construct() {
CORRADE_VERIFY((std::is_nothrow_constructible<Color4, Float, Float, Float, Float>::value));
}
void ColorTest::constructDefaultAlphaHalf() {
/* The default for alpha should work also for the Half type */
Color4h a{1.0_h, 0.5_h, 0.75_h};
CORRADE_COMPARE(a, Vector4h(1.0_h, 0.5_h, 0.75_h, 1.0_h));
}
void ColorTest::constructDefault() {
constexpr Color3 a1;
constexpr Color3 a2{ZeroInit};

Loading…
Cancel
Save