From 22dc8170e8ca7a160e47f86931a0a914b6c68edd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 19 Nov 2013 22:20:37 +0100 Subject: [PATCH] Added Color*::{red,green.blue,cyan,magenta,yellow}() convenience functions. --- src/Color.h | 126 +++++++++++++++++++++++++++++++++++++++++ src/Math/Vector3.h | 21 +++---- src/Test/ColorTest.cpp | 31 ++++++++++ 3 files changed, 168 insertions(+), 10 deletions(-) diff --git a/src/Color.h b/src/Color.h index c4d3c6aa0..00d6ca14f 100644 --- a/src/Color.h +++ b/src/Color.h @@ -157,6 +157,72 @@ range @f$ [0.0, 1.0] @f$. 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; @@ -289,6 +355,66 @@ class BasicColor4: public Math::Vector4 { /** @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 diff --git a/src/Math/Vector3.h b/src/Math/Vector3.h index 5beb801ca..04147cea0 100644 --- a/src/Math/Vector3.h +++ b/src/Math/Vector3.h @@ -51,23 +51,24 @@ template class Vector3: public Vector<3, T> { * Matrix4::translation(Vector3::xAxis(5.0f)); // same as Matrix4::translation({5.0f, 0.0f, 0.0f}); * Matrix4::rotation(30.0_degf, Vector3::xAxis()); // same as Matrix::rotation(30.0_degf, {1.0f, 0.0f, 0.0f}); * @endcode - * @see yAxis(), zAxis(), xScale(), Matrix4::right() + * @see @ref yAxis(), @ref zAxis(), @ref xScale(), @ref Color3::red(), + * @ref Matrix4::right() */ constexpr static Vector3 xAxis(T length = T(1)) { return {length, T(0), T(0)}; } /** * @brief %Vector in direction of Y axis (up) * - * See xAxis() for more information. - * @see yScale(), Matrix4::up() + * See @ref xAxis() for more information. + * @see @ref yScale(), @ref Color3::green(), @ref Matrix4::up() */ constexpr static Vector3 yAxis(T length = T(1)) { return {T(0), length, T(0)}; } /** * @brief %Vector in direction of Z axis (backward) * - * See xAxis() for more information. - * @see zScale(), Matrix4::backward() + * See @ref xAxis() for more information. + * @see @ref zScale(), @ref Color3::blue(), @ref Matrix4::backward() */ constexpr static Vector3 zAxis(T length = T(1)) { return {T(0), T(0), length}; } @@ -78,23 +79,23 @@ template class Vector3: public Vector<3, T> { * @code * Matrix4::scaling(Vector3::xScale(-2.0f)); // same as Matrix4::scaling({-2.0f, 1.0f, 1.0f}); * @endcode - * @see yScale(), zScale(), xAxis() + * @see @ref yScale(), @ref zScale(), @ref Color3::cyan(), @ref xAxis() */ constexpr static Vector3 xScale(T scale) { return {scale, T(1), T(1)}; } /** * @brief Scaling vector in direction of Y axis (height) * - * See xScale() for more information. - * @see yAxis() + * See @ref xScale() for more information. + * @see @ref yAxis(), @ref Color3::magenta() */ constexpr static Vector3 yScale(T scale) { return {T(1), scale, T(1)}; } /** * @brief Scaling vector in direction of Z axis (depth) * - * See xScale() for more information. - * @see zAxis() + * See @ref xScale() for more information. + * @see @ref zAxis(), @ref Color3::yellow() */ constexpr static Vector3 zScale(T scale) { return {T(1), T(1), scale}; } diff --git a/src/Test/ColorTest.cpp b/src/Test/ColorTest.cpp index 434e43460..f138605b2 100644 --- a/src/Test/ColorTest.cpp +++ b/src/Test/ColorTest.cpp @@ -42,6 +42,8 @@ class ColorTest: public TestSuite::Tester { void constructNormalization(); void constructCopy(); + void colors(); + void fromHue(); void fromSaturation(); void fromValue(); @@ -71,6 +73,8 @@ ColorTest::ColorTest() { &ColorTest::constructNormalization, &ColorTest::constructCopy, + &ColorTest::colors, + &ColorTest::fromHue, &ColorTest::fromSaturation, &ColorTest::fromValue, @@ -181,6 +185,33 @@ void ColorTest::constructCopy() { CORRADE_COMPARE(d, Color4(1.0f, 0.5f, 0.75f, 0.25f)); } +void ColorTest::colors() { + CORRADE_COMPARE(Color3ub::red(75), Color3ub(75, 0, 0)); + CORRADE_COMPARE(Color3ub::green(75), Color3ub(0, 75, 0)); + CORRADE_COMPARE(Color3ub::blue(75), Color3ub(0, 0, 75)); + + CORRADE_COMPARE(Color3ub::cyan(75), Color3ub(75, 255, 255)); + CORRADE_COMPARE(Color3ub::magenta(75), Color3ub(255, 75, 255)); + CORRADE_COMPARE(Color3ub::yellow(75), Color3ub(255, 255, 75)); + + CORRADE_COMPARE(Color4ub::red(75, 138), Color4ub(75, 0, 0, 138)); + CORRADE_COMPARE(Color4ub::green(75, 138), Color4ub(0, 75, 0, 138)); + CORRADE_COMPARE(Color4ub::blue(75, 138), Color4ub(0, 0, 75, 138)); + + CORRADE_COMPARE(Color4ub::cyan(75, 138), Color4ub(75, 255, 255, 138)); + CORRADE_COMPARE(Color4ub::magenta(75, 138), Color4ub(255, 75, 255, 138)); + CORRADE_COMPARE(Color4ub::yellow(75, 138), Color4ub(255, 255, 75, 138)); + + /* Default alpha */ + CORRADE_COMPARE(Color4ub::red(75), Color4ub(75, 0, 0, 255)); + CORRADE_COMPARE(Color4ub::green(75), Color4ub(0, 75, 0, 255)); + CORRADE_COMPARE(Color4ub::blue(75), Color4ub(0, 0, 75, 255)); + + CORRADE_COMPARE(Color4ub::cyan(75), Color4ub(75, 255, 255, 255)); + CORRADE_COMPARE(Color4ub::magenta(75), Color4ub(255, 75, 255, 255)); + CORRADE_COMPARE(Color4ub::yellow(75), Color4ub(255, 255, 75, 255)); +} + 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));