Browse Source

Moved RGB(A) accessors from Color to Vector.

This functionality doesn't depend on anything from Color itself and it
is inconvenient to cast the vector to Color just to use them.
pull/23/head
Vladimír Vondruš 13 years ago
parent
commit
f7a776d397
  1. 33
      src/Color.h
  2. 9
      src/Math/Test/Vector3Test.cpp
  3. 13
      src/Math/Test/Vector4Test.cpp
  4. 48
      src/Math/Vector3.h
  5. 77
      src/Math/Vector4.h
  6. 30
      src/Test/ColorTest.cpp

33
src/Color.h

@ -202,13 +202,6 @@ template<class T> class BasicColor3: public Math::Vector3<T> {
/** @brief Copy constructor */
constexpr BasicColor3(const Math::Vector<3, T>& other): Math::Vector3<T>(other) {}
T& r() { return Math::Vector3<T>::x(); } /**< @brief R component */
constexpr T r() const { return Math::Vector3<T>::x(); } /**< @overload */
T& g() { return Math::Vector3<T>::y(); } /**< @brief G component */
constexpr T g() const { return Math::Vector3<T>::y(); } /**< @overload */
T& b() { return Math::Vector3<T>::z(); } /**< @brief B component */
constexpr T b() const { return Math::Vector3<T>::z(); } /**< @overload */
/**
* @brief Convert to HSV
*
@ -336,42 +329,24 @@ class BasicColor4: public Math::Vector4<T> {
/** @brief Copy constructor */
constexpr BasicColor4(const Math::Vector<4, T>& other): Math::Vector4<T>(other) {}
T& r() { return Math::Vector4<T>::x(); } /**< @brief R component */
constexpr T r() const { return Math::Vector4<T>::x(); } /**< @overload */
T& g() { return Math::Vector4<T>::y(); } /**< @brief G component */
constexpr T g() const { return Math::Vector4<T>::y(); } /**< @overload */
T& b() { return Math::Vector4<T>::z(); } /**< @brief B component */
constexpr T b() const { return Math::Vector4<T>::z(); } /**< @overload */
T& a() { return Math::Vector4<T>::w(); } /**< @brief A component */
constexpr T a() const { return Math::Vector4<T>::w(); } /**< @overload */
/**
* @brief RGB part of the vector
* @return First three components of the vector
*
* @see swizzle()
*/
BasicColor3<T>& rgb() { return BasicColor3<T>::from(Math::Vector4<T>::data()); }
constexpr BasicColor3<T> rgb() const { return BasicColor3<T>::from(Math::Vector4<T>::data()); } /**< @overload */
/** @copydoc BasicColor3::toHSV() */
constexpr HSV toHSV() const {
return Implementation::toHSV<T>(rgb());
return Implementation::toHSV<T>(Math::Vector4<T>::rgb());
}
/** @copydoc BasicColor3::hue() */
constexpr Math::Deg<FloatingPointType> hue() const {
return Implementation::hue<T>(rgb());
return Implementation::hue<T>(Math::Vector4<T>::rgb());
}
/** @copydoc BasicColor3::saturation() */
constexpr FloatingPointType saturation() const {
return Implementation::saturation<T>(rgb());
return Implementation::saturation<T>(Math::Vector4<T>::rgb());
}
/** @copydoc BasicColor3::value() */
constexpr FloatingPointType value() const {
return Implementation::value<T>(rgb());
return Implementation::value<T>(Math::Vector4<T>::rgb());
}
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(4, BasicColor4)

9
src/Math/Test/Vector3Test.cpp

@ -167,16 +167,25 @@ void Vector3Test::convert() {
void Vector3Test::access() {
Vector3 vec(1.0f, -2.0f, 5.0f);
CORRADE_COMPARE(vec.x(), 1.0f);
CORRADE_COMPARE(vec.r(), 1.0f);
CORRADE_COMPARE(vec.y(), -2.0f);
CORRADE_COMPARE(vec.g(), -2.0f);
CORRADE_COMPARE(vec.z(), 5.0f);
CORRADE_COMPARE(vec.b(), 5.0f);
constexpr Vector3 cvec(1.0f, -2.0f, 5.0f);
constexpr Float x = cvec.x();
constexpr Float r = cvec.r();
constexpr Float y = cvec.y();
constexpr Float g = cvec.g();
constexpr Float z = cvec.z();
constexpr Float b = cvec.b();
CORRADE_COMPARE(x, 1.0f);
CORRADE_COMPARE(r, 1.0f);
CORRADE_COMPARE(y, -2.0f);
CORRADE_COMPARE(g, -2.0f);
CORRADE_COMPARE(z, 5.0f);
CORRADE_COMPARE(b, 5.0f);
}
void Vector3Test::cross() {

13
src/Math/Test/Vector4Test.cpp

@ -165,24 +165,37 @@ void Vector4Test::convert() {
void Vector4Test::access() {
Vector4 vec(1.0f, -2.0f, 5.0f, 0.5f);
CORRADE_COMPARE(vec.x(), 1.0f);
CORRADE_COMPARE(vec.r(), 1.0f);
CORRADE_COMPARE(vec.y(), -2.0f);
CORRADE_COMPARE(vec.g(), -2.0f);
CORRADE_COMPARE(vec.z(), 5.0f);
CORRADE_COMPARE(vec.b(), 5.0f);
CORRADE_COMPARE(vec.w(), 0.5f);
CORRADE_COMPARE(vec.a(), 0.5f);
constexpr Vector4 cvec(1.0f, -2.0f, 5.0f, 0.5f);
constexpr Float x = cvec.x();
constexpr Float r = cvec.r();
constexpr Float y = cvec.y();
constexpr Float g = cvec.g();
constexpr Float z = cvec.z();
constexpr Float b = cvec.b();
constexpr Float w = cvec.w();
constexpr Float a = cvec.a();
CORRADE_COMPARE(x, 1.0f);
CORRADE_COMPARE(r, 1.0f);
CORRADE_COMPARE(y, -2.0f);
CORRADE_COMPARE(g, -2.0f);
CORRADE_COMPARE(z, 5.0f);
CORRADE_COMPARE(b, 5.0f);
CORRADE_COMPARE(w, 0.5f);
CORRADE_COMPARE(a, 0.5f);
}
void Vector4Test::threeComponent() {
Vector4 a(1.0f, 2.0f, 3.0f, 4.0f);
CORRADE_COMPARE(a.xyz(), Vector3(1.0f, 2.0f, 3.0f));
CORRADE_COMPARE(a.rgb(), Vector3(1.0f, 2.0f, 3.0f));
constexpr Vector4 b(1.0f, 2.0f, 3.0f, 4.0f);
constexpr Vector3 c = b.xyz();

48
src/Math/Vector3.h

@ -145,13 +145,55 @@ template<class T> class Vector3: public Vector<3, T> {
/** @brief Copy constructor */
constexpr Vector3(const Vector<3, T>& other): Vector<3, T>(other) {}
T& x() { return (*this)[0]; } /**< @brief X component */
/**
* @brief X component
*
* @see @ref r()
*/
T& x() { return (*this)[0]; }
constexpr T x() const { return (*this)[0]; } /**< @overload */
T& y() { return (*this)[1]; } /**< @brief Y component */
/**
* @brief Y component
*
* @see @ref g()
*/
T& y() { return (*this)[1]; }
constexpr T y() const { return (*this)[1]; } /**< @overload */
T& z() { return (*this)[2]; } /**< @brief Z component */
/**
* @brief Z component
*
* @see @ref b()
*/
T& z() { return (*this)[2]; }
constexpr T z() const { return (*this)[2]; } /**< @overload */
/**
* @brief R component
*
* Equivalent to @ref x().
*/
T& r() { return x(); }
constexpr T r() const { return x(); } /**< @overload */
/**
* @brief G component
*
* Equivalent to @ref y().
*/
T& g() { return y(); }
constexpr T g() const { return y(); } /**< @overload */
/**
* @brief B component
*
* Equivalent to @ref z().
*/
T& b() { return z(); }
constexpr T b() const { return z(); } /**< @overload */
/**
* @brief XY part of the vector
* @return First two components of the vector

77
src/Math/Vector4.h

@ -75,29 +75,94 @@ template<class T> class Vector4: public Vector<4, T> {
/** @brief Copy constructor */
constexpr Vector4(const Vector<4, T>& other): Vector<4, T>(other) {}
T& x() { return (*this)[0]; } /**< @brief X component */
/**
* @brief X component
*
* @see @ref r()
*/
T& x() { return (*this)[0]; }
constexpr T x() const { return (*this)[0]; } /**< @overload */
T& y() { return (*this)[1]; } /**< @brief Y component */
/**
* @brief Y component
*
* @see @ref g()
*/
T& y() { return (*this)[1]; }
constexpr T y() const { return (*this)[1]; } /**< @overload */
T& z() { return (*this)[2]; } /**< @brief Z component */
/**
* @brief Z component
*
* @see @ref b()
*/
T& z() { return (*this)[2]; }
constexpr T z() const { return (*this)[2]; } /**< @overload */
T& w() { return (*this)[3]; } /**< @brief W component */
/**
* @brief W component
*
* @see @ref a()
*/
T& w() { return (*this)[3]; }
constexpr T w() const { return (*this)[3]; } /**< @overload */
/**
* @brief R component
*
* Equivalent to @ref x().
*/
T& r() { return x(); }
constexpr T r() const { return x(); } /**< @overload */
/**
* @brief G component
*
* Equivalent to @ref y().
*/
T& g() { return y(); }
constexpr T g() const { return y(); } /**< @overload */
/**
* @brief B component
*
* Equivalent to @ref z().
*/
T& b() { return z(); }
constexpr T b() const { return z(); } /**< @overload */
/**
* @brief A component
*
* Equivalent to @ref w().
*/
T& a() { return w(); }
constexpr T a() const { return w(); } /**< @overload */
/**
* @brief XYZ part of the vector
* @return First three components of the vector
*
* @see swizzle()
* @see @ref swizzle(), @ref rgb()
*/
Vector3<T>& xyz() { return Vector3<T>::from(Vector<4, T>::data()); }
constexpr const Vector3<T> xyz() const { return {x(), y(), z()}; } /**< @overload */
/**
* @brief RGB part of the vector
* @return First three components of the vector
*
* Equivalent to @ref xyz().
* @see @ref swizzle()
*/
Vector3<T>& rgb() { return xyz(); }
constexpr const Vector3<T> rgb() const { return xyz(); } /**< @overload */
/**
* @brief XY part of the vector
* @return First two components of the vector
*
* @see swizzle()
* @see @ref swizzle()
*/
Vector2<T>& xy() { return Vector2<T>::from(Vector<4, T>::data()); }
constexpr const Vector2<T> xy() const { return {x(), y()}; } /**< @overload */

30
src/Test/ColorTest.cpp

@ -34,8 +34,6 @@ class ColorTest: public TestSuite::Tester {
public:
ColorTest();
void access();
void fromHue();
void fromSaturation();
void fromValue();
@ -56,9 +54,7 @@ typedef Magnum::BasicColor3<UnsignedByte> Color3ub;
typedef Magnum::BasicColor4<UnsignedByte> Color4ub;
ColorTest::ColorTest() {
addTests({&ColorTest::access,
&ColorTest::fromHue,
addTests({&ColorTest::fromHue,
&ColorTest::fromSaturation,
&ColorTest::fromValue,
@ -74,30 +70,6 @@ ColorTest::ColorTest() {
&ColorTest::configuration});
}
void ColorTest::access() {
Color3ub c3(15, 255, 10);
const Color3ub cc3(15, 255, 10);
CORRADE_COMPARE(c3.r(), 15);
CORRADE_COMPARE(c3.g(), 255);
CORRADE_COMPARE(c3.b(), 10);
CORRADE_COMPARE(cc3.r(), 15);
CORRADE_COMPARE(cc3.g(), 255);
CORRADE_COMPARE(cc3.b(), 10);
Color4ub c4(125, 98, 51, 22);
const Color4ub cc4(125, 98, 51, 22);
CORRADE_COMPARE(c4.r(), 125);
CORRADE_COMPARE(c4.g(), 98);
CORRADE_COMPARE(c4.b(), 51);
CORRADE_COMPARE(c4.a(), 22);
CORRADE_COMPARE(cc4.r(), 125);
CORRADE_COMPARE(cc4.g(), 98);
CORRADE_COMPARE(cc4.b(), 51);
CORRADE_COMPARE(cc4.a(), 22);
}
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