From 01cc65097f27ad657130fb58a10813e8d5c09fe7 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 18 Dec 2017 10:43:23 +0100 Subject: [PATCH] color conversion tests --- src/creature/creature.cpp | 4 ++ src/graphics/color.hpp | 37 +++++++++++ src/math/glm.hpp | 25 -------- tst/graphics/ColorTest.cpp | 124 +++++++++++++++++++++++++++++++++++++ tst/graphics/ColorTest.hpp | 33 ++++++++++ 5 files changed, 198 insertions(+), 25 deletions(-) create mode 100644 src/graphics/color.hpp create mode 100644 tst/graphics/ColorTest.cpp create mode 100644 tst/graphics/ColorTest.hpp diff --git a/src/creature/creature.cpp b/src/creature/creature.cpp index cb0f501..e118451 100644 --- a/src/creature/creature.cpp +++ b/src/creature/creature.cpp @@ -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 #include +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 index 0000000..6f6083e --- /dev/null +++ b/src/graphics/color.hpp @@ -0,0 +1,37 @@ +#ifndef BLOBS_GRAPHICS_COLOR_HPP_ +#define BLOBS_GRAPHICS_COLOR_HPP_ + +#include "../math/glm.hpp" + +namespace blobs { +namespace graphics { + +template +inline T rgb2hsl(const T &rgb) { + using Vec4 = glm::tvec4; + 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 +inline T hsl2rgb(const T &hsl) { + using Vec3 = glm::tvec3; + using Vec4 = glm::tvec4; + 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/src/math/glm.hpp b/src/math/glm.hpp index 0d344e6..76472e8 100644 --- a/src/math/glm.hpp +++ b/src/math/glm.hpp @@ -51,29 +51,4 @@ inline Vec limit(const Vec &v, typename Vec::value_type max) noexcept { } } -template -inline T rgb2hsl(const T &rgb) { - using Vec4 = glm::tvec4; - 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 -inline T hsl2rgb(const T &hsl) { - using Vec3 = glm::tvec3; - using Vec4 = glm::tvec4; - 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 index 0000000..250b902 --- /dev/null +++ b/tst/graphics/ColorTest.cpp @@ -0,0 +1,124 @@ +#include "ColorTest.hpp" + +#include "../assert.hpp" + +#include "graphics/color.hpp" + +#include + +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 index 0000000..5c4e8bf --- /dev/null +++ b/tst/graphics/ColorTest.hpp @@ -0,0 +1,33 @@ +#ifndef BLOBS_TEST_GRAPHICS_COLORTEST_HPP +#define BLOBS_TEST_GRAPHICS_COLORTEST_HPP + +#include + + + +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 -- 2.39.2