diff --git a/src/Color.h b/src/Color.h index cd5ac19ee..bb256994d 100644 --- a/src/Color.h +++ b/src/Color.h @@ -32,15 +32,16 @@ namespace Implementation { /* Convert color from HSV */ template inline typename std::enable_if::value, Color3>::type fromHSV(typename Color3::HSV hsv) { - T hue, saturation, value; + Math::Deg hue; + T saturation, value; std::tie(hue, saturation, value) = hsv; /* Remove repeats */ - hue -= int(hue/T(360))*T(360); - if(hue < T(0)) hue += T(360); + hue -= int(T(hue)/T(360))*Math::Deg(360); + if(hue < Math::Deg(0)) hue += Math::Deg(360); - int h = int(hue/T(60)) % 6; - T f = hue/T(60) - h; + int h = int(T(hue)/T(60)) % 6; + T f = T(hue)/T(60) - h; T p = value * (T(1) - saturation); T q = value * (T(1) - f*saturation); @@ -61,7 +62,7 @@ template inline typename std::enable_if::value, Col } /* Internal hue computing function */ -template T hue(const Color3& color, T max, T delta) { +template Math::Deg hue(const Color3& color, T max, T delta) { T deltaInv60 = T(60)/delta; T hue(0); @@ -74,11 +75,11 @@ template T hue(const Color3& color, T max, T delta) { hue = (color.r()-color.g())*deltaInv60 + T(240); } - return hue; + return Math::Deg(hue); } /* Hue, saturation, value for floating-point types */ -template inline T hue(typename std::enable_if::value, const Color3&>::type color) { +template inline Math::Deg hue(typename std::enable_if::value, const Color3&>::type color) { T max = color.max(); T delta = max - color.min(); return hue(color, max, delta); @@ -93,7 +94,7 @@ template inline T value(typename std::enable_if inline typename Color3::FloatingPointType hue(typename std::enable_if::value, const Color3&>::type color) { +template inline Math::Deg::FloatingPointType> hue(typename std::enable_if::value, const Color3&>::type color) { return hue::FloatingPointType>(Math::normalize::FloatingPointType>>(color)); } template inline typename Color3::FloatingPointType saturation(typename std::enable_if::value, const Color3&>::type& color) { @@ -137,8 +138,6 @@ is always in range in range @f$ [0.0, 360.0] @f$, saturation and value in range @f$ [0.0, 1.0] @f$. @see Color4 - -@todo Hue in degrees so users can use deg() */ /* Not using template specialization because some internal functions are impossible to explicitly instantiate */ @@ -158,7 +157,7 @@ class Color3: public Math::Vector3 { * Hue in range @f$ [0.0, 360.0] @f$, saturation and value in * range @f$ [0.0, 1.0] @f$. */ - typedef std::tuple HSV; + typedef std::tuple, FloatingPointType, FloatingPointType> HSV; /** * @brief Create RGB color from HSV representation @@ -170,7 +169,7 @@ class Color3: public Math::Vector3 { return Implementation::fromHSV(hsv); } /** @overload */ - inline constexpr static Color3 fromHSV(FloatingPointType hue, FloatingPointType saturation, FloatingPointType value) { + inline constexpr static Color3 fromHSV(Math::Deg hue, FloatingPointType saturation, FloatingPointType value) { return fromHSV(std::make_tuple(hue, saturation, value)); } @@ -229,8 +228,8 @@ class Color3: public Math::Vector3 { * * @see saturation(), value(), toHSV(), fromHSV() */ - inline constexpr FloatingPointType hue() const { - return Implementation::hue(*this); + inline constexpr Math::Deg hue() const { + return Math::Deg(Implementation::hue(*this)); } /** @@ -287,7 +286,7 @@ class Color4: public Math::Vector4 { return Color4(Implementation::fromHSV(hsv), a); } /** @overload */ - inline constexpr static Color4 fromHSV(FloatingPointType hue, FloatingPointType saturation, FloatingPointType value, T alpha) { + inline constexpr static Color4 fromHSV(Math::Deg hue, FloatingPointType saturation, FloatingPointType value, T alpha) { return fromHSV(std::make_tuple(hue, saturation, value), alpha); } @@ -355,7 +354,7 @@ class Color4: public Math::Vector4 { } /** @copydoc Color3::hue() */ - inline constexpr FloatingPointType hue() const { + inline constexpr Math::Deg hue() const { return Implementation::hue(rgb()); } diff --git a/src/Test/ColorTest.cpp b/src/Test/ColorTest.cpp index d2dfb0526..e498e1da7 100644 --- a/src/Test/ColorTest.cpp +++ b/src/Test/ColorTest.cpp @@ -92,25 +92,25 @@ void ColorTest::access() { } void ColorTest::fromHue() { - CORRADE_COMPARE(Color3::fromHSV(27.0f, 1.0f, 1.0f), Color3(255, 114, 0)); - CORRADE_COMPARE(Color3::fromHSV(86.0f, 1.0f, 1.0f), Color3(144, 255, 0)); - CORRADE_COMPARE(Color3::fromHSV(134.0f, 1.0f, 1.0f), Color3(0, 255, 59)); - CORRADE_COMPARE(Color3::fromHSV(191.0f, 1.0f, 1.0f), Color3(0, 208, 255)); - CORRADE_COMPARE(Color3::fromHSV(269.0f, 1.0f, 1.0f), Color3(123, 0, 255)); - CORRADE_COMPARE(Color3::fromHSV(317.0f, 1.0f, 1.0f), Color3(255, 0, 182)); + CORRADE_COMPARE(Color3::fromHSV(Deg(27.0f), 1.0f, 1.0f), Color3(255, 114, 0)); + CORRADE_COMPARE(Color3::fromHSV(Deg(86.0f), 1.0f, 1.0f), Color3(144, 255, 0)); + CORRADE_COMPARE(Color3::fromHSV(Deg(134.0f), 1.0f, 1.0f), Color3(0, 255, 59)); + CORRADE_COMPARE(Color3::fromHSV(Deg(191.0f), 1.0f, 1.0f), Color3(0, 208, 255)); + CORRADE_COMPARE(Color3::fromHSV(Deg(269.0f), 1.0f, 1.0f), Color3(123, 0, 255)); + CORRADE_COMPARE(Color3::fromHSV(Deg(317.0f), 1.0f, 1.0f), Color3(255, 0, 182)); } void ColorTest::hue() { - CORRADE_COMPARE(Color3(255, 115, 0).hue(), 27.058824f); - CORRADE_COMPARE(Color3(145, 255, 0).hue(), 85.882353f); - CORRADE_COMPARE(Color3(0, 255, 60).hue(), 134.11765f); - CORRADE_COMPARE(Color3(0, 208, 255).hue(), 191.05882f); - CORRADE_COMPARE(Color3(123, 0, 255).hue(), 268.94117f); - CORRADE_COMPARE(Color3(255, 0, 183).hue(), 316.94117f); + CORRADE_COMPARE(Color3(255, 115, 0).hue(), Deg(27.058824f)); + CORRADE_COMPARE(Color3(145, 255, 0).hue(), Deg(85.882353f)); + CORRADE_COMPARE(Color3(0, 255, 60).hue(), Deg(134.11765f)); + CORRADE_COMPARE(Color3(0, 208, 255).hue(), Deg(191.05882f)); + CORRADE_COMPARE(Color3(123, 0, 255).hue(), Deg(268.94117f)); + CORRADE_COMPARE(Color3(255, 0, 183).hue(), Deg(316.94117f)); } void ColorTest::fromSaturation() { - CORRADE_COMPARE(Color3::fromHSV(0.0f, 0.702f, 1.0f), Color3(255, 75, 75)); + CORRADE_COMPARE(Color3::fromHSV(Deg(0.0f), 0.702f, 1.0f), Color3(255, 75, 75)); } void ColorTest::saturation() { @@ -119,7 +119,7 @@ void ColorTest::saturation() { } void ColorTest::fromValue() { - CORRADE_COMPARE(Color3::fromHSV(0.0f, 1.0f, 0.522f), Color3(133, 0, 0)); + CORRADE_COMPARE(Color3::fromHSV(Deg(0.0f), 1.0f, 0.522f), Color3(133, 0, 0)); } void ColorTest::value() { @@ -127,34 +127,35 @@ void ColorTest::value() { } void ColorTest::hsv() { - CORRADE_COMPARE(Color3::fromHSV(230.0f, 0.749f, 0.427f), Color3(27, 40, 108)); + CORRADE_COMPARE(Color3::fromHSV(Deg(230.0f), 0.749f, 0.427f), Color3(27, 40, 108)); - float hue, saturation, value; + Deg hue; + float saturation, value; std::tie(hue, saturation, value) = Color3(27, 41, 109).toHSV(); - CORRADE_COMPARE(hue, 229.756106f); + CORRADE_COMPARE(hue, Deg(229.756106f)); CORRADE_COMPARE(saturation, 0.752294f); CORRADE_COMPARE(value, 0.427451f); } void ColorTest::hsvOverflow() { - CORRADE_COMPARE(Color3::fromHSV(27.0f-360.0f, 1.0f, 1.0f), Color3(255, 114, 0)); - CORRADE_COMPARE(Color3::fromHSV(86.0f-360.0f, 1.0f, 1.0f), Color3(144, 255, 0)); - CORRADE_COMPARE(Color3::fromHSV(134.0f-360.0f, 1.0f, 1.0f), Color3(0, 255, 59)); - CORRADE_COMPARE(Color3::fromHSV(191.0f-360.0f, 1.0f, 1.0f), Color3(0, 208, 255)); - CORRADE_COMPARE(Color3::fromHSV(269.0f-360.0f, 1.0f, 1.0f), Color3(123, 0, 255)); - CORRADE_COMPARE(Color3::fromHSV(317.0f-360.0f, 1.0f, 1.0f), Color3(255, 0, 182)); - - CORRADE_COMPARE(Color3::fromHSV(360.0f+27.0f, 1.0f, 1.0f), Color3(255, 114, 0)); - CORRADE_COMPARE(Color3::fromHSV(360.0f+86.0f, 1.0f, 1.0f), Color3(144, 255, 0)); - CORRADE_COMPARE(Color3::fromHSV(360.0f+134.0f, 1.0f, 1.0f), Color3(0, 255, 59)); - CORRADE_COMPARE(Color3::fromHSV(360.0f+191.0f, 1.0f, 1.0f), Color3(0, 208, 255)); - CORRADE_COMPARE(Color3::fromHSV(360.0f+269.0f, 1.0f, 1.0f), Color3(123, 0, 255)); - CORRADE_COMPARE(Color3::fromHSV(360.0f+317.0f, 1.0f, 1.0f), Color3(255, 0, 182)); + CORRADE_COMPARE(Color3::fromHSV(Deg(27.0f-360.0f), 1.0f, 1.0f), Color3(255, 114, 0)); + CORRADE_COMPARE(Color3::fromHSV(Deg(86.0f-360.0f), 1.0f, 1.0f), Color3(144, 255, 0)); + CORRADE_COMPARE(Color3::fromHSV(Deg(134.0f-360.0f), 1.0f, 1.0f), Color3(0, 255, 59)); + CORRADE_COMPARE(Color3::fromHSV(Deg(191.0f-360.0f), 1.0f, 1.0f), Color3(0, 208, 255)); + CORRADE_COMPARE(Color3::fromHSV(Deg(269.0f-360.0f), 1.0f, 1.0f), Color3(123, 0, 255)); + CORRADE_COMPARE(Color3::fromHSV(Deg(317.0f-360.0f), 1.0f, 1.0f), Color3(255, 0, 182)); + + CORRADE_COMPARE(Color3::fromHSV(Deg(360.0f+27.0f), 1.0f, 1.0f), Color3(255, 114, 0)); + CORRADE_COMPARE(Color3::fromHSV(Deg(360.0f+86.0f), 1.0f, 1.0f), Color3(144, 255, 0)); + CORRADE_COMPARE(Color3::fromHSV(Deg(360.0f+134.0f), 1.0f, 1.0f), Color3(0, 255, 59)); + CORRADE_COMPARE(Color3::fromHSV(Deg(360.0f+191.0f), 1.0f, 1.0f), Color3(0, 208, 255)); + CORRADE_COMPARE(Color3::fromHSV(Deg(360.0f+269.0f), 1.0f, 1.0f), Color3(123, 0, 255)); + CORRADE_COMPARE(Color3::fromHSV(Deg(360.0f+317.0f), 1.0f, 1.0f), Color3(255, 0, 182)); } void ColorTest::hsvAlpha() { - CORRADE_COMPARE(Color4::fromHSV(std::make_tuple(230.0f, 0.749f, 0.427f), 23), Color4(27, 40, 108, 23)); - CORRADE_COMPARE(Color4::fromHSV(230.0f, 0.749f, 0.427f, 23), Color4(27, 40, 108, 23)); + CORRADE_COMPARE(Color4::fromHSV(std::make_tuple(Deg(230.0f), 0.749f, 0.427f), 23), Color4(27, 40, 108, 23)); + CORRADE_COMPARE(Color4::fromHSV(Deg(230.0f), 0.749f, 0.427f, 23), Color4(27, 40, 108, 23)); } void ColorTest::debug() {