]> git.localhorst.tv Git - blobs.git/commitdiff
color conversion tests
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 18 Dec 2017 09:43:23 +0000 (10:43 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 18 Dec 2017 09:43:23 +0000 (10:43 +0100)
src/creature/creature.cpp
src/graphics/color.hpp [new file with mode: 0644]
src/math/glm.hpp
tst/graphics/ColorTest.cpp [new file with mode: 0644]
tst/graphics/ColorTest.hpp [new file with mode: 0644]

index cb0f501eaece3e45d5500e3961acdc9486478362..e118451147fa0e3d88919ecd2a4b96e96698924d 100644 (file)
@@ -11,6 +11,7 @@
 #include "Goal.hpp"
 #include "IdleGoal.hpp"
 #include "../app/Assets.hpp"
+#include "../graphics/color.hpp"
 #include "../math/const.hpp"
 #include "../ui/string.hpp"
 #include "../world/Body.hpp"
@@ -26,6 +27,9 @@
 #include <iostream>
 #include <glm/gtx/io.hpp>
 
+using blobs::graphics::hsl2rgb;
+using blobs::graphics::rgb2hsl;
+
 
 namespace blobs {
 namespace creature {
diff --git a/src/graphics/color.hpp b/src/graphics/color.hpp
new file mode 100644 (file)
index 0000000..6f6083e
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef BLOBS_GRAPHICS_COLOR_HPP_
+#define BLOBS_GRAPHICS_COLOR_HPP_
+
+#include "../math/glm.hpp"
+
+namespace blobs {
+namespace graphics {
+
+template<class T>
+inline T rgb2hsl(const T &rgb) {
+       using Vec4 = glm::tvec4<typename T::value_type>;
+       const Vec4 K(0.0, -1.0/3.0, 2.0/3.0, -1.0);
+       const Vec4 p(glm::mix(Vec4(rgb.z, rgb.y, K.w, K.z), Vec4(rgb.y, rgb.z, K.x, K.y), rgb.y < rgb.z ? 0.0 : 1.0));
+       const Vec4 q(glm::mix(Vec4(p.x, p.y, p.w, rgb.x), Vec4(rgb.x, p.y, p.z, p.x), rgb.x < p.x ? 0.0 : 1.0));
+       const typename T::value_type d = q.x - std::min(q.w, q.y);
+       const typename T::value_type e = 1.0e-10;
+       T hsl = rgb;
+       hsl.x = std::abs(q.z + (q.w - q.y) / (6.0 * d + e));
+       hsl.y = d / (q.x + e);
+       hsl.z = q.x;
+       return hsl;
+}
+
+template<class T>
+inline T hsl2rgb(const T &hsl) {
+       using Vec3 = glm::tvec3<typename T::value_type>;
+       using Vec4 = glm::tvec4<typename T::value_type>;
+       const Vec4 K(1.0, 2.0/3.0, 1.0/3.0, 3.0);
+       const Vec3 p(glm::abs(glm::fract(Vec3(hsl.x) + Vec3(K)) * 6.0 - Vec3(K.w)));
+       T rgb = hsl.z * glm::mix(Vec3(K.x), glm::clamp(p - Vec3(K.x), 0.0, 1.0), hsl.y);
+       return rgb;
+}
+
+}
+}
+
+#endif
index 0d344e6e7879bbb4786aecb64266060854154f7a..76472e8baa2b166d90243b4e3ce11811ce331222 100644 (file)
@@ -51,29 +51,4 @@ inline Vec limit(const Vec &v, typename Vec::value_type max) noexcept {
        }
 }
 
-template<class T>
-inline T rgb2hsl(const T &rgb) {
-       using Vec4 = glm::tvec4<typename T::value_type>;
-       const Vec4 K(0.0, -1.0/3.0, 2.0/3.0, -1.0);
-       const Vec4 p(glm::mix(Vec4(rgb.z, rgb.y, K.w, K.z), Vec4(rgb.y, rgb.z, K.x, K.y), rgb.y < rgb.z ? 0.0 : 1.0));
-       const Vec4 q(glm::mix(Vec4(p.x, p.y, p.w, rgb.x), Vec4(rgb.x, p.y, p.z, p.x), rgb.x < p.x ? 0.0 : 1.0));
-       const typename T::value_type d = q.x - std::min(q.w, q.y);
-       const typename T::value_type e = 1.0e-10;
-       T hsl = rgb;
-       hsl.x = std::abs(q.z + (q.w - q.y) / (6.0 * d + e));
-       hsl.y = d / (q.x + e);
-       hsl.z = q.x;
-       return hsl;
-}
-
-template<class T>
-inline T hsl2rgb(const T &hsl) {
-       using Vec3 = glm::tvec3<typename T::value_type>;
-       using Vec4 = glm::tvec4<typename T::value_type>;
-       const Vec4 K(1.0, 2.0/3.0, 1.0/3.0, 3.0);
-       const Vec3 p(glm::abs(glm::fract(Vec3(hsl.x) + Vec3(K)) * 6.0 - Vec3(K.w)));
-       T rgb = hsl.z * glm::mix(Vec3(K.x), glm::clamp(p - Vec3(K.x), 0.0, 1.0), hsl.y);
-       return rgb;
-}
-
 #endif
diff --git a/tst/graphics/ColorTest.cpp b/tst/graphics/ColorTest.cpp
new file mode 100644 (file)
index 0000000..250b902
--- /dev/null
@@ -0,0 +1,124 @@
+#include "ColorTest.hpp"
+
+#include "../assert.hpp"
+
+#include "graphics/color.hpp"
+
+#include <limits>
+
+CPPUNIT_TEST_SUITE_REGISTRATION(blobs::graphics::test::ColorTest);
+
+using blobs::test::AssertEqual;
+
+
+namespace blobs {
+namespace graphics {
+namespace test {
+
+void ColorTest::setUp() {
+
+}
+
+void ColorTest::tearDown() {
+
+}
+
+void ColorTest::testConversion() {
+       const double epsilon = 1.0e-9;
+       const glm::dvec3 rgb_black(0.0);
+       const glm::dvec3 rgb_white(1.0);
+       const glm::dvec3 rgb_red(1.0, 0.0, 0.0);
+       const glm::dvec3 rgb_green(0.0, 1.0, 0.0);
+       const glm::dvec3 rgb_blue(0.0, 0.0, 1.0);
+
+       glm::dvec3 hsl_result(rgb2hsl(rgb_black));
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "wrong saturation for black",
+               0.0, hsl_result.y, epsilon
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "wrong lightness for black",
+               0.0, hsl_result.z, epsilon
+       );
+       glm::dvec3 rgb_result(hsl2rgb(hsl_result));
+       AssertEqual(
+               "bad HSL to RGB conversion for black",
+               rgb_black, rgb_result, epsilon
+       );
+
+       hsl_result = rgb2hsl(rgb_white);
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "wrong saturation for white",
+               0.0, hsl_result.y, epsilon
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "wrong lightness for white",
+               1.0, hsl_result.z, epsilon
+       );
+       rgb_result = hsl2rgb(hsl_result);
+       AssertEqual(
+               "bad HSL to RGB conversion for white",
+               rgb_white, rgb_result, epsilon
+       );
+
+       hsl_result = rgb2hsl(rgb_red);
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "wrong hue for red",
+               0.0, hsl_result.x, epsilon
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "wrong saturation for red",
+               1.0, hsl_result.y, epsilon
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "wrong lightness for red",
+               1.0, hsl_result.z, epsilon
+       );
+       rgb_result = hsl2rgb(hsl_result);
+       AssertEqual(
+               "bad HSL to RGB conversion for red",
+               rgb_red, rgb_result, epsilon
+       );
+
+       hsl_result = rgb2hsl(rgb_green);
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "wrong hue for green",
+               (1.0 / 3.0), hsl_result.x, epsilon
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "wrong saturation for green",
+               1.0, hsl_result.y, epsilon
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "wrong lightness for green",
+               1.0, hsl_result.z, epsilon
+       );
+       rgb_result = hsl2rgb(hsl_result);
+       AssertEqual(
+               "bad HSL to RGB conversion for green",
+               rgb_green, rgb_result, epsilon
+       );
+
+       hsl_result = rgb2hsl(rgb_blue);
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "wrong hue for blue",
+               (2.0 / 3.0), hsl_result.x, epsilon
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "wrong saturation for blue",
+               1.0, hsl_result.y, epsilon
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "wrong lightness for blue",
+               1.0, hsl_result.z, epsilon
+       );
+       rgb_result = hsl2rgb(hsl_result);
+       AssertEqual(
+               "bad HSL to RGB conversion for blue",
+               rgb_blue, rgb_result, epsilon
+       );
+}
+
+}
+}
+}
diff --git a/tst/graphics/ColorTest.hpp b/tst/graphics/ColorTest.hpp
new file mode 100644 (file)
index 0000000..5c4e8bf
--- /dev/null
@@ -0,0 +1,33 @@
+#ifndef BLOBS_TEST_GRAPHICS_COLORTEST_HPP
+#define BLOBS_TEST_GRAPHICS_COLORTEST_HPP
+
+#include <cppunit/extensions/HelperMacros.h>
+
+
+
+namespace blobs {
+namespace graphics {
+namespace test {
+
+class ColorTest
+: public CppUnit::TestFixture {
+
+CPPUNIT_TEST_SUITE(ColorTest);
+
+CPPUNIT_TEST(testConversion);
+
+CPPUNIT_TEST_SUITE_END();
+
+public:
+       void setUp();
+       void tearDown();
+
+       void testConversion();
+
+};
+
+}
+}
+}
+
+#endif