Browse Source

Added Color*::{red,green.blue,cyan,magenta,yellow}() convenience functions.

pull/34/head
Vladimír Vondruš 13 years ago
parent
commit
22dc8170e8
  1. 126
      src/Color.h
  2. 21
      src/Math/Vector3.h
  3. 31
      src/Test/ColorTest.cpp

126
src/Color.h

@ -157,6 +157,72 @@ range @f$ [0.0, 1.0] @f$.
impossible to explicitly instantiate */
template<class T> class BasicColor3: public Math::Vector3<T> {
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<T> red(T red = Implementation::fullChannel<T>()) {
return Math::Vector3<T>::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<T> green(T green = Implementation::fullChannel<T>()) {
return Math::Vector3<T>::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<T> blue(T blue = Implementation::fullChannel<T>()) {
return Math::Vector3<T>::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<T> cyan(T red = T(0)) {
return {red, Implementation::fullChannel<T>(), Implementation::fullChannel<T>()};
}
/**
* @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<T> magenta(T green = T(0)) {
return {Implementation::fullChannel<T>(), green, Implementation::fullChannel<T>()};
}
/**
* @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<T> yellow(T blue = T(0)) {
return {Implementation::fullChannel<T>(), Implementation::fullChannel<T>(), blue};
}
/** @brief Corresponding floating-point type for HSV computation */
typedef typename Math::TypeTraits<T>::FloatingPointType FloatingPointType;
@ -289,6 +355,66 @@ class BasicColor4: public Math::Vector4<T> {
/** @copydoc BasicColor3::HSV */
typedef typename BasicColor3<T>::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<T> red(T red = Implementation::fullChannel<T>(), T alpha = Implementation::fullChannel<T>()) {
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<T> green(T green = Implementation::fullChannel<T>(), T alpha = Implementation::fullChannel<T>()) {
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<T> blue(T blue = Implementation::fullChannel<T>(), T alpha = Implementation::fullChannel<T>()) {
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<T> cyan(T red = T(0), T alpha = Implementation::fullChannel<T>()) {
return {red, Implementation::fullChannel<T>(), Implementation::fullChannel<T>(), 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<T> magenta(T green = T(0), T alpha = Implementation::fullChannel<T>()) {
return {Implementation::fullChannel<T>(), green, Implementation::fullChannel<T>(), 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<T> yellow(T blue = T(0), T alpha = Implementation::fullChannel<T>()) {
return {Implementation::fullChannel<T>(), Implementation::fullChannel<T>(), blue, alpha};
}
/**
* @copydoc BasicColor3::fromHSV()
* @param a Alpha value, defaults to 1.0 for floating-point types

21
src/Math/Vector3.h

@ -51,23 +51,24 @@ template<class T> 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<T> 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<T> 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<T> zAxis(T length = T(1)) { return {T(0), T(0), length}; }
@ -78,23 +79,23 @@ template<class T> 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<T> 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<T> 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<T> zScale(T scale) { return {T(1), T(1), scale}; }

31
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));

Loading…
Cancel
Save