Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support for hsl colors #924

Merged
merged 7 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Jaskowicz1 marked this conversation as resolved.
Show resolved Hide resolved
* @param l lightness 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
Jaskowicz1 marked this conversation as resolved.
Show resolved Hide resolved
* @param s saturation value in procents, between 0 and 100 inclusive
* @param l lightness 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>
Jaskowicz1 marked this conversation as resolved.
Show resolved Hide resolved
#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);
Jaskowicz1 marked this conversation as resolved.
Show resolved Hide resolved
}

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;
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 = 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);
Jaskowicz1 marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading