X-Git-Url: http://git.localhorst.tv/?p=blobs.git;a=blobdiff_plain;f=src%2Fgraphics%2Fcolor.hpp;fp=src%2Fgraphics%2Fcolor.hpp;h=6f6083e869b26ee22bc6ea5f888ab8e0b63f925f;hp=0000000000000000000000000000000000000000;hb=01cc65097f27ad657130fb58a10813e8d5c09fe7;hpb=99bac662c454fcf3680fa9c080c90e24319d8d3d 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