From 85451a1d6f85b8923174ee40e0d2b4f5172bb312 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 23 Feb 2025 00:31:13 +0300 Subject: [PATCH] Add gfx::hue(colorf) --- libs/gfx/include/psemek/gfx/color.hpp | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/libs/gfx/include/psemek/gfx/color.hpp b/libs/gfx/include/psemek/gfx/color.hpp index 2f2e78f6..9fbd4c39 100644 --- a/libs/gfx/include/psemek/gfx/color.hpp +++ b/libs/gfx/include/psemek/gfx/color.hpp @@ -317,6 +317,39 @@ namespace psemek::gfx return generic_color{dark(c.c, darkness)}; } + template requires (std::is_floating_point_v && (N == 3 || N == 4)) + T hue(math::vector const & c) + { + T max = std::max({c[0], c[1], c[2]}); + T min = std::min({c[0], c[1], c[2]}); + T delta = max - min; + + if (delta == T{0}) + return T{0}; + + T result; + + if (c[0] > c[1] && c[0] > c[2]) + { + result = (c[1] - c[2]) / delta; + } + else if (c[1] > c[2]) + { + result = 2.f + (c[2] - c[0]) / delta; + } + else + { + result = 4.f + (c[0] - c[1]) / delta; + } + + result *= math::rad(60); + + if (result < 0) + result += math::rad(360); + + return result; + } + std::optional parse_color(std::string_view const & text); }