Files
ESP8266lib/data/Color.h
2018-09-22 15:44:26 +02:00

100 lines
1.9 KiB
C++

struct Color {
uint8_t r;
uint8_t g;
uint8_t b;
/** no-init */
Color() {
;
}
private:
/** Hidden ctor. RGB */
Color(const uint8_t r, const uint8_t g, const uint8_t b) : r(r), g(g), b(b) {
;
}
public:
/** get X-percent [0:100] of this color = darker/brighter */
Color inline getPercent(const int percent) const {
return Color(r*percent/100, g*percent/100, b*percent/100);
}
/** mix the two given colors */
static Color mix(const Color c1, const Color c2, int percentC1) {
return Color(
((c1.r * percentC1) + (c2.r * (100-percentC1))) / 100,
((c1.g * percentC1) + (c2.g * (100-percentC1))) / 100,
((c1.b * percentC1) + (c2.b * (100-percentC1))) / 100
);
}
static inline Color fromRGB(const uint8_t r, const uint8_t g, const uint8_t b) {
return Color(r,g,b);
}
static inline Color fromHSV(const uint8_t h, const uint8_t s, const uint8_t v) {
Color c; c.setHSV(h,s,v); return c;
}
void setHSV(const uint8_t h, const uint8_t s, const uint8_t v) {
uint8_t region, remainder, p, q, t;
region = h / 43;
remainder = (h - (region * 43)) * 6;
p = (v * (255 - s)) >> 8;
q = (v * (255 - ((s * remainder) >> 8))) >> 8;
t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
switch (region) {
case 0:
r = v; g = t; b = p;
break;
case 1:
r = q; g = v; b = p;
break;
case 2:
r = p; g = v; b = t;
break;
case 3:
r = p; g = q; b = v;
break;
case 4:
r = t; g = p; b = v;
break;
default:
r = v; g = p; b = q;
break;
}
}
void setRGB(const uint8_t r, const uint8_t g, const uint8_t b) {
this->r = r;
this->g = g;
this->b = b;
}
void setOff() {
this->r = 0;
this->g = 0;
this->b = 0;
}
/** add two colors */
Color operator + (const Color o) const {
return Color(r+o.r, g+o.g, b+o.g);
}
Color operator * (const float f) const {
return Color(r*f, g*f, b*f);
}
};