Skip to content
Snippets Groups Projects
Commit d978e30a authored by Jens Nolte's avatar Jens Nolte
Browse files

Add RGB type

parent e7393541
No related branches found
No related tags found
No related merge requests found
...@@ -3,11 +3,9 @@ ...@@ -3,11 +3,9 @@
namespace qthing { namespace qthing {
// TODO: should return RGB once that can be casted to RGBW // TODO: should return RGB once that can be casted to RGBW
RGBW hue(const float hue, const float value) { RGB hue(const float hue, const float value) {
RGBW color;
if (value <= 0) { if (value <= 0) {
return color; return RGB();
} }
auto h = fmod(hue, 1); auto h = fmod(hue, 1);
...@@ -18,19 +16,15 @@ namespace qthing { ...@@ -18,19 +16,15 @@ namespace qthing {
h = h * 3; h = h * 3;
auto v = value > 1 ? 1 : value; auto v = value > 1 ? 1 : value;
if (h < 1) { if (h < 1) {
color.r = (1 - h) * v; return RGB((1 - h) * v, h * v, 0);
color.g = h * v;
} }
else if (h < 2) { else if (h < 2) {
h -= 1; h -= 1;
color.g = (1 - h) * v; return RGB(0, (1 - h) * v, h * v);
color.b = h * v;
} }
else { else {
h -= 2; h -= 2;
color.b = (1 - h) * v; return RGB(h * v, 0, (1 - h) * v);
color.r = h * v;
} }
return color;
} }
} }
...@@ -165,6 +165,43 @@ namespace qthing { ...@@ -165,6 +165,43 @@ namespace qthing {
} }
}; };
struct RGB {
float r;
float g;
float b;
RGB() : r(0), g(0), b(0) {
}
RGB(float r, float g, float b) : r(r), g(g), b(b) {
}
RGB operator+(const RGB& other) {
RGB result = *this;
result.r += other.r;
result.g += other.g;
result.b += other.b;
return result;
}
RGB operator*(const float scale) {
RGB result = *this;
result.r *= scale;
result.g *= scale;
result.b *= scale;
return result;
}
void operator*=(const float scale) {
this->r *= scale;
this->g *= scale;
this->b *= scale;
}
operator RGBW() {
return RGBW(this->r, this->g, this->b, 0);
}
operator led_color_t() {
return led_color_t(this->r * 255, this->g * 255, this->b * 255, 0);
}
};
struct GammaCorrection { struct GammaCorrection {
float gamma_r; float gamma_r;
float gamma_g; float gamma_g;
...@@ -190,7 +227,7 @@ namespace qthing { ...@@ -190,7 +227,7 @@ namespace qthing {
} }
}; };
RGBW hue(const float hue, const float value = 1); RGB hue(const float hue, const float value = 1);
typedef std::function<led_color_t(uint8_t, uint16_t, led_color_t)> led_color_handler_t; typedef std::function<led_color_t(uint8_t, uint16_t, led_color_t)> led_color_handler_t;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment