ESPHome  2024.11.1
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 ESPHOME_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 ESPHOME_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 ESPHOME_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 ESPHOME_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 ESPHOME_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 ESPHOME_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 ESPHOME_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  uint16_t res = ((uncorrected / this->max_brightness_.red) * 255UL) / this->local_brightness_;
45  return (uint8_t) std::min(res, uint16_t(255));
46  }
47  inline uint8_t color_uncorrect_green(uint8_t green) const ESPHOME_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  uint16_t res = ((uncorrected / this->max_brightness_.green) * 255UL) / this->local_brightness_;
52  return (uint8_t) std::min(res, uint16_t(255));
53  }
54  inline uint8_t color_uncorrect_blue(uint8_t blue) const ESPHOME_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  uint16_t res = ((uncorrected / this->max_brightness_.blue) * 255UL) / this->local_brightness_;
59  return (uint8_t) std::min(res, uint16_t(255));
60  }
61  inline uint8_t color_uncorrect_white(uint8_t white) const ESPHOME_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  uint16_t res = ((uncorrected / this->max_brightness_.white) * 255UL) / this->local_brightness_;
66  return (uint8_t) std::min(res, uint16_t(255));
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
uint8_t white
Definition: color.h:27
uint8_t color_correct_white(uint8_t white) const ESPHOME_ALWAYS_INLINE
void set_max_brightness(const Color &max_brightness)
Color color_uncorrect(Color color) const ESPHOME_ALWAYS_INLINE
uint8_t color_correct_green(uint8_t green) const ESPHOME_ALWAYS_INLINE
uint8_t color_correct_red(uint8_t red) const ESPHOME_ALWAYS_INLINE
uint8_t color_uncorrect_red(uint8_t red) const ESPHOME_ALWAYS_INLINE
void set_local_brightness(uint8_t local_brightness)
uint8_t green
Definition: color.h:19
uint8_t color_uncorrect_blue(uint8_t blue) const ESPHOME_ALWAYS_INLINE
uint8_t blue
Definition: color.h:23
uint8_t color_uncorrect_green(uint8_t green) const ESPHOME_ALWAYS_INLINE
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t color_correct_blue(uint8_t blue) const ESPHOME_ALWAYS_INLINE
uint8_t color_uncorrect_white(uint8_t white) const ESPHOME_ALWAYS_INLINE
Color color_correct(Color color) const ESPHOME_ALWAYS_INLINE
uint8_t red
Definition: color.h:15