PatrixNode/src/patrix/display/DisplayMatrix_common.h
2025-01-25 19:19:09 +01:00

93 lines
1.9 KiB
C++

#ifndef DISPLAY_MATRIX_COMMON_H
#define DISPLAY_MATRIX_COMMON_H
#include <Arduino.h>
#define _ false
#define X true
#define countof(a) (sizeof(a)/sizeof(a[0]))
struct RGBA {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
static RGBA gray(const uint8_t brightness) {
return gray(brightness, 255);
}
static RGBA gray(const uint8_t brightness, const uint8_t alpha) {
return {brightness, brightness, brightness, alpha};
}
static RGBA rnd() {
return rnd(255);
}
static RGBA rnd(const uint8_t alpha) {
return {static_cast<uint8_t>(random(255)), static_cast<uint8_t>(random(255)), static_cast<uint8_t>(random(255)), alpha};
}
RGBA factor(const double factor) const {
return {
limit(r * factor),
limit(g * factor),
limit(b * factor),
a
};
}
protected:
static uint8_t limit(const double newValue) {
return static_cast<uint8_t>(round(max(0.0, min(255.0, newValue))));
}
};
struct RGB {
uint8_t r;
uint8_t g;
uint8_t b;
RGB &operator =(const RGBA& color) {
r = color.r;
g = color.g;
b = color.b;
// blend(&this->r, color.r, color.a);
// blend(&this->g, color.g, color.a);
// blend(&this->b, color.b, color.a);
return *this;
}
protected:
static uint8_t limit(const double newValue) {
return static_cast<uint8_t>(round(max(0.0, min(255.0, newValue))));
}
static void blend(uint8_t *target, const uint8_t color, const uint8_t alpha) {
const auto factor = alpha / 255.0;
const auto newValue = (*target * (1 - factor) + color * factor) / 2.0;
*target = limit(newValue);
}
};
extern const RGBA Transparent;
extern const RGBA Black;
extern const RGBA Red;
extern const RGBA Yellow;
extern const RGBA Orange;
extern const RGBA Green;
extern const RGBA Cyan;
extern const RGBA Blue;
extern const RGBA Magenta;
extern const RGBA Violet;
extern const RGBA Gray;
extern const RGBA White;
#endif