From 1362b696262b13ed2c97a37f6c52605ab33c9ea0 Mon Sep 17 00:00:00 2001 From: ruslan-ilesik Date: Thu, 5 Oct 2023 11:09:54 -0400 Subject: [PATCH] feat: added support for hsl colors --- include/dpp/utility.h | 20 ++++++++++++++++++++ src/dpp/utility.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/include/dpp/utility.h b/include/dpp/utility.h index 28e2810b63..964adfbe22 100644 --- a/include/dpp/utility.h +++ b/include/dpp/utility.h @@ -420,6 +420,26 @@ namespace dpp { */ uint32_t DPP_EXPORT cmyk(int c, int m, int y, int k); + /** + * @brief Convert doubles to HSL for sending in embeds + * + * @param h hue value, between 0 and 1 inclusive + * @param s saturation value in procents, between 0 and 1 inclusive + * @param l ligthness value in procents, between 0 and 1 inclusive + * @return uint32_t returned integer colour value + */ + uint32_t DPP_EXPORT hsl(double h, double s, double l); + + /** + * @brief Convert ints to HSL for sending in embeds + * + * @param h hue value, between 0 and 356 inclusive + * @param s saturation value in procents, between 0 and 100 inclusive + * @param l ligthness value in procents, between 0 and 100 inclusive + * @return uint32_t returned integer colour value + */ + uint32_t DPP_EXPORT hsl(int h, int s, int l); + /** * @brief Output hex values of a section of memory for debugging * diff --git a/src/dpp/utility.cpp b/src/dpp/utility.cpp index 1ca4751ca3..3334874b94 100644 --- a/src/dpp/utility.cpp +++ b/src/dpp/utility.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -299,7 +300,38 @@ namespace dpp { uint32_t cmyk(int c, int m, int y, int k) { return cmyk(c / 255.0, m / 255.0, y / 255.0, k / 255.0); } + + uint32_t hsl(int h, int s, int l) { + double hue = static_cast(h) / 360.0; + double saturation = static_cast(s) / 100.0; + double lightness = static_cast(l) / 100.0; + return hsl(hue,saturation,lightness); + } + uint32_t hsl(double h, double s, double l) { + const auto hueToRgb = [](double p, double q, double t){ + if (t < 0) t += 1; + if (t > 1) t -= 1; + if (t < 1.0 / 6.0) return p + (q - p) * 6.0 * t; + if (t < 0.5) return q; + if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6.0; + return p; + }; + + double r, g, b; + + if (s == 0) { + r = g = b = l; // Gray scale + } else { + double q = l < 0.5 ? l * (1 + s) : l + s - l * s; + double p = 2 * l - q; + r = hueToRgb(p, q, h + 1.0 / 3.0); + g = hueToRgb(p, q, h); + b = hueToRgb(p, q, h - 1.0 / 3.0); + } + return rgb(r,g,b); + } + void exec(const std::string& cmd, std::vector parameters, cmd_result_t callback) { auto t = std::thread([cmd, parameters, callback]() { utility::set_thread_name("async_exec");