Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
color.hpp
Go to the documentation of this file.
1/**
2 * @file color.hpp
3 * @brief RGB color types and utilities
4 *
5 * Pure math operations on 8-bit RGB colors. No hardware dependencies.
6 * Designed for LED feedback on embedded instruments - every function
7 * is ISR-safe and branchless where practical.
8 *
9 * Rgb: Simple {r, g, b} aggregate. Layout-compatible with RgbLedState.
10 * GAMMA8[256]: Perceptual gamma correction LUT (gamma 2.8).
11 * gamma_correct(): Apply GAMMA8 to an Rgb color.
12 * rgb_lerp(): Linear interpolation between two colors.
13 * rgb_scale(): Scale a color by brightness (0-255).
14 * gradient(): Evaluate a multi-stop color gradient at a position.
15 *
16 * Source: Gamma table from Adafruit LED Tricks guide (gamma 2.8).
17 *
18 * Usage:
19 * @code
20 * using sbl::control::display::Rgb;
21 * constexpr Rgb red{255, 0, 0};
22 * Rgb dimmed = rgb_scale(red, 128); // half brightness
23 * Rgb mixed = rgb_lerp(red, {0,0,255}, 128); // purple
24 * Rgb out = gamma_correct(mixed); // perceptually correct PWM
25 * @endcode
26 */
27
28#ifndef SBL_CONTROL_DISPLAY_COLOR_HPP_
29#define SBL_CONTROL_DISPLAY_COLOR_HPP_
30
31#include <cstdint>
32
34
35/** Simple 8-bit RGB color triple. Aggregate - constexpr constructible. */
36struct Rgb {
37 uint8_t r;
38 uint8_t g;
39 uint8_t b;
40};
41
42/**
43 * Gamma correction LUT (gamma 2.8). Maps perceived-linear 0-255 to PWM duty.
44 * Under that model, uncorrected PWM 128 appears (128/255)^(1/2.8), about
45 * 78%, as bright as 255.
46 */
47// clang-format off
48inline constexpr uint8_t GAMMA8[256] = {
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
51 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
52 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
53 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
54 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
55 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
56 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
57 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
58 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
59 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
60 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
61 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
62 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
63 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
64 215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255,
65};
66// clang-format on
67
68/** Apply gamma correction to an RGB color. */
69inline Rgb gamma_correct(const Rgb& c) { return {GAMMA8[c.r], GAMMA8[c.g], GAMMA8[c.b]}; }
70
71/** Linear interpolation between two colors. t=0 -> a, t=255 -> b. */
72inline Rgb rgb_lerp(const Rgb& a, const Rgb& b, uint8_t t) {
73 auto mix = [](uint8_t x, uint8_t y, uint8_t t) -> uint8_t {
74 return static_cast<uint8_t>(x + ((static_cast<int16_t>(y) - x) * t) / 255);
75 };
76 return {mix(a.r, b.r, t), mix(a.g, b.g, t), mix(a.b, b.b, t)};
77}
78
79/** Scale a color by brightness (0-255). 255 = full, 128 ~ half, 0 = off. */
80inline Rgb rgb_scale(const Rgb& c, uint8_t brightness) {
81 return {
82 static_cast<uint8_t>((static_cast<uint16_t>(c.r) * brightness) >> 8),
83 static_cast<uint8_t>((static_cast<uint16_t>(c.g) * brightness) >> 8),
84 static_cast<uint8_t>((static_cast<uint16_t>(c.b) * brightness) >> 8),
85 };
86}
87
88/**
89 * @brief Evaluate a multi-stop color gradient at a position.
90 *
91 * Divides the 0-65535 range into (n_stops - 1) equal segments and
92 * interpolates between adjacent stops. Endpoints are exact.
93 *
94 * @param stops Array of color stops (at least 2).
95 * @param n_stops Number of stops (2-16).
96 * @param position Position in gradient (0 = first stop, 65535 = last stop).
97 *
98 * Example:
99 * @code
100 * constexpr Rgb stops[] = {{0,0,255}, {0,255,0}, {255,0,0}};
101 * Rgb c = gradient(stops, 3, 32768); // midpoint -> green
102 * @endcode
103 */
104inline Rgb gradient(const Rgb* stops, uint8_t n_stops, uint16_t position) {
105 if (n_stops < 2) {
106 return stops[0];
107 }
108
109 uint8_t segments = n_stops - 1;
110 uint32_t scaled = static_cast<uint32_t>(position) * segments;
111 uint8_t seg = static_cast<uint8_t>(scaled >> 16);
112 if (seg >= segments) {
113 seg = segments - 1;
114 }
115
116 // Fractional position within segment, mapped to 0-255
117 uint8_t frac = static_cast<uint8_t>((scaled - (static_cast<uint32_t>(seg) << 16)) >> 8);
118 return rgb_lerp(stops[seg], stops[seg + 1], frac);
119}
120
121} // namespace sbl::control::display
122
123#endif // SBL_CONTROL_DISPLAY_COLOR_HPP_
Indicators: RGB LED state and colors.
Definition color.hpp:33
Rgb gamma_correct(const Rgb &c)
Definition color.hpp:69
Rgb gradient(const Rgb *stops, uint8_t n_stops, uint16_t position)
Evaluate a multi-stop color gradient at a position.
Definition color.hpp:104
constexpr uint8_t GAMMA8[256]
Definition color.hpp:48
Rgb rgb_lerp(const Rgb &a, const Rgb &b, uint8_t t)
Definition color.hpp:72
Rgb rgb_scale(const Rgb &c, uint8_t brightness)
Definition color.hpp:80