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