From e934a1d1d881dab4ac311392042cd7286ed30b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 2 Aug 2015 16:40:49 +0200 Subject: [PATCH] Moved Color to Math namespace. Apart from different include ( instead of ) there shouldn't be any visible change to the user. The BasicColor3 and BasicColor4 classes are now Math::Color3 and Math::Color4. The Color3, Color4, Color3ub and Color4ub typedefs in Magnum namespace stayed the same. BasicColor3 and BasicColor4 is now an alias to Math::Color3 and Math::Color4, is marked as deprecated and will be removed in future release. The same goes for the include, which now just includes the header. --- src/Magnum/AbstractTexture.cpp | 2 +- src/Magnum/Attribute.h | 4 +- src/Magnum/CMakeLists.txt | 5 +- src/Magnum/Color.h | 547 +---------------- src/Magnum/DebugTools/ForceRenderer.h | 2 +- src/Magnum/DebugTools/ShapeRenderer.h | 2 +- src/Magnum/Magnum.h | 22 +- src/Magnum/Math/CMakeLists.txt | 1 + src/Magnum/Math/Color.h | 559 ++++++++++++++++++ src/Magnum/Math/Math.h | 3 + src/Magnum/Math/Swizzle.h | 2 +- src/Magnum/Math/Test/CMakeLists.txt | 1 + src/Magnum/{ => Math}/Test/ColorTest.cpp | 84 +-- src/Magnum/Renderer.cpp | 4 +- src/Magnum/Shaders/DistanceFieldVector.h | 2 +- src/Magnum/Shaders/Flat.h | 2 +- src/Magnum/Shaders/MeshVisualizer.h | 4 +- src/Magnum/Shaders/Phong.h | 2 +- src/Magnum/Shaders/Vector.h | 2 +- src/Magnum/Shaders/VertexColor.h | 2 +- src/Magnum/Test/CMakeLists.txt | 1 - src/Magnum/Test/CubeMapTextureArrayGLTest.cpp | 2 +- src/Magnum/Test/CubeMapTextureGLTest.cpp | 2 +- src/Magnum/Test/FramebufferGLTest.cpp | 2 +- src/Magnum/Test/MeshGLTest.cpp | 2 +- src/Magnum/Test/RectangleTextureGLTest.cpp | 2 +- src/Magnum/Test/TextureArrayGLTest.cpp | 2 +- src/Magnum/Test/TextureGLTest.cpp | 2 +- 28 files changed, 668 insertions(+), 599 deletions(-) create mode 100644 src/Magnum/Math/Color.h rename src/Magnum/{ => Math}/Test/ColorTest.cpp (75%) diff --git a/src/Magnum/AbstractTexture.cpp b/src/Magnum/AbstractTexture.cpp index 0bac4087c..efc6e42c6 100644 --- a/src/Magnum/AbstractTexture.cpp +++ b/src/Magnum/AbstractTexture.cpp @@ -31,12 +31,12 @@ #include "Magnum/BufferImage.h" #endif #include "Magnum/Array.h" -#include "Magnum/Color.h" #include "Magnum/ColorFormat.h" #include "Magnum/Context.h" #include "Magnum/Extensions.h" #include "Magnum/Image.h" #include "Magnum/TextureFormat.h" +#include "Magnum/Math/Color.h" #include "Magnum/Math/Range.h" #ifndef MAGNUM_TARGET_WEBGL diff --git a/src/Magnum/Attribute.h b/src/Magnum/Attribute.h index 2faa39b74..124926a79 100644 --- a/src/Magnum/Attribute.h +++ b/src/Magnum/Attribute.h @@ -571,8 +571,8 @@ template struct Attribute>: Doubl template struct Attribute>: Attribute> {}; template struct Attribute>: Attribute> {}; template struct Attribute>: Attribute> {}; -template struct Attribute>: Attribute> {}; -template struct Attribute>: Attribute> {}; +template struct Attribute>: Attribute> {}; +template struct Attribute>: Attribute> {}; /* Common float and double rectangular matrix attributes */ template struct Attribute>: FloatAttribute, SizedAttribute {}; diff --git a/src/Magnum/CMakeLists.txt b/src/Magnum/CMakeLists.txt index 2a7b2bb68..ac9bcca11 100644 --- a/src/Magnum/CMakeLists.txt +++ b/src/Magnum/CMakeLists.txt @@ -87,7 +87,6 @@ set(Magnum_HEADERS Array.h Attribute.h Buffer.h - Color.h ColorFormat.h Context.h CubeMapTexture.h @@ -132,7 +131,9 @@ set(Magnum_PRIVATE_HEADERS # Deprecated stuff if(BUILD_DEPRECATED) - list(APPEND Magnum_HEADERS ImageReference.h) + list(APPEND Magnum_HEADERS + Color.h + ImageReference.h) endif() # Desktop-only stuff diff --git a/src/Magnum/Color.h b/src/Magnum/Color.h index 1024d0b99..2023f38d1 100644 --- a/src/Magnum/Color.h +++ b/src/Magnum/Color.h @@ -26,547 +26,32 @@ */ /** @file - * @brief Class @ref Magnum::BasicColor3, @ref Magnum::BasicColor4, typedef @ref Magnum::Color3, @ref Magnum::Color4 + * @deprecated Use @ref Magnum/Math/Color.h instead. */ -#include +#include "Magnum/configure.h" -#include "Magnum/Magnum.h" -#include "Magnum/Math/Functions.h" -#include "Magnum/Math/Vector4.h" +#ifdef MAGNUM_BUILD_DEPRECATED +#include -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()); -} +#include "Magnum/Math/Color.h" +CORRADE_DEPRECATED_FILE("use Magnum/Math/Color.h instead") -/* 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(); +namespace Magnum { - 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)); -} +/** @copybrief Math::Color3 + * @deprecated Use @ref Math::Color3 instead. + */ +template using BasicColor3 CORRADE_DEPRECATED_ALIAS("use Math::Color3 instead") = Math::Color3; -/* Value for full channel (1.0f for floats, 255 for unsigned byte) */ -template constexpr typename std::enable_if::value, T>::type fullChannel() { - return T(1); -} -template constexpr typename std::enable_if::value, T>::type fullChannel() { - return std::numeric_limits::max(); -} +/** @copybrief Math::Color4 + * @deprecated Use @ref Math::Color4 instead. + */ +template using BasicColor4 CORRADE_DEPRECATED_ALIAS("use Math::Color4 instead") = Math::Color4; } - -/** -@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(Math::ZeroInitT = Math::ZeroInit) - /** @todoc remove workaround when doxygen is sane */ - #ifndef DOXYGEN_GENERATING_OUTPUT - : Math::Vector3{Math::ZeroInit} - #endif - {} - - /** @copydoc Math::Vector::Vector(NoInitT) */ - explicit BasicColor3(Math::NoInitT) - /** @todoc remove workaround when doxygen is sane */ - #ifndef DOXYGEN_GENERATING_OUTPUT - : Math::Vector3{Math::NoInit} - #endif - {} - - /** - * @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 Math::Vector::Vector(ZeroInitT) */ - constexpr explicit BasicColor4(Math::ZeroInitT) - /** @todoc remove workaround when doxygen is sane */ - #ifndef DOXYGEN_GENERATING_OUTPUT - : Math::Vector4{Math::ZeroInit} - #endif - {} - - /** @copydoc Math::Vector::Vector(NoInitT) */ - explicit BasicColor4(Math::NoInitT) - /** @todoc remove workaround when doxygen is sane */ - #ifndef DOXYGEN_GENERATING_OUTPUT - : Math::Vector4{Math::NoInit} - #endif - {} - - /** - * @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) +#error use Magnum/Math/Color.h instead #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 diff --git a/src/Magnum/DebugTools/ForceRenderer.h b/src/Magnum/DebugTools/ForceRenderer.h index 99814a42d..5162e6884 100644 --- a/src/Magnum/DebugTools/ForceRenderer.h +++ b/src/Magnum/DebugTools/ForceRenderer.h @@ -29,8 +29,8 @@ * @brief Class @ref Magnum::DebugTools::ForceRenderer, @ref Magnum::DebugTools::ForceRendererOptions, typedef @ref Magnum::DebugTools::ForceRenderer2D, @ref Magnum::DebugTools::ForceRenderer3D */ -#include "Magnum/Color.h" #include "Magnum/Resource.h" +#include "Magnum/Math/Color.h" #include "Magnum/SceneGraph/Drawable.h" #include "Magnum/Shaders/Shaders.h" #include "Magnum/DebugTools/visibility.h" diff --git a/src/Magnum/DebugTools/ShapeRenderer.h b/src/Magnum/DebugTools/ShapeRenderer.h index c67dc10f2..025913aca 100644 --- a/src/Magnum/DebugTools/ShapeRenderer.h +++ b/src/Magnum/DebugTools/ShapeRenderer.h @@ -29,8 +29,8 @@ * @brief Class @ref Magnum::DebugTools::ShapeRenderer, @ref Magnum::DebugTools::ShapeRendererOptions, typedef @ref Magnum::DebugTools::ShapeRenderer2D, @ref Magnum::DebugTools::ShapeRenderer3D */ -#include "Magnum/Color.h" #include "Magnum/Resource.h" +#include "Magnum/Math/Color.h" #include "Magnum/SceneGraph/Drawable.h" #include "Magnum/Shapes/Shapes.h" #include "Magnum/Shapes/shapeImplementation.h" diff --git a/src/Magnum/Magnum.h b/src/Magnum/Magnum.h index c3826bb29..51314a2f7 100644 --- a/src/Magnum/Magnum.h +++ b/src/Magnum/Magnum.h @@ -213,6 +213,18 @@ typedef Math::Vector3 Vector3i; /** @brief Four-component signed integer vector */ typedef Math::Vector4 Vector4i; +/** @brief Three-component (RGB) float color */ +typedef Math::Color3 Color3; + +/** @brief Four-component (RGBA) float color */ +typedef Math::Color4 Color4; + +/** @brief Three-component (RGB) unsigned byte color */ +typedef Math::Color3 Color3ub; + +/** @brief Four-component (RGBA) unsigned byte color */ +typedef Math::Color4 Color4ub; + /** @brief 3x3 float transformation matrix @@ -455,12 +467,10 @@ class BufferTexture; enum class BufferTextureFormat: GLenum; #endif -template class BasicColor3; -template class BasicColor4; -typedef BasicColor3 Color3; -typedef BasicColor3 Color3ub; -typedef BasicColor4 Color4; -typedef BasicColor4 Color4ub; +#ifdef MAGNUM_BUILD_DEPRECATED +template using BasicColor3 CORRADE_DEPRECATED_ALIAS("use Math::Color3 instead") = Math::Color3; +template using BasicColor4 CORRADE_DEPRECATED_ALIAS("use Math::Color4 instead") = Math::Color4; +#endif enum class ColorFormat: GLenum; enum class ColorType: GLenum; diff --git a/src/Magnum/Math/CMakeLists.txt b/src/Magnum/Math/CMakeLists.txt index b6eca6573..9e4aa1c40 100644 --- a/src/Magnum/Math/CMakeLists.txt +++ b/src/Magnum/Math/CMakeLists.txt @@ -26,6 +26,7 @@ set(MagnumMath_HEADERS Angle.h BoolVector.h + Color.h Complex.h Constants.h Dual.h diff --git a/src/Magnum/Math/Color.h b/src/Magnum/Math/Color.h new file mode 100644 index 000000000..07963ac70 --- /dev/null +++ b/src/Magnum/Math/Color.h @@ -0,0 +1,559 @@ +#ifndef Magnum_Math_Color_h +#define Magnum_Math_Color_h +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015 + 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::Math::Color3, @ref Magnum::Math::Color4 + */ + +#include + +#include "Magnum/Math/Functions.h" +#include "Magnum/Math/Vector4.h" + +namespace Magnum { namespace Math { + +namespace Implementation { + +/* Convert color from HSV */ +template typename std::enable_if::value, Color3>::type fromHSV(typename Color3::HSV hsv) { + Deg hue; + T saturation, value; + std::tie(hue, saturation, value) = hsv; + + /* Remove repeats */ + hue -= floor(T(hue)/T(360))*Deg(360); + if(hue < Deg(0)) hue += 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, Color3>::type fromHSV(typename Color3::HSV hsv) { + return denormalize>(fromHSV::FloatingPointType>(hsv)); +} + +/* Internal hue computing function */ +template Deg hue(const Color3& 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 Deg(hue); +} + +/* Hue, saturation, value for floating-point types */ +template inline 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); +} +template inline T saturation(typename std::enable_if::value, const Color3&>::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 Color3&>::type color) { + return color.max(); +} + +/* Hue, saturation, value for integral types */ +template inline Deg::FloatingPointType> hue(typename std::enable_if::value, const Color3&>::type color) { + return hue::FloatingPointType>(normalize::FloatingPointType>>(color)); +} +template inline typename Color3::FloatingPointType saturation(typename std::enable_if::value, const Color3&>::type& color) { + return saturation::FloatingPointType>(normalize::FloatingPointType>>(color)); +} +template inline typename Color3::FloatingPointType value(typename std::enable_if::value, const Color3&>::type color) { + return normalize::FloatingPointType>(color.max()); +} + +/* Convert color to HSV */ +template inline typename Color3::HSV toHSV(typename std::enable_if::value, const Color3&>::type color) { + T max = color.max(); + T delta = max - color.min(); + + return typename Color3::HSV(hue::FloatingPointType>(color, max, delta), max != T(0) ? delta/max : T(0), max); +} +template inline typename Color3::HSV toHSV(typename std::enable_if::value, const Color3&>::type color) { + return toHSV::FloatingPointType>(normalize::FloatingPointType>>(color)); +} + +/* Value for full channel (1.0f for floats, 255 for unsigned byte) */ +template constexpr typename std::enable_if::value, T>::type fullChannel() { + return T(1); +} +template 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 Vector classes) doesn't do any +(de)normalization, you should use @ref normalize() and +@ref denormalize() instead, for example: +@code +typedef Color3 Color3ub; +Color3 a(1.0f, 0.5f, 0.75f); +auto b = 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 Color4, @ref Magnum::Color3, @ref Magnum::Color3ub +*/ +/* Not using template specialization because some internal functions are + impossible to explicitly instantiate */ +template class Color3: public 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 Color3 red(T red = Implementation::fullChannel()) { + return 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 Color3 green(T green = Implementation::fullChannel()) { + return 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 Color3 blue(T blue = Implementation::fullChannel()) { + return 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 Color3 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 Color3 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 Color3 yellow(T blue = T(0)) { + return {Implementation::fullChannel(), Implementation::fullChannel(), blue}; + } + + /** @brief Corresponding floating-point type for HSV computation */ + typedef typename 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 Color3 fromHSV(HSV hsv) { + return Implementation::fromHSV(hsv); + } + /** @overload */ + constexpr static Color3 fromHSV(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*/ Color3(ZeroInitT = ZeroInit) + /** @todoc remove workaround when doxygen is sane */ + #ifndef DOXYGEN_GENERATING_OUTPUT + : Vector3{ZeroInit} + #endif + {} + + /** @copydoc Vector::Vector(NoInitT) */ + explicit Color3(NoInitT) + /** @todoc remove workaround when doxygen is sane */ + #ifndef DOXYGEN_GENERATING_OUTPUT + : Vector3{NoInit} + #endif + {} + + /** + * @brief Gray constructor + * @param rgb RGB value + */ + constexpr explicit Color3(T rgb): Vector3(rgb) {} + + /** + * @brief Constructor + * @param r R value + * @param g G value + * @param b B value + */ + constexpr /*implicit*/ Color3(T r, T g, T b): Vector3(r, g, b) {} + + /** + * @copydoc Vector::Vector(const Vector&) + * + * @attention This function doesn't do any (de)normalization, use + * @ref normalize() and @ref denormalize() instead. + * See class documentation for more information. + */ + template constexpr explicit Color3(const Vector<3, U>& other): Vector3(other) {} + + /** @brief Copy constructor */ + constexpr Color3(const Vector<3, T>& other): 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 Deg hue() const { + return 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, Color3) +}; + +#ifndef DOXYGEN_GENERATING_OUTPUT +MAGNUM_VECTORn_OPERATOR_IMPLEMENTATION(3, Color3) +#endif + +/** +@brief Four-component (RGBA) color + +See @ref Color3 for more information. +@see @ref Magnum::Color4, @ref Magnum::Color4ub +*/ +/* Not using template specialization because some internal functions are + impossible to explicitly instantiate */ +#ifndef DOXYGEN_GENERATING_OUTPUT +template +#else +template +#endif +class Color4: public Vector4 { + public: + /** @copydoc Color3::FloatingPointType */ + typedef typename Color3::FloatingPointType FloatingPointType; + + /** @copydoc Color3::HSV */ + typedef typename Color3::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 Color4 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 Color4 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 Color4 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 Color4 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 Color4 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 Color4 yellow(T blue = T(0), T alpha = Implementation::fullChannel()) { + return {Implementation::fullChannel(), Implementation::fullChannel(), blue, alpha}; + } + + /** + * @copydoc Color3::fromHSV() + * @param a Alpha value, defaults to `1.0` for floating-point types + * and maximum positive value for integral types. + */ + constexpr static Color4 fromHSV(HSV hsv, T a = Implementation::fullChannel()) { + return Color4(Implementation::fromHSV(hsv), a); + } + /** @overload */ + constexpr static Color4 fromHSV(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*/ Color4(): Vector4(T(0), T(0), T(0), Implementation::fullChannel()) {} + + /** @copydoc Vector::Vector(ZeroInitT) */ + constexpr explicit Color4(ZeroInitT) + /** @todoc remove workaround when doxygen is sane */ + #ifndef DOXYGEN_GENERATING_OUTPUT + : Vector4{ZeroInit} + #endif + {} + + /** @copydoc Vector::Vector(NoInitT) */ + explicit Color4(NoInitT) + /** @todoc remove workaround when doxygen is sane */ + #ifndef DOXYGEN_GENERATING_OUTPUT + : Vector4{NoInit} + #endif + {} + + /** + * @copydoc Color3::Color3(T) + * @param alpha Alpha value, defaults to `1.0` for floating-point types + * and maximum positive value for integral types. + */ + constexpr explicit Color4(T rgb, T alpha = Implementation::fullChannel()): 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*/ Color4(T r, T g, T b, T a = Implementation::fullChannel()): Vector4(r, g, b, a) {} + + /** + * @brief Constructor + * @param rgb Three-component color + * @param a A value + */ + /* Not marked as explicit, because conversion from Color3 to Color4 + is fairly common, nearly always with A set to 1 */ + constexpr /*implicit*/ Color4(const Vector3& rgb, T a = Implementation::fullChannel()): Vector4(rgb[0], rgb[1], rgb[2], a) {} + + /** + * @copydoc Vector::Vector(const Vector&) + * + * @attention This function doesn't do any (de)normalization, use + * @ref normalize() and @ref denormalize() instead. + * See @ref Color3 class documentation for more information. + */ + template constexpr explicit Color4(const Vector<4, U>& other): Vector4(other) {} + + /** @brief Copy constructor */ + constexpr Color4(const Vector<4, T>& other): Vector4(other) {} + + /** @copydoc Color3::toHSV() */ + constexpr HSV toHSV() const { + return Implementation::toHSV(Vector4::rgb()); + } + + /** @copydoc Color3::hue() */ + constexpr Deg hue() const { + return Implementation::hue(Vector4::rgb()); + } + + /** @copydoc Color3::saturation() */ + constexpr FloatingPointType saturation() const { + return Implementation::saturation(Vector4::rgb()); + } + + /** @copydoc Color3::value() */ + constexpr FloatingPointType value() const { + return Implementation::value(Vector4::rgb()); + } + + MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(4, Color4) +}; + +#ifndef DOXYGEN_GENERATING_OUTPUT +MAGNUM_VECTORn_OPERATOR_IMPLEMENTATION(4, Color4) +#endif + +/** @debugoperator{Magnum::Math::Color3} */ +template inline Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Color3& value) { + return debug << static_cast&>(value); +} + +/** @debugoperator{Magnum::Math::Color4} */ +template inline Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Color4& value) { + return debug << static_cast&>(value); +} + +namespace Implementation { + template struct TypeForSize<3, Color3> { typedef Color3 Type; }; + template struct TypeForSize<3, Color4> { typedef Color3 Type; }; + template struct TypeForSize<4, Color3> { typedef Color4 Type; }; + template struct TypeForSize<4, Color4> { typedef Color4 Type; }; +} + +}} + +namespace Corrade { namespace Utility { + /** @configurationvalue{Magnum::Color3} */ + template struct ConfigurationValue>: public ConfigurationValue> {}; + + /** @configurationvalue{Magnum::Color4} */ + template struct ConfigurationValue>: public ConfigurationValue> {}; +}} + +#endif diff --git a/src/Magnum/Math/Math.h b/src/Magnum/Math/Math.h index 0827c2ce9..40fff93d0 100644 --- a/src/Magnum/Math/Math.h +++ b/src/Magnum/Math/Math.h @@ -73,6 +73,9 @@ template class Vector2; template class Vector3; template class Vector4; +template class Color3; +template class Color4; + template class Range; template using Range1D = Range<1, T>; template class Range2D; diff --git a/src/Magnum/Math/Swizzle.h b/src/Magnum/Math/Swizzle.h index 5a83e55a6..14da89371 100644 --- a/src/Magnum/Math/Swizzle.h +++ b/src/Magnum/Math/Swizzle.h @@ -78,7 +78,7 @@ four-component, corresponding @ref Math::Vector2, @ref Math::Vector3, @ref Math::Vector4, @ref Color3 or @ref Color4 specialization is returned. @see @ref matrix-vector-component-access, @ref Vector4::xyz(), - @ref Vector4::xy(), @ref Vector3::xy(), @ref BasicColor4::rgb() + @ref Vector4::rgb(), @ref Vector4::xy(), @ref Vector3::xy() */ template constexpr typename Implementation::TypeForSize::Type swizzle(const T& vector) { return {Implementation::Component::value(vector)...}; diff --git a/src/Magnum/Math/Test/CMakeLists.txt b/src/Magnum/Math/Test/CMakeLists.txt index f504b0036..13596b1a6 100644 --- a/src/Magnum/Math/Test/CMakeLists.txt +++ b/src/Magnum/Math/Test/CMakeLists.txt @@ -32,6 +32,7 @@ corrade_add_test(MathVectorTest VectorTest.cpp LIBRARIES MagnumMathTestLib) corrade_add_test(MathVector2Test Vector2Test.cpp LIBRARIES MagnumMathTestLib) corrade_add_test(MathVector3Test Vector3Test.cpp LIBRARIES MagnumMathTestLib) corrade_add_test(MathVector4Test Vector4Test.cpp LIBRARIES MagnumMathTestLib) +corrade_add_test(MathColorTest ColorTest.cpp LIBRARIES MagnumMathTestLib) corrade_add_test(MathRectangularMatrixTest RectangularMatrixTest.cpp LIBRARIES MagnumMathTestLib) corrade_add_test(MathMatrixTest MatrixTest.cpp LIBRARIES MagnumMathTestLib) diff --git a/src/Magnum/Test/ColorTest.cpp b/src/Magnum/Math/Test/ColorTest.cpp similarity index 75% rename from src/Magnum/Test/ColorTest.cpp rename to src/Magnum/Math/Test/ColorTest.cpp index 12c295041..8ba0f607e 100644 --- a/src/Magnum/Test/ColorTest.cpp +++ b/src/Magnum/Math/Test/ColorTest.cpp @@ -27,11 +27,11 @@ #include #include -#include "Magnum/Color.h" +#include "Magnum/Math/Color.h" -namespace Magnum { namespace Test { +namespace Magnum { namespace Math { namespace Test { -struct ColorTest: TestSuite::Tester { +struct ColorTest: Corrade::TestSuite::Tester { explicit ColorTest(); void construct(); @@ -63,6 +63,16 @@ struct ColorTest: TestSuite::Tester { void configuration(); }; +typedef Math::Vector3 Vector3; +typedef Math::Color3 Color3; +typedef Math::Color3 Color3ub; + +typedef Math::Vector4 Vector4; +typedef Math::Color4 Color4; +typedef Math::Color4 Color4ub; + +typedef Math::Deg Deg; + ColorTest::ColorTest() { addTests({&ColorTest::construct, &ColorTest::constructDefault, @@ -233,25 +243,25 @@ void ColorTest::colors() { } void ColorTest::fromHue() { - CORRADE_COMPARE(Color3ub::fromHSV(Deg(27.0f), 1.0f, 1.0f), Color3ub(255, 114, 0)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(86.0f), 1.0f, 1.0f), Color3ub(144, 255, 0)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(134.0f), 1.0f, 1.0f), Color3ub(0, 255, 59)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(191.0f), 1.0f, 1.0f), Color3ub(0, 208, 255)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(269.0f), 1.0f, 1.0f), Color3ub(123, 0, 255)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(317.0f), 1.0f, 1.0f), Color3ub(255, 0, 182)); + CORRADE_COMPARE(Color3ub::fromHSV(27.0_degf, 1.0f, 1.0f), Color3ub(255, 114, 0)); + CORRADE_COMPARE(Color3ub::fromHSV(86.0_degf, 1.0f, 1.0f), Color3ub(144, 255, 0)); + CORRADE_COMPARE(Color3ub::fromHSV(134.0_degf, 1.0f, 1.0f), Color3ub(0, 255, 59)); + CORRADE_COMPARE(Color3ub::fromHSV(191.0_degf, 1.0f, 1.0f), Color3ub(0, 208, 255)); + CORRADE_COMPARE(Color3ub::fromHSV(269.0_degf, 1.0f, 1.0f), Color3ub(123, 0, 255)); + CORRADE_COMPARE(Color3ub::fromHSV(317.0_degf, 1.0f, 1.0f), Color3ub(255, 0, 182)); } void ColorTest::hue() { - CORRADE_COMPARE(Color3ub(255, 115, 0).hue(), Deg(27.058824f)); - CORRADE_COMPARE(Color3ub(145, 255, 0).hue(), Deg(85.882353f)); - CORRADE_COMPARE(Color3ub(0, 255, 60).hue(), Deg(134.11765f)); - CORRADE_COMPARE(Color3ub(0, 208, 255).hue(), Deg(191.05882f)); - CORRADE_COMPARE(Color3ub(123, 0, 255).hue(), Deg(268.94117f)); - CORRADE_COMPARE(Color3ub(255, 0, 183).hue(), Deg(316.94117f)); + CORRADE_COMPARE(Color3ub(255, 115, 0).hue(), 27.058824_degf); + CORRADE_COMPARE(Color3ub(145, 255, 0).hue(), 85.882353_degf); + CORRADE_COMPARE(Color3ub(0, 255, 60).hue(), 134.11765_degf); + CORRADE_COMPARE(Color3ub(0, 208, 255).hue(), 191.05882_degf); + CORRADE_COMPARE(Color3ub(123, 0, 255).hue(), 268.94117_degf); + CORRADE_COMPARE(Color3ub(255, 0, 183).hue(), 316.94117_degf); } void ColorTest::fromSaturation() { - CORRADE_COMPARE(Color3ub::fromHSV(Deg(0.0f), 0.702f, 1.0f), Color3ub(255, 75, 75)); + CORRADE_COMPARE(Color3ub::fromHSV(0.0_degf, 0.702f, 1.0f), Color3ub(255, 75, 75)); } void ColorTest::saturation() { @@ -260,7 +270,7 @@ void ColorTest::saturation() { } void ColorTest::fromValue() { - CORRADE_COMPARE(Color3ub::fromHSV(Deg(0.0f), 1.0f, 0.522f), Color3ub(133, 0, 0)); + CORRADE_COMPARE(Color3ub::fromHSV(0.0_degf, 1.0f, 0.522f), Color3ub(133, 0, 0)); } void ColorTest::value() { @@ -268,35 +278,35 @@ void ColorTest::value() { } void ColorTest::hsv() { - CORRADE_COMPARE(Color3ub::fromHSV(Deg(230.0f), 0.749f, 0.427f), Color3ub(27, 40, 108)); + CORRADE_COMPARE(Color3ub::fromHSV(230.0_degf, 0.749f, 0.427f), Color3ub(27, 40, 108)); Deg hue; Float saturation, value; std::tie(hue, saturation, value) = Color3ub(27, 41, 109).toHSV(); - CORRADE_COMPARE(hue, Deg(229.756106f)); + CORRADE_COMPARE(hue, 229.756106_degf); CORRADE_COMPARE(saturation, 0.752294f); CORRADE_COMPARE(value, 0.427451f); } void ColorTest::hsvOverflow() { - CORRADE_COMPARE(Color3ub::fromHSV(Deg(27.0f-360.0f), 1.0f, 1.0f), Color3ub(255, 114, 0)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(86.0f-360.0f), 1.0f, 1.0f), Color3ub(144, 255, 0)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(134.0f-360.0f), 1.0f, 1.0f), Color3ub(0, 255, 59)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(191.0f-360.0f), 1.0f, 1.0f), Color3ub(0, 208, 255)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(269.0f-360.0f), 1.0f, 1.0f), Color3ub(123, 0, 255)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(317.0f-360.0f), 1.0f, 1.0f), Color3ub(255, 0, 182)); - - CORRADE_COMPARE(Color3ub::fromHSV(Deg(360.0f+27.0f), 1.0f, 1.0f), Color3ub(255, 114, 0)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(360.0f+86.0f), 1.0f, 1.0f), Color3ub(144, 255, 0)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(360.0f+134.0f), 1.0f, 1.0f), Color3ub(0, 255, 59)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(360.0f+191.0f), 1.0f, 1.0f), Color3ub(0, 208, 255)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(360.0f+269.0f), 1.0f, 1.0f), Color3ub(123, 0, 255)); - CORRADE_COMPARE(Color3ub::fromHSV(Deg(360.0f+317.0f), 1.0f, 1.0f), Color3ub(255, 0, 182)); + CORRADE_COMPARE(Color3ub::fromHSV(27.0_degf - 360.0_degf, 1.0f, 1.0f), Color3ub(255, 114, 0)); + CORRADE_COMPARE(Color3ub::fromHSV(86.0_degf - 360.0_degf, 1.0f, 1.0f), Color3ub(144, 255, 0)); + CORRADE_COMPARE(Color3ub::fromHSV(134.0_degf - 360.0_degf, 1.0f, 1.0f), Color3ub(0, 255, 59)); + CORRADE_COMPARE(Color3ub::fromHSV(191.0_degf - 360.0_degf, 1.0f, 1.0f), Color3ub(0, 208, 255)); + CORRADE_COMPARE(Color3ub::fromHSV(269.0_degf - 360.0_degf, 1.0f, 1.0f), Color3ub(123, 0, 255)); + CORRADE_COMPARE(Color3ub::fromHSV(317.0_degf - 360.0_degf, 1.0f, 1.0f), Color3ub(255, 0, 182)); + + CORRADE_COMPARE(Color3ub::fromHSV(360.0_degf + 27.0_degf, 1.0f, 1.0f), Color3ub(255, 114, 0)); + CORRADE_COMPARE(Color3ub::fromHSV(360.0_degf + 86.0_degf, 1.0f, 1.0f), Color3ub(144, 255, 0)); + CORRADE_COMPARE(Color3ub::fromHSV(360.0_degf + 134.0_degf, 1.0f, 1.0f), Color3ub(0, 255, 59)); + CORRADE_COMPARE(Color3ub::fromHSV(360.0_degf + 191.0_degf, 1.0f, 1.0f), Color3ub(0, 208, 255)); + CORRADE_COMPARE(Color3ub::fromHSV(360.0_degf + 269.0_degf, 1.0f, 1.0f), Color3ub(123, 0, 255)); + CORRADE_COMPARE(Color3ub::fromHSV(360.0_degf + 317.0_degf, 1.0f, 1.0f), Color3ub(255, 0, 182)); } void ColorTest::hsvAlpha() { - CORRADE_COMPARE(Color4ub::fromHSV(std::make_tuple(Deg(230.0f), 0.749f, 0.427f), 23), Color4ub(27, 40, 108, 23)); - CORRADE_COMPARE(Color4ub::fromHSV(Deg(230.0f), 0.749f, 0.427f, 23), Color4ub(27, 40, 108, 23)); + CORRADE_COMPARE(Color4ub::fromHSV(std::make_tuple(230.0_degf, 0.749f, 0.427f), 23), Color4ub(27, 40, 108, 23)); + CORRADE_COMPARE(Color4ub::fromHSV(230.0_degf, 0.749f, 0.427f, 23), Color4ub(27, 40, 108, 23)); } void ColorTest::swizzleType() { @@ -327,7 +337,7 @@ void ColorTest::debug() { } void ColorTest::configuration() { - Utility::Configuration c; + Corrade::Utility::Configuration c; Color3 color3(0.5f, 0.75f, 1.0f); std::string value3("0.5 0.75 1"); @@ -344,6 +354,6 @@ void ColorTest::configuration() { CORRADE_COMPARE(c.value("color4"), color4); } -}} +}}} -CORRADE_TEST_MAIN(Magnum::Test::ColorTest) +CORRADE_TEST_MAIN(Magnum::Math::Test::ColorTest) diff --git a/src/Magnum/Renderer.cpp b/src/Magnum/Renderer.cpp index 9c7bb2b96..8b146a959 100644 --- a/src/Magnum/Renderer.cpp +++ b/src/Magnum/Renderer.cpp @@ -25,10 +25,10 @@ #include "Renderer.h" -#include "Magnum/Math/Range.h" -#include "Magnum/Color.h" #include "Magnum/Context.h" #include "Magnum/Extensions.h" +#include "Magnum/Math/Color.h" +#include "Magnum/Math/Range.h" #include "Implementation/State.h" #include "Implementation/RendererState.h" diff --git a/src/Magnum/Shaders/DistanceFieldVector.h b/src/Magnum/Shaders/DistanceFieldVector.h index ed15e70be..e66b73269 100644 --- a/src/Magnum/Shaders/DistanceFieldVector.h +++ b/src/Magnum/Shaders/DistanceFieldVector.h @@ -29,8 +29,8 @@ * @brief Class @ref Magnum::Shaders::DistanceFieldVector, typedef @ref Magnum::Shaders::DistanceFieldVector2D, @ref Magnum::Shaders::DistanceFieldVector3D */ -#include "Magnum/Color.h" #include "Magnum/DimensionTraits.h" +#include "Magnum/Math/Color.h" #include "Magnum/Math/Matrix3.h" #include "Magnum/Math/Matrix4.h" #include "Magnum/Shaders/AbstractVector.h" diff --git a/src/Magnum/Shaders/Flat.h b/src/Magnum/Shaders/Flat.h index 0e306dbe9..a54c520c8 100644 --- a/src/Magnum/Shaders/Flat.h +++ b/src/Magnum/Shaders/Flat.h @@ -29,8 +29,8 @@ * @brief Class @ref Magnum::Shaders::Flat, typedef @ref Magnum::Shaders::Flat2D, @ref Magnum::Shaders::Flat3D */ -#include "Magnum/Color.h" #include "Magnum/DimensionTraits.h" +#include "Magnum/Math/Color.h" #include "Magnum/Math/Matrix3.h" #include "Magnum/Math/Matrix4.h" #include "Magnum/Shaders/Generic.h" diff --git a/src/Magnum/Shaders/MeshVisualizer.h b/src/Magnum/Shaders/MeshVisualizer.h index 44b235edb..6c2f2ee1f 100644 --- a/src/Magnum/Shaders/MeshVisualizer.h +++ b/src/Magnum/Shaders/MeshVisualizer.h @@ -29,9 +29,9 @@ * @brief Class @ref Magnum::Shaders::MeshVisualizer */ -#include "Magnum/Math/Matrix4.h" #include "Magnum/AbstractShaderProgram.h" -#include "Magnum/Color.h" +#include "Magnum/Math/Color.h" +#include "Magnum/Math/Matrix4.h" #include "Magnum/Shaders/visibility.h" diff --git a/src/Magnum/Shaders/Phong.h b/src/Magnum/Shaders/Phong.h index 26496b487..170ff122d 100644 --- a/src/Magnum/Shaders/Phong.h +++ b/src/Magnum/Shaders/Phong.h @@ -29,7 +29,7 @@ * @brief Class @ref Magnum::Shaders::Phong */ -#include "Magnum/Color.h" +#include "Magnum/Math/Color.h" #include "Magnum/Math/Matrix4.h" #include "Magnum/Shaders/Generic.h" #include "Magnum/Shaders/visibility.h" diff --git a/src/Magnum/Shaders/Vector.h b/src/Magnum/Shaders/Vector.h index fc9024bbf..b3640132c 100644 --- a/src/Magnum/Shaders/Vector.h +++ b/src/Magnum/Shaders/Vector.h @@ -29,8 +29,8 @@ * @brief Class @ref Magnum::Shaders::Vector, typedef @ref Magnum::Shaders::Vector2D, @ref Magnum::Shaders::Vector3D */ -#include "Magnum/Color.h" #include "Magnum/DimensionTraits.h" +#include "Magnum/Math/Color.h" #include "Magnum/Math/Matrix3.h" #include "Magnum/Math/Matrix4.h" #include "Magnum/Shaders/AbstractVector.h" diff --git a/src/Magnum/Shaders/VertexColor.h b/src/Magnum/Shaders/VertexColor.h index 3f98b61dc..15f78a509 100644 --- a/src/Magnum/Shaders/VertexColor.h +++ b/src/Magnum/Shaders/VertexColor.h @@ -29,8 +29,8 @@ * @brief Class @ref Magnum::Shaders::VertexColor */ -#include "Magnum/Color.h" #include "Magnum/DimensionTraits.h" +#include "Magnum/Math/Color.h" #include "Magnum/Math/Matrix3.h" #include "Magnum/Math/Matrix4.h" #include "Magnum/Shaders/Generic.h" diff --git a/src/Magnum/Test/CMakeLists.txt b/src/Magnum/Test/CMakeLists.txt index 82f146533..ecf1168bc 100644 --- a/src/Magnum/Test/CMakeLists.txt +++ b/src/Magnum/Test/CMakeLists.txt @@ -26,7 +26,6 @@ corrade_add_test(AbstractImageTest AbstractImageTest.cpp LIBRARIES Magnum) corrade_add_test(AbstractShaderProgramTest AbstractShaderProgramTest.cpp LIBRARIES Magnum) corrade_add_test(ArrayTest ArrayTest.cpp) -corrade_add_test(ColorTest ColorTest.cpp LIBRARIES MagnumMathTestLib) corrade_add_test(FormatTest FormatTest.cpp LIBRARIES Magnum) corrade_add_test(ContextTest ContextTest.cpp LIBRARIES Magnum) corrade_add_test(DebugOutputTest DebugOutputTest.cpp LIBRARIES Magnum) diff --git a/src/Magnum/Test/CubeMapTextureArrayGLTest.cpp b/src/Magnum/Test/CubeMapTextureArrayGLTest.cpp index 76dd2db04..3f344ff9e 100644 --- a/src/Magnum/Test/CubeMapTextureArrayGLTest.cpp +++ b/src/Magnum/Test/CubeMapTextureArrayGLTest.cpp @@ -26,11 +26,11 @@ #include #include "Magnum/BufferImage.h" -#include "Magnum/Color.h" #include "Magnum/ColorFormat.h" #include "Magnum/CubeMapTextureArray.h" #include "Magnum/Image.h" #include "Magnum/TextureFormat.h" +#include "Magnum/Math/Color.h" #include "Magnum/Math/Range.h" #include "Magnum/Test/AbstractOpenGLTester.h" diff --git a/src/Magnum/Test/CubeMapTextureGLTest.cpp b/src/Magnum/Test/CubeMapTextureGLTest.cpp index 6248545f4..4e8b6bb6b 100644 --- a/src/Magnum/Test/CubeMapTextureGLTest.cpp +++ b/src/Magnum/Test/CubeMapTextureGLTest.cpp @@ -29,11 +29,11 @@ #ifndef MAGNUM_TARGET_GLES2 #include "Magnum/BufferImage.h" #endif -#include "Magnum/Color.h" #include "Magnum/ColorFormat.h" #include "Magnum/CubeMapTexture.h" #include "Magnum/Image.h" #include "Magnum/TextureFormat.h" +#include "Magnum/Math/Color.h" #include "Magnum/Math/Range.h" #include "Magnum/Test/AbstractOpenGLTester.h" diff --git a/src/Magnum/Test/FramebufferGLTest.cpp b/src/Magnum/Test/FramebufferGLTest.cpp index 99751b770..8671eaf1b 100644 --- a/src/Magnum/Test/FramebufferGLTest.cpp +++ b/src/Magnum/Test/FramebufferGLTest.cpp @@ -24,7 +24,6 @@ */ #include "Magnum/configure.h" -#include "Magnum/Color.h" #include "Magnum/ColorFormat.h" #include "Magnum/Context.h" #include "Magnum/Extensions.h" @@ -34,6 +33,7 @@ #include "Magnum/RenderbufferFormat.h" #include "Magnum/Texture.h" #include "Magnum/TextureFormat.h" +#include "Magnum/Math/Color.h" #include "Magnum/Test/AbstractOpenGLTester.h" #ifndef MAGNUM_TARGET_GLES2 diff --git a/src/Magnum/Test/MeshGLTest.cpp b/src/Magnum/Test/MeshGLTest.cpp index dea2ef8cd..57e882b50 100644 --- a/src/Magnum/Test/MeshGLTest.cpp +++ b/src/Magnum/Test/MeshGLTest.cpp @@ -25,7 +25,6 @@ #include "Magnum/AbstractShaderProgram.h" #include "Magnum/Buffer.h" -#include "Magnum/Color.h" #include "Magnum/ColorFormat.h" #include "Magnum/Framebuffer.h" #include "Magnum/Image.h" @@ -34,6 +33,7 @@ #include "Magnum/Renderbuffer.h" #include "Magnum/RenderbufferFormat.h" #include "Magnum/Shader.h" +#include "Magnum/Math/Color.h" #include "Magnum/Math/Matrix.h" #include "Magnum/Math/Vector4.h" #include "Magnum/Test/AbstractOpenGLTester.h" diff --git a/src/Magnum/Test/RectangleTextureGLTest.cpp b/src/Magnum/Test/RectangleTextureGLTest.cpp index 5404fbe4a..7a90e0841 100644 --- a/src/Magnum/Test/RectangleTextureGLTest.cpp +++ b/src/Magnum/Test/RectangleTextureGLTest.cpp @@ -28,11 +28,11 @@ #include "Magnum/configure.h" #include "Magnum/BufferImage.h" -#include "Magnum/Color.h" #include "Magnum/ColorFormat.h" #include "Magnum/Image.h" #include "Magnum/RectangleTexture.h" #include "Magnum/TextureFormat.h" +#include "Magnum/Math/Color.h" #include "Magnum/Math/Range.h" #include "Magnum/Test/AbstractOpenGLTester.h" diff --git a/src/Magnum/Test/TextureArrayGLTest.cpp b/src/Magnum/Test/TextureArrayGLTest.cpp index 6aae9fbc6..27125e8c4 100644 --- a/src/Magnum/Test/TextureArrayGLTest.cpp +++ b/src/Magnum/Test/TextureArrayGLTest.cpp @@ -27,11 +27,11 @@ #include "Magnum/configure.h" #include "Magnum/BufferImage.h" -#include "Magnum/Color.h" #include "Magnum/ColorFormat.h" #include "Magnum/Image.h" #include "Magnum/TextureArray.h" #include "Magnum/TextureFormat.h" +#include "Magnum/Math/Color.h" #include "Magnum/Math/Range.h" #include "Magnum/Test/AbstractOpenGLTester.h" diff --git a/src/Magnum/Test/TextureGLTest.cpp b/src/Magnum/Test/TextureGLTest.cpp index 7c2a1028d..d7a7be222 100644 --- a/src/Magnum/Test/TextureGLTest.cpp +++ b/src/Magnum/Test/TextureGLTest.cpp @@ -29,11 +29,11 @@ #ifndef MAGNUM_TARGET_GLES2 #include "Magnum/BufferImage.h" #endif -#include "Magnum/Color.h" #include "Magnum/ColorFormat.h" #include "Magnum/Image.h" #include "Magnum/Texture.h" #include "Magnum/TextureFormat.h" +#include "Magnum/Math/Color.h" #include "Magnum/Math/Range.h" #include "Magnum/Test/AbstractOpenGLTester.h"