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