Skip to content

Commit

Permalink
feat: added support for hsl colors (#924)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslan-ilesik authored Oct 5, 2023
1 parent 7bc1619 commit 8851eab
Show file tree
Hide file tree
Showing 2 changed files with 58 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 percentage, between 0 and 1 inclusive
* @param l lightness value in percentage, 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 360 inclusive
* @param s saturation value in percentage, between 0 and 100 inclusive
* @param l lightness value in percentage, 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
38 changes: 38 additions & 0 deletions src/dpp/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,45 @@ 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 hue_to_rgb = [](double p, double q, double t){
if (t < 0) {
t += 1;
} else if (t > 1) {
t -= 1;
}
if (t < 1.0 / 6.0) {
return p + (q - p) * 6.0 * t;
} else if (t < 0.5) {
return q;
} else 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 = hue_to_rgb(p, q, h + 1.0 / 3.0);
g = hue_to_rgb(p, q, h);
b = hue_to_rgb(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 8851eab

Please sign in to comment.