ESPHome  2024.4.0
esp_color_correction.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/color.h"
4 
5 namespace esphome {
6 namespace light {
7 
9  public:
10  ESPColorCorrection() : max_brightness_(255, 255, 255, 255) {}
11  void set_max_brightness(const Color &max_brightness) { this->max_brightness_ = max_brightness; }
12  void set_local_brightness(uint8_t local_brightness) { this->local_brightness_ = local_brightness; }
13  void calculate_gamma_table(float gamma);
14  inline Color color_correct(Color color) const ALWAYS_INLINE {
15  // corrected = (uncorrected * max_brightness * local_brightness) ^ gamma
16  return Color(this->color_correct_red(color.red), this->color_correct_green(color.green),
17  this->color_correct_blue(color.blue), this->color_correct_white(color.white));
18  }
19  inline uint8_t color_correct_red(uint8_t red) const ALWAYS_INLINE {
20  uint8_t res = esp_scale8(esp_scale8(red, this->max_brightness_.red), this->local_brightness_);
21  return this->gamma_table_[res];
22  }
23  inline uint8_t color_correct_green(uint8_t green) const ALWAYS_INLINE {
24  uint8_t res = esp_scale8(esp_scale8(green, this->max_brightness_.green), this->local_brightness_);
25  return this->gamma_table_[res];
26  }
27  inline uint8_t color_correct_blue(uint8_t blue) const ALWAYS_INLINE {
28  uint8_t res = esp_scale8(esp_scale8(blue, this->max_brightness_.blue), this->local_brightness_);
29  return this->gamma_table_[res];
30  }
31  inline uint8_t color_correct_white(uint8_t white) const ALWAYS_INLINE {
32  uint8_t res = esp_scale8(esp_scale8(white, this->max_brightness_.white), this->local_brightness_);
33  return this->gamma_table_[res];
34  }
35  inline Color color_uncorrect(Color color) const ALWAYS_INLINE {
36  // uncorrected = corrected^(1/gamma) / (max_brightness * local_brightness)
37  return Color(this->color_uncorrect_red(color.red), this->color_uncorrect_green(color.green),
38  this->color_uncorrect_blue(color.blue), this->color_uncorrect_white(color.white));
39  }
40  inline uint8_t color_uncorrect_red(uint8_t red) const ALWAYS_INLINE {
41  if (this->max_brightness_.red == 0 || this->local_brightness_ == 0)
42  return 0;
43  uint16_t uncorrected = this->gamma_reverse_table_[red] * 255UL;
44  uint8_t res = ((uncorrected / this->max_brightness_.red) * 255UL) / this->local_brightness_;
45  return res;
46  }
47  inline uint8_t color_uncorrect_green(uint8_t green) const ALWAYS_INLINE {
48  if (this->max_brightness_.green == 0 || this->local_brightness_ == 0)
49  return 0;
50  uint16_t uncorrected = this->gamma_reverse_table_[green] * 255UL;
51  uint8_t res = ((uncorrected / this->max_brightness_.green) * 255UL) / this->local_brightness_;
52  return res;
53  }
54  inline uint8_t color_uncorrect_blue(uint8_t blue) const ALWAYS_INLINE {
55  if (this->max_brightness_.blue == 0 || this->local_brightness_ == 0)
56  return 0;
57  uint16_t uncorrected = this->gamma_reverse_table_[blue] * 255UL;
58  uint8_t res = ((uncorrected / this->max_brightness_.blue) * 255UL) / this->local_brightness_;
59  return res;
60  }
61  inline uint8_t color_uncorrect_white(uint8_t white) const ALWAYS_INLINE {
62  if (this->max_brightness_.white == 0 || this->local_brightness_ == 0)
63  return 0;
64  uint16_t uncorrected = this->gamma_reverse_table_[white] * 255UL;
65  uint8_t res = ((uncorrected / this->max_brightness_.white) * 255UL) / this->local_brightness_;
66  return res;
67  }
68 
69  protected:
70  uint8_t gamma_table_[256];
71  uint8_t gamma_reverse_table_[256];
73  uint8_t local_brightness_{255};
74 };
75 
76 } // namespace light
77 } // namespace esphome
Color color_correct(Color color) const ALWAYS_INLINE
Color color_uncorrect(Color color) const ALWAYS_INLINE
uint8_t white
Definition: color.h:27
void set_max_brightness(const Color &max_brightness)
uint8_t color_uncorrect_blue(uint8_t blue) const ALWAYS_INLINE
uint8_t color_correct_white(uint8_t white) const ALWAYS_INLINE
uint8_t color_uncorrect_green(uint8_t green) const ALWAYS_INLINE
uint8_t color_uncorrect_white(uint8_t white) const ALWAYS_INLINE
void set_local_brightness(uint8_t local_brightness)
uint8_t color_correct_green(uint8_t green) const ALWAYS_INLINE
uint8_t green
Definition: color.h:19
uint8_t color_correct_red(uint8_t red) const ALWAYS_INLINE
uint8_t color_correct_blue(uint8_t blue) const ALWAYS_INLINE
uint8_t blue
Definition: color.h:23
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
uint8_t color_uncorrect_red(uint8_t red) const ALWAYS_INLINE
uint8_t red
Definition: color.h:15