ESPHome 2026.10.2
Loading...
Searching...
No Matches
esp_color_correction.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/hal.h"
5
6namespace esphome::light {
7
9struct GammaTable {
10 uint16_t lut[256];
11 uint16_t gamma_x100;
12};
13
16inline uint8_t gamma_table_reverse_search(const uint16_t *table, uint16_t target) {
17 uint8_t lo = 0, hi = 255;
18 while (lo < hi) {
19 uint8_t mid = (lo + hi + 1) / 2;
20 if (progmem_read_uint16(&table[mid]) <= target) {
21 lo = mid;
22 } else {
23 hi = mid - 1;
24 }
25 }
26 return lo;
27}
28
30 public:
31 void set_max_brightness(const Color &max_brightness) { this->max_brightness_ = max_brightness; }
32 void set_local_brightness(uint8_t local_brightness) { this->local_brightness_ = local_brightness; }
33 void set_gamma_table(const uint16_t *table) { this->gamma_table_ = table; }
34 inline Color color_correct(Color color) const ESPHOME_ALWAYS_INLINE {
35 // corrected = (uncorrected * max_brightness * local_brightness) ^ gamma
36 return Color(this->color_correct_red(color.red), this->color_correct_green(color.green),
37 this->color_correct_blue(color.blue), this->color_correct_white(color.white));
38 }
39 inline uint8_t color_correct_red(uint8_t red) const ESPHOME_ALWAYS_INLINE {
40 uint8_t res = esp_scale8_twice(red, this->max_brightness_.red, this->local_brightness_);
41 return this->gamma_correct_(res);
42 }
43 inline uint8_t color_correct_green(uint8_t green) const ESPHOME_ALWAYS_INLINE {
44 uint8_t res = esp_scale8_twice(green, this->max_brightness_.green, this->local_brightness_);
45 return this->gamma_correct_(res);
46 }
47 inline uint8_t color_correct_blue(uint8_t blue) const ESPHOME_ALWAYS_INLINE {
48 uint8_t res = esp_scale8_twice(blue, this->max_brightness_.blue, this->local_brightness_);
49 return this->gamma_correct_(res);
50 }
51 inline uint8_t color_correct_white(uint8_t white) const ESPHOME_ALWAYS_INLINE {
52 uint8_t res = esp_scale8_twice(white, this->max_brightness_.white, this->local_brightness_);
53 return this->gamma_correct_(res);
54 }
55 Color color_uncorrect(Color color) const;
56 inline uint8_t color_uncorrect_red(uint8_t red) const ESPHOME_ALWAYS_INLINE {
57 return this->color_uncorrect_channel_(red, this->max_brightness_.red);
58 }
59 inline uint8_t color_uncorrect_green(uint8_t green) const ESPHOME_ALWAYS_INLINE {
60 return this->color_uncorrect_channel_(green, this->max_brightness_.green);
61 }
62 inline uint8_t color_uncorrect_blue(uint8_t blue) const ESPHOME_ALWAYS_INLINE {
63 return this->color_uncorrect_channel_(blue, this->max_brightness_.blue);
64 }
65 inline uint8_t color_uncorrect_white(uint8_t white) const ESPHOME_ALWAYS_INLINE {
66 return this->color_uncorrect_channel_(white, this->max_brightness_.white);
67 }
68
69 protected:
71 uint8_t gamma_correct_(uint8_t value) const;
73 uint8_t gamma_uncorrect_(uint8_t value) const;
76 uint8_t color_uncorrect_channel_(uint8_t value, uint8_t max_brightness) const;
77
78 const uint16_t *gamma_table_{nullptr};
79 Color max_brightness_{255, 255, 255, 255};
80 uint8_t local_brightness_{255};
81};
82
83} // namespace esphome::light
uint8_t gamma_correct_(uint8_t value) const
Forward gamma: read uint16 PROGMEM table, convert to uint8.
uint8_t color_uncorrect_red(uint8_t red) const ESPHOME_ALWAYS_INLINE
uint8_t color_correct_blue(uint8_t blue) const ESPHOME_ALWAYS_INLINE
void set_max_brightness(const Color &max_brightness)
uint8_t color_correct_red(uint8_t red) const ESPHOME_ALWAYS_INLINE
uint8_t color_uncorrect_channel_(uint8_t value, uint8_t max_brightness) const
Shared body of color_uncorrect_{red,green,blue,white}.
void set_local_brightness(uint8_t local_brightness)
Color color_correct(Color color) const ESPHOME_ALWAYS_INLINE
uint8_t color_uncorrect_blue(uint8_t blue) const ESPHOME_ALWAYS_INLINE
void set_gamma_table(const uint16_t *table)
uint8_t color_uncorrect_green(uint8_t green) const ESPHOME_ALWAYS_INLINE
uint8_t color_correct_green(uint8_t green) const ESPHOME_ALWAYS_INLINE
uint8_t gamma_uncorrect_(uint8_t value) const
Reverse gamma: binary search the forward PROGMEM table.
uint8_t color_correct_white(uint8_t white) const ESPHOME_ALWAYS_INLINE
uint8_t color_uncorrect_white(uint8_t white) const ESPHOME_ALWAYS_INLINE
uint8_t gamma_table_reverse_search(const uint16_t *table, uint16_t target)
Binary search a monotonically increasing uint16[256] PROGMEM table.
uint16_t progmem_read_uint16(const uint16_t *addr)
Definition hal.h:45
uint8_t red
Definition color.h:31
uint8_t white
Definition color.h:43
uint8_t green
Definition color.h:35
uint8_t blue
Definition color.h:39
A gamma curve as codegen emits it into PROGMEM: the lookup table and the gamma it was built from.