Add gfx::hue(colorf)

This commit is contained in:
Nikita Lisitsa 2025-02-23 00:31:13 +03:00
parent 72694664de
commit 85451a1d6f

View file

@ -317,6 +317,39 @@ namespace psemek::gfx
return generic_color{dark(c.c, darkness)};
}
template <typename T, std::size_t N> requires (std::is_floating_point_v<T> && (N == 3 || N == 4))
T hue(math::vector<T, N> 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<T>(60);
if (result < 0)
result += math::rad<T>(360);
return result;
}
std::optional<color_rgba> parse_color(std::string_view const & text);
}