Browse Source

Math: renamed Color[34]::*HSV types and functions to Color[34]::*Hsv.

For consistency with naming scheme for other color spaces (upcoming
Srgb, Xyz, Lab etc.). The old uppercase names are now marked as
deprecated and will be removed in some future release.
pull/190/head
Vladimír Vondruš 10 years ago
parent
commit
c27a12bb30
  1. 120
      src/Magnum/Math/Color.h
  2. 54
      src/Magnum/Math/Test/ColorTest.cpp

120
src/Magnum/Math/Color.h

@ -39,7 +39,7 @@ namespace Magnum { namespace Math {
namespace Implementation { namespace Implementation {
/* Convert color from HSV */ /* Convert color from HSV */
template<class T> typename std::enable_if<std::is_floating_point<T>::value, Color3<T>>::type fromHSV(typename Color3<T>::HSV hsv) { template<class T> typename std::enable_if<std::is_floating_point<T>::value, Color3<T>>::type fromHsv(typename Color3<T>::Hsv hsv) {
Deg<T> hue; Deg<T> hue;
T saturation, value; T saturation, value;
std::tie(hue, saturation, value) = hsv; std::tie(hue, saturation, value) = hsv;
@ -65,8 +65,8 @@ template<class T> typename std::enable_if<std::is_floating_point<T>::value, Colo
default: CORRADE_ASSERT_UNREACHABLE(); /* LCOV_EXCL_LINE */ default: CORRADE_ASSERT_UNREACHABLE(); /* LCOV_EXCL_LINE */
} }
} }
template<class T> inline typename std::enable_if<std::is_integral<T>::value, Color3<T>>::type fromHSV(typename Color3<T>::HSV hsv) { template<class T> inline typename std::enable_if<std::is_integral<T>::value, Color3<T>>::type fromHsv(typename Color3<T>::Hsv hsv) {
return denormalize<Color3<T>>(fromHSV<typename Color3<T>::FloatingPointType>(hsv)); return denormalize<Color3<T>>(fromHsv<typename Color3<T>::FloatingPointType>(hsv));
} }
/* Internal hue computing function */ /* Internal hue computing function */
@ -113,14 +113,14 @@ template<class T> inline typename Color3<T>::FloatingPointType value(typename st
} }
/* Convert color to HSV */ /* Convert color to HSV */
template<class T> inline typename Color3<T>::HSV toHSV(typename std::enable_if<std::is_floating_point<T>::value, const Color3<T>&>::type color) { template<class T> inline typename Color3<T>::Hsv toHsv(typename std::enable_if<std::is_floating_point<T>::value, const Color3<T>&>::type color) {
T max = color.max(); T max = color.max();
T delta = max - color.min(); T delta = max - color.min();
return typename Color3<T>::HSV(hue<typename Color3<T>::FloatingPointType>(color, max, delta), max != T(0) ? delta/max : T(0), max); return typename Color3<T>::Hsv(hue<typename Color3<T>::FloatingPointType>(color, max, delta), max != T(0) ? delta/max : T(0), max);
} }
template<class T> inline typename Color3<T>::HSV toHSV(typename std::enable_if<std::is_integral<T>::value, const Color3<T>&>::type color) { template<class T> inline typename Color3<T>::Hsv toHsv(typename std::enable_if<std::is_integral<T>::value, const Color3<T>&>::type color) {
return toHSV<typename Color3<T>::FloatingPointType>(normalize<Color3<typename Color3<T>::FloatingPointType>>(color)); return toHsv<typename Color3<T>::FloatingPointType>(normalize<Color3<typename Color3<T>::FloatingPointType>>(color));
} }
/* Value for full channel (1.0f for floats, 255 for unsigned byte) */ /* Value for full channel (1.0f for floats, 255 for unsigned byte) */
@ -233,23 +233,46 @@ template<class T> class Color3: public Vector3<T> {
* Hue in range @f$ [0.0, 360.0] @f$, saturation and value in * Hue in range @f$ [0.0, 360.0] @f$, saturation and value in
* range @f$ [0.0, 1.0] @f$. * range @f$ [0.0, 1.0] @f$.
*/ */
typedef std::tuple<Deg<FloatingPointType>, FloatingPointType, FloatingPointType> HSV; typedef std::tuple<Deg<FloatingPointType>, FloatingPointType, FloatingPointType> Hsv;
#ifdef MAGNUM_BUILD_DEPRECATED
/** @copybrief Hsv
* @deprecated Use @ref Hsv instead.
*/
typedef CORRADE_DEPRECATED("use Hsv instead") Hsv HSV;
#endif
/** /**
* @brief Create RGB color from HSV representation * @brief Create RGB color from HSV representation
* @param hsv Hue, saturation and value * @param hsv Hue, saturation and value
* *
* Hue can overflow the range @f$ [0.0, 360.0] @f$. * Hue can overflow the range @f$ [0.0, 360.0] @f$.
* @see @ref toHSV() * @see @ref toHsv()
*/ */
static Color3<T> fromHSV(HSV hsv) { static Color3<T> fromHsv(Hsv hsv) {
return Implementation::fromHSV<T>(hsv); return Implementation::fromHsv<T>(hsv);
} }
/** @overload */ /** @overload */
static Color3<T> fromHSV(Deg<FloatingPointType> hue, FloatingPointType saturation, FloatingPointType value) { static Color3<T> fromHsv(Deg<FloatingPointType> hue, FloatingPointType saturation, FloatingPointType value) {
return fromHSV(std::make_tuple(hue, saturation, value)); return fromHsv(std::make_tuple(hue, saturation, value));
} }
#ifdef MAGNUM_BUILD_DEPRECATED
/** @copybrief fromHsv(Hsv)
* @deprecated Use @ref fromHsv(Hsv) instead.
*/
CORRADE_DEPRECATED("use fromHsv() instead") static Color3<T> fromHSV(Hsv hsv) {
return fromHsv(hsv);
}
/** @copybrief fromHsv(Deg<FloatingPointType>, FloatingPointType, FloatingPointType)
* @deprecated Use @ref fromHsv(Deg<FloatingPointType>, FloatingPointType, FloatingPointType)
* instead.
*/
CORRADE_DEPRECATED("use fromHsv() instead") static Color3<T> fromHSV(Deg<FloatingPointType> hue, FloatingPointType saturation, FloatingPointType value) {
return fromHsv(hue, saturation, value);
}
#endif
/** /**
* @brief Default constructor * @brief Default constructor
* *
@ -315,20 +338,27 @@ template<class T> class Color3: public Vector3<T> {
* @code * @code
* Deg hue; * Deg hue;
* Float saturation, value; * Float saturation, value;
* std::tie(hue, saturation, value) = color.toHSV(); * std::tie(hue, saturation, value) = color.toHsv();
* @endcode * @endcode
* *
* @see @ref hue(), @ref saturation(), @ref value(), @ref fromHSV() * @see @ref hue(), @ref saturation(), @ref value(), @ref fromHSv()
*/ */
HSV toHSV() const { Hsv toHsv() const {
return Implementation::toHSV<T>(*this); return Implementation::toHsv<T>(*this);
} }
#ifdef MAGNUM_BUILD_DEPRECATED
/** @copybrief toHsv()
* @deprecated Use @ref toHsv() instead.
*/
CORRADE_DEPRECATED("use toHsv() instead") Hsv toHSV() const { return toHsv(); }
#endif
/** /**
* @brief Hue * @brief Hue
* @return Hue in range @f$ [0.0, 360.0] @f$. * @return Hue in range @f$ [0.0, 360.0] @f$.
* *
* @see @ref saturation(), @ref value(), @ref toHSV(), @ref fromHSV() * @see @ref saturation(), @ref value(), @ref toHsv(), @ref fromHsv()
*/ */
Deg<FloatingPointType> hue() const { Deg<FloatingPointType> hue() const {
return Deg<FloatingPointType>(Implementation::hue<T>(*this)); return Deg<FloatingPointType>(Implementation::hue<T>(*this));
@ -338,7 +368,7 @@ template<class T> class Color3: public Vector3<T> {
* @brief Saturation * @brief Saturation
* @return Saturation in range @f$ [0.0, 1.0] @f$. * @return Saturation in range @f$ [0.0, 1.0] @f$.
* *
* @see @ref hue(), @ref value(), @ref toHSV(), @ref fromHSV() * @see @ref hue(), @ref value(), @ref toHsv(), @ref fromHsv()
*/ */
FloatingPointType saturation() const { FloatingPointType saturation() const {
return Implementation::saturation<T>(*this); return Implementation::saturation<T>(*this);
@ -348,7 +378,7 @@ template<class T> class Color3: public Vector3<T> {
* @brief Value * @brief Value
* @return Value in range @f$ [0.0, 1.0] @f$. * @return Value in range @f$ [0.0, 1.0] @f$.
* *
* @see @ref hue(), @ref saturation(), @ref toHSV(), @ref fromHSV() * @see @ref hue(), @ref saturation(), @ref toHsv(), @ref fromHsv()
*/ */
FloatingPointType value() const { FloatingPointType value() const {
return Implementation::value<T>(*this); return Implementation::value<T>(*this);
@ -380,8 +410,15 @@ class Color4: public Vector4<T> {
/** @copydoc Color3::FloatingPointType */ /** @copydoc Color3::FloatingPointType */
typedef typename Color3<T>::FloatingPointType FloatingPointType; typedef typename Color3<T>::FloatingPointType FloatingPointType;
/** @copydoc Color3::HSV */ /** @copydoc Color3::Hsv */
typedef typename Color3<T>::HSV HSV; typedef typename Color3<T>::Hsv Hsv;
#ifdef MAGNUM_BUILD_DEPRECATED
/** @copybrief Hsv
* @deprecated Use @ref Hsv instead.
*/
typedef CORRADE_DEPRECATED("use Hsv instead") Hsv HSV;
#endif
/** /**
* @brief Red color * @brief Red color
@ -450,16 +487,32 @@ class Color4: public Vector4<T> {
* and maximum positive value for integral types. * and maximum positive value for integral types.
* *
* Hue can overflow the range @f$ [0.0, 360.0] @f$. * Hue can overflow the range @f$ [0.0, 360.0] @f$.
* @see @ref toHSV() * @see @ref toHsv()
*/ */
static Color4<T> fromHSV(HSV hsv, T a = Implementation::fullChannel<T>()) { static Color4<T> fromHsv(Hsv hsv, T a = Implementation::fullChannel<T>()) {
return Color4<T>(Implementation::fromHSV<T>(hsv), a); return Color4<T>(Implementation::fromHsv<T>(hsv), a);
} }
/** @overload */ /** @overload */
static Color4<T> fromHSV(Deg<FloatingPointType> hue, FloatingPointType saturation, FloatingPointType value, T alpha = Implementation::fullChannel<T>()) { static Color4<T> fromHsv(Deg<FloatingPointType> hue, FloatingPointType saturation, FloatingPointType value, T alpha = Implementation::fullChannel<T>()) {
return fromHSV(std::make_tuple(hue, saturation, value), alpha); return fromHsv(std::make_tuple(hue, saturation, value), alpha);
} }
#ifdef MAGNUM_BUILD_DEPRECATED
/** @copybrief fromHsv(Hsv, T)
* @deprecated Use @ref fromHsv(Hsv, T) instead.
*/
CORRADE_DEPRECATED("use fromHsv() instead") static Color4<T> fromHSV(Hsv hsv, T a = Implementation::fullChannel<T>()) {
return fromHsv(hsv, a);
}
/** @copybrief fromHsv(Deg<FloatingPointType>, FloatingPointType, FloatingPointType, T)
* @deprecated Use @ref fromHsv(Deg<FloatingPointType>, FloatingPointType, FloatingPointType, T)
* instead.
*/
CORRADE_DEPRECATED("use fromHsv() instead") static Color4<T> fromHSV(Deg<FloatingPointType> hue, FloatingPointType saturation, FloatingPointType value, T a = Implementation::fullChannel<T>()) {
return fromHsv(hue, saturation, value, a);
}
#endif
/** /**
* @brief Default constructor * @brief Default constructor
* *
@ -533,11 +586,18 @@ class Color4: public Vector4<T> {
/** @brief Copy constructor */ /** @brief Copy constructor */
constexpr /*implicit*/ Color4(const Vector<4, T>& other) noexcept: Vector4<T>(other) {} constexpr /*implicit*/ Color4(const Vector<4, T>& other) noexcept: Vector4<T>(other) {}
/** @copydoc Color3::toHSV() */ /** @copydoc Color3::toHsv() */
HSV toHSV() const { Hsv toHsv() const {
return Implementation::toHSV<T>(Vector4<T>::rgb()); return Implementation::toHsv<T>(Vector4<T>::rgb());
} }
#ifdef MAGNUM_BUILD_DEPRECATED
/** @copybrief toHsv()
* @deprecated Use @ref toHsv() instead.
*/
CORRADE_DEPRECATED("use toHsv() instead") Hsv toHSV() const { return toHsv(); }
#endif
/** @copydoc Color3::hue() */ /** @copydoc Color3::hue() */
Deg<FloatingPointType> hue() const { Deg<FloatingPointType> hue() const {
return Implementation::hue<T>(Vector4<T>::rgb()); return Implementation::hue<T>(Vector4<T>::rgb());

54
src/Magnum/Math/Test/ColorTest.cpp

@ -371,12 +371,12 @@ void ColorTest::colors() {
} }
void ColorTest::fromHue() { void ColorTest::fromHue() {
CORRADE_COMPARE(Color3ub::fromHSV(27.0_degf, 1.0f, 1.0f), Color3ub(255, 114, 0)); 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(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(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(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(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)); CORRADE_COMPARE(Color3ub::fromHsv(317.0_degf, 1.0f, 1.0f), Color3ub(255, 0, 182));
} }
void ColorTest::hue() { void ColorTest::hue() {
@ -389,7 +389,7 @@ void ColorTest::hue() {
} }
void ColorTest::fromSaturation() { void ColorTest::fromSaturation() {
CORRADE_COMPARE(Color3ub::fromHSV(0.0_degf, 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() { void ColorTest::saturation() {
@ -398,7 +398,7 @@ void ColorTest::saturation() {
} }
void ColorTest::fromValue() { void ColorTest::fromValue() {
CORRADE_COMPARE(Color3ub::fromHSV(0.0_degf, 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() { void ColorTest::value() {
@ -406,39 +406,39 @@ void ColorTest::value() {
} }
void ColorTest::hsv() { void ColorTest::hsv() {
CORRADE_COMPARE(Color3ub::fromHSV(230.0_degf, 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; Deg hue;
Float saturation, value; Float saturation, value;
std::tie(hue, saturation, value) = Color3ub(27, 41, 109).toHSV(); std::tie(hue, saturation, value) = Color3ub(27, 41, 109).toHsv();
CORRADE_COMPARE(hue, 229.756106_degf); CORRADE_COMPARE(hue, 229.756106_degf);
CORRADE_COMPARE(saturation, 0.752294f); CORRADE_COMPARE(saturation, 0.752294f);
CORRADE_COMPARE(value, 0.427451f); CORRADE_COMPARE(value, 0.427451f);
} }
void ColorTest::hsvOverflow() { void ColorTest::hsvOverflow() {
CORRADE_COMPARE(Color3ub::fromHSV(27.0_degf - 360.0_degf, 1.0f, 1.0f), Color3ub(255, 114, 0)); 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(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(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(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(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(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 + 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 + 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 + 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 + 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 + 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)); CORRADE_COMPARE(Color3ub::fromHsv(360.0_degf + 317.0_degf, 1.0f, 1.0f), Color3ub(255, 0, 182));
} }
void ColorTest::hsvAlpha() { void ColorTest::hsvAlpha() {
CORRADE_COMPARE(Color4ub::fromHSV(std::make_tuple(230.0_degf, 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)); CORRADE_COMPARE(Color4ub::fromHsv(230.0_degf, 0.749f, 0.427f, 23), Color4ub(27, 40, 108, 23));
/* Default alpha */ /* Default alpha */
CORRADE_COMPARE(Color4ub::fromHSV(std::make_tuple(230.0_degf, 0.749f, 0.427f)), Color4ub(27, 40, 108, 255)); CORRADE_COMPARE(Color4ub::fromHsv(std::make_tuple(230.0_degf, 0.749f, 0.427f)), Color4ub(27, 40, 108, 255));
CORRADE_COMPARE(Color4ub::fromHSV(230.0_degf, 0.749f, 0.427f), Color4ub(27, 40, 108, 255)); CORRADE_COMPARE(Color4ub::fromHsv(230.0_degf, 0.749f, 0.427f), Color4ub(27, 40, 108, 255));
} }
void ColorTest::swizzleType() { void ColorTest::swizzleType() {

Loading…
Cancel
Save