Browse Source

Using Deg for Color hue.

pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
a220289067
  1. 33
      src/Color.h
  2. 65
      src/Test/ColorTest.cpp

33
src/Color.h

@ -32,15 +32,16 @@ namespace Implementation {
/* Convert color from HSV */
template<class T> inline typename std::enable_if<std::is_floating_point<T>::value, Color3<T>>::type fromHSV(typename Color3<T>::HSV hsv) {
T hue, saturation, value;
Math::Deg<T> hue;
T saturation, value;
std::tie(hue, saturation, value) = hsv;
/* Remove repeats */
hue -= int(hue/T(360))*T(360);
if(hue < T(0)) hue += T(360);
hue -= int(T(hue)/T(360))*Math::Deg<T>(360);
if(hue < Math::Deg<T>(0)) hue += Math::Deg<T>(360);
int h = int(hue/T(60)) % 6;
T f = hue/T(60) - h;
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);
@ -61,7 +62,7 @@ template<class T> inline typename std::enable_if<std::is_integral<T>::value, Col
}
/* Internal hue computing function */
template<class T> T hue(const Color3<T>& color, T max, T delta) {
template<class T> Math::Deg<T> hue(const Color3<T>& color, T max, T delta) {
T deltaInv60 = T(60)/delta;
T hue(0);
@ -74,11 +75,11 @@ template<class T> T hue(const Color3<T>& color, T max, T delta) {
hue = (color.r()-color.g())*deltaInv60 + T(240);
}
return hue;
return Math::Deg<T>(hue);
}
/* Hue, saturation, value for floating-point types */
template<class T> inline T hue(typename std::enable_if<std::is_floating_point<T>::value, const Color3<T>&>::type color) {
template<class T> inline Math::Deg<T> hue(typename std::enable_if<std::is_floating_point<T>::value, const Color3<T>&>::type color) {
T max = color.max();
T delta = max - color.min();
return hue(color, max, delta);
@ -93,7 +94,7 @@ template<class T> inline T value(typename std::enable_if<std::is_floating_point<
}
/* Hue, saturation, value for integral types */
template<class T> inline typename Color3<T>::FloatingPointType hue(typename std::enable_if<std::is_integral<T>::value, const Color3<T>&>::type color) {
template<class T> inline Math::Deg<typename Color3<T>::FloatingPointType> hue(typename std::enable_if<std::is_integral<T>::value, const Color3<T>&>::type color) {
return hue<typename Color3<T>::FloatingPointType>(Math::normalize<Color3<typename Color3<T>::FloatingPointType>>(color));
}
template<class T> inline typename Color3<T>::FloatingPointType saturation(typename std::enable_if<std::is_integral<T>::value, const Color3<T>&>::type& color) {
@ -137,8 +138,6 @@ is always in range in range @f$ [0.0, 360.0] @f$, saturation and value in
range @f$ [0.0, 1.0] @f$.
@see Color4
@todo Hue in degrees so users can use deg()
*/
/* Not using template specialization because some internal functions are
impossible to explicitly instantiate */
@ -158,7 +157,7 @@ class Color3: public Math::Vector3<T> {
* 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, FloatingPointType> HSV;
typedef std::tuple<Math::Deg<FloatingPointType>, FloatingPointType, FloatingPointType> HSV;
/**
* @brief Create RGB color from HSV representation
@ -170,7 +169,7 @@ class Color3: public Math::Vector3<T> {
return Implementation::fromHSV<T>(hsv);
}
/** @overload */
inline constexpr static Color3<T> fromHSV(FloatingPointType hue, FloatingPointType saturation, FloatingPointType value) {
inline constexpr static Color3<T> fromHSV(Math::Deg<FloatingPointType> hue, FloatingPointType saturation, FloatingPointType value) {
return fromHSV(std::make_tuple(hue, saturation, value));
}
@ -229,8 +228,8 @@ class Color3: public Math::Vector3<T> {
*
* @see saturation(), value(), toHSV(), fromHSV()
*/
inline constexpr FloatingPointType hue() const {
return Implementation::hue<T>(*this);
inline constexpr Math::Deg<FloatingPointType> hue() const {
return Math::Deg<FloatingPointType>(Implementation::hue<T>(*this));
}
/**
@ -287,7 +286,7 @@ class Color4: public Math::Vector4<T> {
return Color4<T>(Implementation::fromHSV<T>(hsv), a);
}
/** @overload */
inline constexpr static Color4<T> fromHSV(FloatingPointType hue, FloatingPointType saturation, FloatingPointType value, T alpha) {
inline constexpr static Color4<T> fromHSV(Math::Deg<FloatingPointType> hue, FloatingPointType saturation, FloatingPointType value, T alpha) {
return fromHSV(std::make_tuple(hue, saturation, value), alpha);
}
@ -355,7 +354,7 @@ class Color4: public Math::Vector4<T> {
}
/** @copydoc Color3::hue() */
inline constexpr FloatingPointType hue() const {
inline constexpr Math::Deg<FloatingPointType> hue() const {
return Implementation::hue<T>(rgb());
}

65
src/Test/ColorTest.cpp

@ -92,25 +92,25 @@ void ColorTest::access() {
}
void ColorTest::fromHue() {
CORRADE_COMPARE(Color3::fromHSV(27.0f, 1.0f, 1.0f), Color3(255, 114, 0));
CORRADE_COMPARE(Color3::fromHSV(86.0f, 1.0f, 1.0f), Color3(144, 255, 0));
CORRADE_COMPARE(Color3::fromHSV(134.0f, 1.0f, 1.0f), Color3(0, 255, 59));
CORRADE_COMPARE(Color3::fromHSV(191.0f, 1.0f, 1.0f), Color3(0, 208, 255));
CORRADE_COMPARE(Color3::fromHSV(269.0f, 1.0f, 1.0f), Color3(123, 0, 255));
CORRADE_COMPARE(Color3::fromHSV(317.0f, 1.0f, 1.0f), Color3(255, 0, 182));
CORRADE_COMPARE(Color3::fromHSV(Deg(27.0f), 1.0f, 1.0f), Color3(255, 114, 0));
CORRADE_COMPARE(Color3::fromHSV(Deg(86.0f), 1.0f, 1.0f), Color3(144, 255, 0));
CORRADE_COMPARE(Color3::fromHSV(Deg(134.0f), 1.0f, 1.0f), Color3(0, 255, 59));
CORRADE_COMPARE(Color3::fromHSV(Deg(191.0f), 1.0f, 1.0f), Color3(0, 208, 255));
CORRADE_COMPARE(Color3::fromHSV(Deg(269.0f), 1.0f, 1.0f), Color3(123, 0, 255));
CORRADE_COMPARE(Color3::fromHSV(Deg(317.0f), 1.0f, 1.0f), Color3(255, 0, 182));
}
void ColorTest::hue() {
CORRADE_COMPARE(Color3(255, 115, 0).hue(), 27.058824f);
CORRADE_COMPARE(Color3(145, 255, 0).hue(), 85.882353f);
CORRADE_COMPARE(Color3(0, 255, 60).hue(), 134.11765f);
CORRADE_COMPARE(Color3(0, 208, 255).hue(), 191.05882f);
CORRADE_COMPARE(Color3(123, 0, 255).hue(), 268.94117f);
CORRADE_COMPARE(Color3(255, 0, 183).hue(), 316.94117f);
CORRADE_COMPARE(Color3(255, 115, 0).hue(), Deg(27.058824f));
CORRADE_COMPARE(Color3(145, 255, 0).hue(), Deg(85.882353f));
CORRADE_COMPARE(Color3(0, 255, 60).hue(), Deg(134.11765f));
CORRADE_COMPARE(Color3(0, 208, 255).hue(), Deg(191.05882f));
CORRADE_COMPARE(Color3(123, 0, 255).hue(), Deg(268.94117f));
CORRADE_COMPARE(Color3(255, 0, 183).hue(), Deg(316.94117f));
}
void ColorTest::fromSaturation() {
CORRADE_COMPARE(Color3::fromHSV(0.0f, 0.702f, 1.0f), Color3(255, 75, 75));
CORRADE_COMPARE(Color3::fromHSV(Deg(0.0f), 0.702f, 1.0f), Color3(255, 75, 75));
}
void ColorTest::saturation() {
@ -119,7 +119,7 @@ void ColorTest::saturation() {
}
void ColorTest::fromValue() {
CORRADE_COMPARE(Color3::fromHSV(0.0f, 1.0f, 0.522f), Color3(133, 0, 0));
CORRADE_COMPARE(Color3::fromHSV(Deg(0.0f), 1.0f, 0.522f), Color3(133, 0, 0));
}
void ColorTest::value() {
@ -127,34 +127,35 @@ void ColorTest::value() {
}
void ColorTest::hsv() {
CORRADE_COMPARE(Color3::fromHSV(230.0f, 0.749f, 0.427f), Color3(27, 40, 108));
CORRADE_COMPARE(Color3::fromHSV(Deg(230.0f), 0.749f, 0.427f), Color3(27, 40, 108));
float hue, saturation, value;
Deg hue;
float saturation, value;
std::tie(hue, saturation, value) = Color3(27, 41, 109).toHSV();
CORRADE_COMPARE(hue, 229.756106f);
CORRADE_COMPARE(hue, Deg(229.756106f));
CORRADE_COMPARE(saturation, 0.752294f);
CORRADE_COMPARE(value, 0.427451f);
}
void ColorTest::hsvOverflow() {
CORRADE_COMPARE(Color3::fromHSV(27.0f-360.0f, 1.0f, 1.0f), Color3(255, 114, 0));
CORRADE_COMPARE(Color3::fromHSV(86.0f-360.0f, 1.0f, 1.0f), Color3(144, 255, 0));
CORRADE_COMPARE(Color3::fromHSV(134.0f-360.0f, 1.0f, 1.0f), Color3(0, 255, 59));
CORRADE_COMPARE(Color3::fromHSV(191.0f-360.0f, 1.0f, 1.0f), Color3(0, 208, 255));
CORRADE_COMPARE(Color3::fromHSV(269.0f-360.0f, 1.0f, 1.0f), Color3(123, 0, 255));
CORRADE_COMPARE(Color3::fromHSV(317.0f-360.0f, 1.0f, 1.0f), Color3(255, 0, 182));
CORRADE_COMPARE(Color3::fromHSV(360.0f+27.0f, 1.0f, 1.0f), Color3(255, 114, 0));
CORRADE_COMPARE(Color3::fromHSV(360.0f+86.0f, 1.0f, 1.0f), Color3(144, 255, 0));
CORRADE_COMPARE(Color3::fromHSV(360.0f+134.0f, 1.0f, 1.0f), Color3(0, 255, 59));
CORRADE_COMPARE(Color3::fromHSV(360.0f+191.0f, 1.0f, 1.0f), Color3(0, 208, 255));
CORRADE_COMPARE(Color3::fromHSV(360.0f+269.0f, 1.0f, 1.0f), Color3(123, 0, 255));
CORRADE_COMPARE(Color3::fromHSV(360.0f+317.0f, 1.0f, 1.0f), Color3(255, 0, 182));
CORRADE_COMPARE(Color3::fromHSV(Deg(27.0f-360.0f), 1.0f, 1.0f), Color3(255, 114, 0));
CORRADE_COMPARE(Color3::fromHSV(Deg(86.0f-360.0f), 1.0f, 1.0f), Color3(144, 255, 0));
CORRADE_COMPARE(Color3::fromHSV(Deg(134.0f-360.0f), 1.0f, 1.0f), Color3(0, 255, 59));
CORRADE_COMPARE(Color3::fromHSV(Deg(191.0f-360.0f), 1.0f, 1.0f), Color3(0, 208, 255));
CORRADE_COMPARE(Color3::fromHSV(Deg(269.0f-360.0f), 1.0f, 1.0f), Color3(123, 0, 255));
CORRADE_COMPARE(Color3::fromHSV(Deg(317.0f-360.0f), 1.0f, 1.0f), Color3(255, 0, 182));
CORRADE_COMPARE(Color3::fromHSV(Deg(360.0f+27.0f), 1.0f, 1.0f), Color3(255, 114, 0));
CORRADE_COMPARE(Color3::fromHSV(Deg(360.0f+86.0f), 1.0f, 1.0f), Color3(144, 255, 0));
CORRADE_COMPARE(Color3::fromHSV(Deg(360.0f+134.0f), 1.0f, 1.0f), Color3(0, 255, 59));
CORRADE_COMPARE(Color3::fromHSV(Deg(360.0f+191.0f), 1.0f, 1.0f), Color3(0, 208, 255));
CORRADE_COMPARE(Color3::fromHSV(Deg(360.0f+269.0f), 1.0f, 1.0f), Color3(123, 0, 255));
CORRADE_COMPARE(Color3::fromHSV(Deg(360.0f+317.0f), 1.0f, 1.0f), Color3(255, 0, 182));
}
void ColorTest::hsvAlpha() {
CORRADE_COMPARE(Color4::fromHSV(std::make_tuple(230.0f, 0.749f, 0.427f), 23), Color4(27, 40, 108, 23));
CORRADE_COMPARE(Color4::fromHSV(230.0f, 0.749f, 0.427f, 23), Color4(27, 40, 108, 23));
CORRADE_COMPARE(Color4::fromHSV(std::make_tuple(Deg(230.0f), 0.749f, 0.427f), 23), Color4(27, 40, 108, 23));
CORRADE_COMPARE(Color4::fromHSV(Deg(230.0f), 0.749f, 0.427f, 23), Color4(27, 40, 108, 23));
}
void ColorTest::debug() {

Loading…
Cancel
Save