1 #ifndef BLOBS_GRAPHICS_COLOR_HPP_
2 #define BLOBS_GRAPHICS_COLOR_HPP_
4 #include "../math/glm.hpp"
10 inline T rgb2hsl(const T &rgb) {
11 using Vec4 = glm::tvec4<typename T::value_type>;
12 const Vec4 K(0.0, -1.0/3.0, 2.0/3.0, -1.0);
13 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));
14 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));
15 const typename T::value_type d = q.x - std::min(q.w, q.y);
16 const typename T::value_type e = 1.0e-10;
18 hsl.x = std::abs(q.z + (q.w - q.y) / (6.0 * d + e));
19 hsl.y = d / (q.x + e);
25 inline T hsl2rgb(const T &hsl) {
26 using Vec3 = glm::tvec3<typename T::value_type>;
27 using Vec4 = glm::tvec4<typename T::value_type>;
28 const Vec4 K(1.0, 2.0/3.0, 1.0/3.0, 3.0);
29 const Vec3 p(glm::abs(glm::fract(Vec3(hsl.x) + Vec3(K)) * 6.0 - Vec3(K.w)));
30 T rgb = hsl.z * glm::mix(Vec3(K.x), glm::clamp(p - Vec3(K.x), 0.0, 1.0), hsl.y);