SensESP 3.4.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
crgb.h
Go to the documentation of this file.
1#ifndef SENSESP_SYSTEM_CRGB_H_
2#define SENSESP_SYSTEM_CRGB_H_
3
4#include <cmath>
5#include <cstdint>
6
7namespace sensesp {
8
16// clang-format off
17constexpr uint8_t kGammaTable[256] = {
18 0, 21, 28, 34, 39, 43, 46, 50, 53, 56, 59, 61, 64, 66, 68, 70,
19 72, 74, 76, 78, 80, 82, 84, 85, 87, 89, 90, 92, 93, 95, 96, 98,
20 99, 101, 102, 103, 105, 106, 107, 109, 110, 111, 112, 114, 115, 116, 117, 118,
21 119, 120, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
22 136, 137, 138, 139, 140, 141, 142, 143, 144, 144, 145, 146, 147, 148, 149, 150,
23 150, 151, 152, 153, 154, 155, 155, 156, 157, 158, 159, 159, 160, 161, 162, 162,
24 163, 164, 165, 165, 166, 167, 167, 168, 169, 170, 170, 171, 172, 172, 173, 174,
25 174, 175, 176, 176, 177, 178, 178, 179, 180, 180, 181, 181, 182, 183, 183, 184,
26 185, 185, 186, 186, 187, 188, 188, 189, 189, 190, 190, 191, 192, 192, 193, 193,
27 194, 194, 195, 196, 196, 197, 197, 198, 198, 199, 199, 200, 200, 201, 201, 202,
28 202, 203, 203, 204, 204, 205, 205, 206, 206, 207, 207, 208, 208, 209, 209, 210,
29 210, 211, 211, 212, 212, 213, 213, 214, 214, 215, 215, 215, 216, 216, 217, 217,
30 218, 218, 219, 219, 219, 220, 220, 221, 221, 222, 222, 222, 223, 223, 224, 224,
31 225, 225, 225, 226, 226, 227, 227, 227, 228, 228, 229, 229, 229, 230, 230, 231,
32 231, 231, 232, 232, 233, 233, 233, 234, 234, 234, 235, 235, 236, 236, 236, 237,
33 237, 237, 238, 238, 238, 239, 239, 240, 240, 240, 241, 241, 241, 242, 242, 255,
34};
35// clang-format on
36
49inline uint8_t gamma_correct(uint8_t value) {
50 return kGammaTable[value];
51}
52
59struct CRGB {
60 uint8_t r;
61 uint8_t g;
62 uint8_t b;
63
65 constexpr CRGB() noexcept : r(0), g(0), b(0) {}
66
68 constexpr CRGB(uint8_t ir, uint8_t ig, uint8_t ib) noexcept
69 : r(ir), g(ig), b(ib) {}
70
72 constexpr CRGB(const CRGB& rhs) noexcept = default;
73
75 CRGB& operator=(const CRGB& rhs) noexcept = default;
76
83 constexpr CRGB operator-(const CRGB& rhs) const noexcept {
84 return CRGB(r > rhs.r ? r - rhs.r : 0, g > rhs.g ? g - rhs.g : 0,
85 b > rhs.b ? b - rhs.b : 0);
86 }
87
88 // Predefined color constants
89 static const CRGB Black;
90 static const CRGB Red;
91 static const CRGB Yellow;
92 static const CRGB Blue;
93};
94
95// Static color constant definitions
96inline constexpr CRGB CRGB::Black{0, 0, 0};
97inline constexpr CRGB CRGB::Red{255, 0, 0};
98inline constexpr CRGB CRGB::Yellow{255, 255, 0};
99inline constexpr CRGB CRGB::Blue{0, 0, 255};
100
111inline uint8_t apply_brightness(uint8_t value, uint8_t brightness) {
112 return static_cast<uint8_t>(gamma_correct(value) * brightness / 255);
113}
114
126inline uint8_t rgb_to_luminance(uint8_t r, uint8_t g, uint8_t b) {
127 // Round to nearest: the weights sum to just under 1.0 in floating point, so
128 // truncation would map pure white (255,255,255) to 254 instead of 255.
129 return static_cast<uint8_t>(std::lround(0.2126 * r + 0.7152 * g + 0.0722 * b));
130}
131
140inline CRGB blend(const CRGB& p1, const CRGB& p2, uint8_t amountOfP2) {
141 if (amountOfP2 == 0) {
142 return p1;
143 }
144 if (amountOfP2 == 255) {
145 return p2;
146 }
147
148 // Linear interpolation for each channel using integer math
149 auto lerp8 = [](uint8_t a, uint8_t b, uint8_t frac) -> uint8_t {
150 if (b > a) {
151 uint8_t delta = b - a;
152 uint8_t scaled = (static_cast<uint16_t>(delta) * frac) >> 8;
153 return a + scaled;
154 } else {
155 uint8_t delta = a - b;
156 uint8_t scaled = (static_cast<uint16_t>(delta) * frac) >> 8;
157 return a - scaled;
158 }
159 };
160
161 return CRGB(lerp8(p1.r, p2.r, amountOfP2), lerp8(p1.g, p2.g, amountOfP2),
162 lerp8(p1.b, p2.b, amountOfP2));
163}
164
165} // namespace sensesp
166
167#endif // SENSESP_SYSTEM_CRGB_H_
uint8_t apply_brightness(uint8_t value, uint8_t brightness)
Apply gamma correction and brightness scaling to a color value.
Definition crgb.h:111
uint8_t rgb_to_luminance(uint8_t r, uint8_t g, uint8_t b)
Convert RGB color to perceptual luminance.
Definition crgb.h:126
constexpr uint8_t kGammaTable[256]
Gamma correction lookup table (γ=2.2, inverted).
Definition crgb.h:17
uint8_t gamma_correct(uint8_t value)
Apply gamma correction to an 8-bit value.
Definition crgb.h:49
CRGB blend(const CRGB &p1, const CRGB &p2, uint8_t amountOfP2)
Linear interpolation (blend) between two colors.
Definition crgb.h:140
Minimal RGB color struct.
Definition crgb.h:59
static const CRGB Blue
Definition crgb.h:92
constexpr CRGB operator-(const CRGB &rhs) const noexcept
Binary subtraction operator with saturation.
Definition crgb.h:83
constexpr CRGB(const CRGB &rhs) noexcept=default
Copy constructor.
uint8_t g
Definition crgb.h:61
constexpr CRGB(uint8_t ir, uint8_t ig, uint8_t ib) noexcept
RGB constructor.
Definition crgb.h:68
static const CRGB Red
Definition crgb.h:90
CRGB & operator=(const CRGB &rhs) noexcept=default
Assignment operator.
static const CRGB Black
Definition crgb.h:89
static const CRGB Yellow
Definition crgb.h:91
constexpr CRGB() noexcept
Default constructor - initializes to black.
Definition crgb.h:65
uint8_t r
Definition crgb.h:60
uint8_t b
Definition crgb.h:62