Skip to content

Commit

Permalink
feat: added support for hsl colors
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslan-ilesik committed Oct 5, 2023
1 parent 7bc1619 commit 1362b69
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions include/dpp/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
32 changes: 32 additions & 0 deletions src/dpp/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <fstream>
#include <streambuf>
#include <array>
#include <cmath>
#include <dpp/cluster.h>
#include <dpp/dispatcher.h>
#include <dpp/message.h>
Expand Down Expand Up @@ -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<double>(h) / 360.0;
double saturation = static_cast<double>(s) / 100.0;
double lightness = static_cast<double>(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<std::string> parameters, cmd_result_t callback) {
auto t = std::thread([cmd, parameters, callback]() {
utility::set_thread_name("async_exec");
Expand Down

0 comments on commit 1362b69

Please sign in to comment.