#ifndef Magnum_Color_h #define Magnum_Color_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** @file * @brief Class @ref Magnum::BasicColor3, @ref Magnum::BasicColor4, typedef @ref Magnum::Color3, @ref Magnum::Color4 */ #include #include "Magnum/Magnum.h" #include "Magnum/Math/Functions.h" #include "Magnum/Math/Vector4.h" namespace Magnum { namespace Implementation { /* Convert color from HSV */ template typename std::enable_if::value, BasicColor3>::type fromHSV(typename BasicColor3::HSV hsv) { Math::Deg hue; T saturation, value; std::tie(hue, saturation, value) = hsv; /* Remove repeats */ hue -= Math::floor(T(hue)/T(360))*Math::Deg(360); if(hue < Math::Deg(0)) hue += Math::Deg(360); 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); T t = value * (T(1) - (T(1) - f)*saturation); switch(h) { case 0: return {value, t, p}; case 1: return {q, value, p}; case 2: return {p, value, t}; case 3: return {p, q, value}; case 4: return {t, p, value}; case 5: return {value, p, q}; default: CORRADE_ASSERT_UNREACHABLE(); } } template inline typename std::enable_if::value, BasicColor3>::type fromHSV(typename BasicColor3::HSV hsv) { return Math::denormalize>(fromHSV::FloatingPointType>(hsv)); } /* Internal hue computing function */ template Math::Deg hue(const BasicColor3& color, T max, T delta) { T deltaInv60 = T(60)/delta; T hue(0); if(delta != T(0)) { if(max == color.r()) hue = (color.g()-color.b())*deltaInv60 + (color.g() < color.b() ? T(360) : T(0)); else if(max == color.g()) hue = (color.b()-color.r())*deltaInv60 + T(120); else /* max == color.b() */ hue = (color.r()-color.g())*deltaInv60 + T(240); } return Math::Deg(hue); } /* Hue, saturation, value for floating-point types */ template inline Math::Deg hue(typename std::enable_if::value, const BasicColor3&>::type color) { T max = color.max(); T delta = max - color.min(); return hue(color, max, delta); } template inline T saturation(typename std::enable_if::value, const BasicColor3&>::type color) { T max = color.max(); T delta = max - color.min(); return max != T(0) ? delta/max : T(0); } template inline T value(typename std::enable_if::value, const BasicColor3&>::type color) { return color.max(); } /* Hue, saturation, value for integral types */ template inline Math::Deg::FloatingPointType> hue(typename std::enable_if::value, const BasicColor3&>::type color) { return hue::FloatingPointType>(Math::normalize::FloatingPointType>>(color)); } template inline typename BasicColor3::FloatingPointType saturation(typename std::enable_if::value, const BasicColor3&>::type& color) { return saturation::FloatingPointType>(Math::normalize::FloatingPointType>>(color)); } template inline typename BasicColor3::FloatingPointType value(typename std::enable_if::value, const BasicColor3&>::type color) { return Math::normalize::FloatingPointType>(color.max()); } /* Convert color to HSV */ template inline typename BasicColor3::HSV toHSV(typename std::enable_if::value, const BasicColor3&>::type color) { T max = color.max(); T delta = max - color.min(); return typename BasicColor3::HSV(hue::FloatingPointType>(color, max, delta), max != T(0) ? delta/max : T(0), max); } template inline typename BasicColor3::HSV toHSV(typename std::enable_if::value, const BasicColor3&>::type color) { return toHSV::FloatingPointType>(Math::normalize::FloatingPointType>>(color)); } /* Value for full channel (1.0f for floats, 255 for unsigned byte) */ template inline constexpr typename std::enable_if::value, T>::type fullChannel() { return T(1); } template inline constexpr typename std::enable_if::value, T>::type fullChannel() { return std::numeric_limits::max(); } } /** @brief Three-component (RGB) color The class can store either floating-point (normalized) or integral (denormalized) representation of color. Note that constructor conversion between different types (like in @ref Math::Vector "Vector" classes) doesn't do any (de)normalization, you should use @ref Math::normalize() and @ref Math::denormalize() instead, for example: @code typedef BasicColor3 Color3ub; Color3 a(1.0f, 0.5f, 0.75f); auto b = Math::denormalize(a); // b == {255, 127, 191} @endcode Conversion from and to HSV is done always using floating-point types, so hue is always in range in range @f$ [0.0, 360.0] @f$, saturation and value in range @f$ [0.0, 1.0] @f$. @see @ref Color3, @ref Color3ub, @ref BasicColor4 */ /* Not using template specialization because some internal functions are impossible to explicitly instantiate */ template class BasicColor3: public Math::Vector3 { public: /** * @brief Red color * * Convenience alternative to e.g. `%Color3(red, 0.0f, 0.0f)`. With * floating-point underlying type equivalent to @ref Vector3::xAxis(). * @see @ref green(), @ref blue(), @ref cyan() */ constexpr static BasicColor3 red(T red = Implementation::fullChannel()) { return Math::Vector3::xAxis(red); } /** * @brief Green color * * Convenience alternative to e.g. `%Color3(0.0f, green, 0.0f)`. With * floating-point underlying type equivalent to @ref Vector3::yAxis(). * @see @ref red(), @ref blue(), @ref magenta() */ constexpr static BasicColor3 green(T green = Implementation::fullChannel()) { return Math::Vector3::yAxis(green); } /** * @brief Blue color * * Convenience alternative to e.g. `%Color3(0.0f, 0.0f, blue)`. With * floating-point underlying type equivalent to @ref Vector3::zAxis(). * @see @ref red(), @ref green(), @ref yellow() */ constexpr static BasicColor3 blue(T blue = Implementation::fullChannel()) { return Math::Vector3::zAxis(blue); } /** * @brief Cyan color * * Convenience alternative to e.g. `%Color3(red, 1.0f, 1.0f)`. With * floating-point underlying type equivalent to @ref Vector3::xScale(). * @see @ref magenta(), @ref yellow(), @ref red() */ constexpr static BasicColor3 cyan(T red = T(0)) { return {red, Implementation::fullChannel(), Implementation::fullChannel()}; } /** * @brief Magenta color * * Convenience alternative to e.g. `%Color3(0.0f, green, 0.0f)`. With * floating-point underlying type equivalent to @ref Vector3::yScale(). * @see @ref cyan(), @ref yellow(), @ref green() */ constexpr static BasicColor3 magenta(T green = T(0)) { return {Implementation::fullChannel(), green, Implementation::fullChannel()}; } /** * @brief Yellow color * * Convenience alternative to `%Color3(0.0f, 0.0f, yellow)`. With * floating-point underlying type equivalent to @ref Vector3::zScale(). * @see @ref cyan(), @ref magenta(), @ref red() */ constexpr static BasicColor3 yellow(T blue = T(0)) { return {Implementation::fullChannel(), Implementation::fullChannel(), blue}; } /** @brief Corresponding floating-point type for HSV computation */ typedef typename Math::TypeTraits::FloatingPointType FloatingPointType; /** * @brief Type for storing HSV values * * Hue in range @f$ [0.0, 360.0] @f$, saturation and value in * range @f$ [0.0, 1.0] @f$. */ typedef std::tuple, FloatingPointType, FloatingPointType> HSV; /** * @brief Create RGB color from HSV representation * @param hsv Hue, saturation and value * * Hue can overflow the range @f$ [0.0, 360.0] @f$. */ constexpr static BasicColor3 fromHSV(HSV hsv) { return Implementation::fromHSV(hsv); } /** @overload */ constexpr static BasicColor3 fromHSV(Math::Deg hue, FloatingPointType saturation, FloatingPointType value) { return fromHSV(std::make_tuple(hue, saturation, value)); } /** * @brief Default constructor * * All components are set to zero. */ constexpr /*implicit*/ BasicColor3() {} /** * @brief Gray constructor * @param rgb RGB value */ constexpr explicit BasicColor3(T rgb): Math::Vector3(rgb) {} /** * @brief Constructor * @param r R value * @param g G value * @param b B value */ constexpr /*implicit*/ BasicColor3(T r, T g, T b): Math::Vector3(r, g, b) {} /** * @copydoc Math::Vector::Vector(const Vector&) * * @attention This function doesn't do any (de)normalization, use * @ref Math::normalize() and @ref Math::denormalize() instead. * See class documentation for more information. */ template constexpr explicit BasicColor3(const Math::Vector<3, U>& other): Math::Vector3(other) {} /** @brief Copy constructor */ constexpr BasicColor3(const Math::Vector<3, T>& other): Math::Vector3(other) {} /** * @brief Convert to HSV * * Example usage: * @code * T hue, saturation, value; * std::tie(hue, saturation, value) = color.toHSV(); * @endcode * * @see @ref hue(), @ref saturation(), @ref value(), @ref fromHSV() */ constexpr HSV toHSV() const { return Implementation::toHSV(*this); } /** * @brief Hue * @return Hue in range @f$ [0.0, 360.0] @f$. * * @see @ref saturation(), @ref value(), @ref toHSV(), @ref fromHSV() */ constexpr Math::Deg hue() const { return Math::Deg(Implementation::hue(*this)); } /** * @brief Saturation * @return Saturation in range @f$ [0.0, 1.0] @f$. * * @see @ref hue(), @ref value(), @ref toHSV(), @ref fromHSV() */ constexpr FloatingPointType saturation() const { return Implementation::saturation(*this); } /** * @brief Value * @return Value in range @f$ [0.0, 1.0] @f$. * * @see @ref hue(), @ref saturation(), @ref toHSV(), @ref fromHSV() */ constexpr FloatingPointType value() const { return Implementation::value(*this); } MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(3, BasicColor3) }; /** @brief Three-component (RGB) float color */ typedef BasicColor3 Color3; /** @brief Three-component (RGB) unsigned byte color */ typedef BasicColor3 Color3ub; #ifndef DOXYGEN_GENERATING_OUTPUT MAGNUM_VECTORn_OPERATOR_IMPLEMENTATION(3, BasicColor3) #endif /** @brief Four-component (RGBA) color See @ref BasicColor3 for more information. @see @ref Color4, @ref Color4ub */ /* Not using template specialization because some internal functions are impossible to explicitly instantiate */ #ifndef DOXYGEN_GENERATING_OUTPUT template #else template #endif class BasicColor4: public Math::Vector4 { public: /** @copydoc BasicColor3::FloatingPointType */ typedef typename BasicColor3::FloatingPointType FloatingPointType; /** @copydoc BasicColor3::HSV */ typedef typename BasicColor3::HSV HSV; /** * @brief Red color * * Convenience alternative to e.g. `%Color4(red, 0.0f, 0.0f, alpha)`. * @see @ref green(), @ref blue(), @ref cyan() */ constexpr static BasicColor4 red(T red = Implementation::fullChannel(), T alpha = Implementation::fullChannel()) { return {red, T(0), T(0), alpha}; } /** * @brief Green color * * Convenience alternative to e.g. `%Color4(0.0f, green, 0.0f, alpha)`. * @see @ref red(), @ref blue(), @ref magenta() */ constexpr static BasicColor4 green(T green = Implementation::fullChannel(), T alpha = Implementation::fullChannel()) { return {T(0), green, T(0), alpha}; } /** * @brief Blue color * * Convenience alternative to e.g. `%Color4(0.0f, 0.0f, blue, alpha)`. * @see @ref red(), @ref green(), @ref yellow() */ constexpr static BasicColor4 blue(T blue = Implementation::fullChannel(), T alpha = Implementation::fullChannel()) { return {T(0), T(0), blue, alpha}; } /** * @brief Cyan color * * Convenience alternative to e.g. `%Color4(red, 1.0f, 1.0f, alpha)`. * @see @ref magenta(), @ref yellow(), @ref red() */ constexpr static BasicColor4 cyan(T red = T(0), T alpha = Implementation::fullChannel()) { return {red, Implementation::fullChannel(), Implementation::fullChannel(), alpha}; } /** * @brief Magenta color * * Convenience alternative to e.g. `%Color4(1.0f, green, 1.0f, alpha)`. * @see @ref cyan(), @ref yellow(), @ref green() */ constexpr static BasicColor4 magenta(T green = T(0), T alpha = Implementation::fullChannel()) { return {Implementation::fullChannel(), green, Implementation::fullChannel(), alpha}; } /** * @brief Yellow color * * Convenience alternative to e.g. `%Color4(1.0f, 1.0f, blue, alpha)`. * @see @ref cyan(), @ref magenta(), @ref red() */ constexpr static BasicColor4 yellow(T blue = T(0), T alpha = Implementation::fullChannel()) { return {Implementation::fullChannel(), Implementation::fullChannel(), blue, alpha}; } /** * @copydoc BasicColor3::fromHSV() * @param a Alpha value, defaults to `1.0` for floating-point types * and maximum positive value for integral types. */ constexpr static BasicColor4 fromHSV(HSV hsv, T a = Implementation::fullChannel()) { return BasicColor4(Implementation::fromHSV(hsv), a); } /** @overload */ constexpr static BasicColor4 fromHSV(Math::Deg hue, FloatingPointType saturation, FloatingPointType value, T alpha) { return fromHSV(std::make_tuple(hue, saturation, value), alpha); } /** * @brief Default constructor * * RGB components are set to zero, A component is set to `1.0` for * floating-point types and maximum positive value for integral types. */ constexpr /*implicit*/ BasicColor4(): Math::Vector4(T(0), T(0), T(0), Implementation::fullChannel()) {} /** * @copydoc BasicColor3::BasicColor3(T) * @param alpha Alpha value, defaults to `1.0` for floating-point types * and maximum positive value for integral types. */ constexpr explicit BasicColor4(T rgb, T alpha = Implementation::fullChannel()): Math::Vector4(rgb, rgb, rgb, alpha) {} /** * @brief Constructor * @param r R value * @param g G value * @param b B value * @param a A value, defaults to `1.0` for floating-point types and * maximum positive value for integral types. */ constexpr /*implicit*/ BasicColor4(T r, T g, T b, T a = Implementation::fullChannel()): Math::Vector4(r, g, b, a) {} /** * @brief Constructor * @param rgb Three-component color * @param a A value */ /* Not marked as explicit, because conversion from BasicColor3 to BasicColor4 is fairly common, nearly always with A set to 1 */ constexpr /*implicit*/ BasicColor4(const Math::Vector3& rgb, T a = Implementation::fullChannel()): Math::Vector4(rgb[0], rgb[1], rgb[2], a) {} /** * @copydoc Math::Vector::Vector(const Vector&) * * @attention This function doesn't do any (de)normalization, use * @ref Math::normalize() and @ref Math::denormalize() instead. * See @ref BasicColor3 class documentation for more information. */ template constexpr explicit BasicColor4(const Math::Vector<4, U>& other): Math::Vector4(other) {} /** @brief Copy constructor */ constexpr BasicColor4(const Math::Vector<4, T>& other): Math::Vector4(other) {} /** @copydoc BasicColor3::toHSV() */ constexpr HSV toHSV() const { return Implementation::toHSV(Math::Vector4::rgb()); } /** @copydoc BasicColor3::hue() */ constexpr Math::Deg hue() const { return Implementation::hue(Math::Vector4::rgb()); } /** @copydoc BasicColor3::saturation() */ constexpr FloatingPointType saturation() const { return Implementation::saturation(Math::Vector4::rgb()); } /** @copydoc BasicColor3::value() */ constexpr FloatingPointType value() const { return Implementation::value(Math::Vector4::rgb()); } MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(4, BasicColor4) }; /** @brief Four-component (RGBA) float color */ typedef BasicColor4 Color4; /** @brief Four-component (RGBA) unsigned byte color */ typedef BasicColor4 Color4ub; #ifndef DOXYGEN_GENERATING_OUTPUT MAGNUM_VECTORn_OPERATOR_IMPLEMENTATION(4, BasicColor4) #endif /** @debugoperator{Magnum::BasicColor3} */ template inline Debug operator<<(Debug debug, const BasicColor3& value) { return debug << static_cast&>(value); } /** @debugoperator{Magnum::BasicColor4} */ template inline Debug operator<<(Debug debug, const BasicColor4& value) { return debug << static_cast&>(value); } namespace Math { namespace Implementation { template struct TypeForSize<3, BasicColor3> { typedef BasicColor3 Type; }; template struct TypeForSize<3, BasicColor4> { typedef BasicColor3 Type; }; template struct TypeForSize<4, BasicColor3> { typedef BasicColor4 Type; }; template struct TypeForSize<4, BasicColor4> { typedef BasicColor4 Type; }; }} } namespace Corrade { namespace Utility { /** @configurationvalue{Magnum::BasicColor3} */ template struct ConfigurationValue>: public ConfigurationValue> {}; /** @configurationvalue{Magnum::BasicColor4} */ template struct ConfigurationValue>: public ConfigurationValue> {}; }} #endif