ESPHome 2025.12.1
Loading...
Searching...
No Matches
esp_color_correction.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace esphome::light {
6
8 public:
9 ESPColorCorrection() : max_brightness_(255, 255, 255, 255) {}
10 void set_max_brightness(const Color &max_brightness) { this->max_brightness_ = max_brightness; }
11 void set_local_brightness(uint8_t local_brightness) { this->local_brightness_ = local_brightness; }
12 void calculate_gamma_table(float gamma);
13 inline Color color_correct(Color color) const ESPHOME_ALWAYS_INLINE {
14 // corrected = (uncorrected * max_brightness * local_brightness) ^ gamma
15 return Color(this->color_correct_red(color.red), this->color_correct_green(color.green),
16 this->color_correct_blue(color.blue), this->color_correct_white(color.white));
17 }
18 inline uint8_t color_correct_red(uint8_t red) const ESPHOME_ALWAYS_INLINE {
19 uint8_t res = esp_scale8_twice(red, this->max_brightness_.red, this->local_brightness_);
20 return this->gamma_table_[res];
21 }
22 inline uint8_t color_correct_green(uint8_t green) const ESPHOME_ALWAYS_INLINE {
23 uint8_t res = esp_scale8_twice(green, this->max_brightness_.green, this->local_brightness_);
24 return this->gamma_table_[res];
25 }
26 inline uint8_t color_correct_blue(uint8_t blue) const ESPHOME_ALWAYS_INLINE {
27 uint8_t res = esp_scale8_twice(blue, this->max_brightness_.blue, this->local_brightness_);
28 return this->gamma_table_[res];
29 }
30 inline uint8_t color_correct_white(uint8_t white) const ESPHOME_ALWAYS_INLINE {
31 uint8_t res = esp_scale8_twice(white, this->max_brightness_.white, this->local_brightness_);
32 return this->gamma_table_[res];
33 }
34 inline Color color_uncorrect(Color color) const ESPHOME_ALWAYS_INLINE {
35 // uncorrected = corrected^(1/gamma) / (max_brightness * local_brightness)
36 return Color(this->color_uncorrect_red(color.red), this->color_uncorrect_green(color.green),
37 this->color_uncorrect_blue(color.blue), this->color_uncorrect_white(color.white));
38 }
39 inline uint8_t color_uncorrect_red(uint8_t red) const ESPHOME_ALWAYS_INLINE {
40 if (this->max_brightness_.red == 0 || this->local_brightness_ == 0)
41 return 0;
42 uint16_t uncorrected = this->gamma_reverse_table_[red] * 255UL;
43 uint16_t res = ((uncorrected / this->max_brightness_.red) * 255UL) / this->local_brightness_;
44 return (uint8_t) std::min(res, uint16_t(255));
45 }
46 inline uint8_t color_uncorrect_green(uint8_t green) const ESPHOME_ALWAYS_INLINE {
47 if (this->max_brightness_.green == 0 || this->local_brightness_ == 0)
48 return 0;
49 uint16_t uncorrected = this->gamma_reverse_table_[green] * 255UL;
50 uint16_t res = ((uncorrected / this->max_brightness_.green) * 255UL) / this->local_brightness_;
51 return (uint8_t) std::min(res, uint16_t(255));
52 }
53 inline uint8_t color_uncorrect_blue(uint8_t blue) const ESPHOME_ALWAYS_INLINE {
54 if (this->max_brightness_.blue == 0 || this->local_brightness_ == 0)
55 return 0;
56 uint16_t uncorrected = this->gamma_reverse_table_[blue] * 255UL;
57 uint16_t res = ((uncorrected / this->max_brightness_.blue) * 255UL) / this->local_brightness_;
58 return (uint8_t) std::min(res, uint16_t(255));
59 }
60 inline uint8_t color_uncorrect_white(uint8_t white) const ESPHOME_ALWAYS_INLINE {
61 if (this->max_brightness_.white == 0 || this->local_brightness_ == 0)
62 return 0;
63 uint16_t uncorrected = this->gamma_reverse_table_[white] * 255UL;
64 uint16_t res = ((uncorrected / this->max_brightness_.white) * 255UL) / this->local_brightness_;
65 return (uint8_t) std::min(res, uint16_t(255));
66 }
67
68 protected:
69 uint8_t gamma_table_[256];
72 uint8_t local_brightness_{255};
73};
74
75} // namespace esphome::light
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
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
Color color_uncorrect(Color color) const ESPHOME_ALWAYS_INLINE
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 color_correct_white(uint8_t white) const ESPHOME_ALWAYS_INLINE
uint8_t color_uncorrect_white(uint8_t white) const ESPHOME_ALWAYS_INLINE
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