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