ESPHome  2024.10.2
esp_color_view.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/color.h"
4 #include "esp_hsv_color.h"
5 #include "esp_color_correction.h"
6 
7 namespace esphome {
8 namespace light {
9 
11  public:
12  virtual void set(const Color &color) = 0;
13  virtual void set_red(uint8_t red) = 0;
14  virtual void set_green(uint8_t green) = 0;
15  virtual void set_blue(uint8_t blue) = 0;
16  virtual void set_white(uint8_t white) = 0;
17  virtual void set_effect_data(uint8_t effect_data) = 0;
18  virtual void fade_to_white(uint8_t amnt) = 0;
19  virtual void fade_to_black(uint8_t amnt) = 0;
20  virtual void lighten(uint8_t delta) = 0;
21  virtual void darken(uint8_t delta) = 0;
22  void set(const ESPHSVColor &color) { this->set_hsv(color); }
23  void set_hsv(const ESPHSVColor &color) {
24  Color rgb = color.to_rgb();
25  this->set_rgb(rgb.r, rgb.g, rgb.b);
26  }
27  void set_rgb(uint8_t red, uint8_t green, uint8_t blue) {
28  this->set_red(red);
29  this->set_green(green);
30  this->set_blue(blue);
31  }
32  void set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
33  this->set_rgb(red, green, blue);
34  this->set_white(white);
35  }
36 };
37 
39  public:
40  ESPColorView(uint8_t *red, uint8_t *green, uint8_t *blue, uint8_t *white, uint8_t *effect_data,
41  const ESPColorCorrection *color_correction)
42  : red_(red),
43  green_(green),
44  blue_(blue),
45  white_(white),
46  effect_data_(effect_data),
47  color_correction_(color_correction) {}
48  ESPColorView &operator=(const Color &rhs) {
49  this->set(rhs);
50  return *this;
51  }
53  this->set_hsv(rhs);
54  return *this;
55  }
56  void set(const Color &color) override { this->set_rgbw(color.r, color.g, color.b, color.w); }
57  void set_red(uint8_t red) override { *this->red_ = this->color_correction_->color_correct_red(red); }
58  void set_green(uint8_t green) override { *this->green_ = this->color_correction_->color_correct_green(green); }
59  void set_blue(uint8_t blue) override { *this->blue_ = this->color_correction_->color_correct_blue(blue); }
60  void set_white(uint8_t white) override {
61  if (this->white_ == nullptr)
62  return;
63  *this->white_ = this->color_correction_->color_correct_white(white);
64  }
65  void set_effect_data(uint8_t effect_data) override {
66  if (this->effect_data_ == nullptr)
67  return;
68  *this->effect_data_ = effect_data;
69  }
70  void fade_to_white(uint8_t amnt) override { this->set(this->get().fade_to_white(amnt)); }
71  void fade_to_black(uint8_t amnt) override { this->set(this->get().fade_to_black(amnt)); }
72  void lighten(uint8_t delta) override { this->set(this->get().lighten(delta)); }
73  void darken(uint8_t delta) override { this->set(this->get().darken(delta)); }
74  Color get() const { return Color(this->get_red(), this->get_green(), this->get_blue(), this->get_white()); }
75  uint8_t get_red() const { return this->color_correction_->color_uncorrect_red(*this->red_); }
76  uint8_t get_red_raw() const { return *this->red_; }
77  uint8_t get_green() const { return this->color_correction_->color_uncorrect_green(*this->green_); }
78  uint8_t get_green_raw() const { return *this->green_; }
79  uint8_t get_blue() const { return this->color_correction_->color_uncorrect_blue(*this->blue_); }
80  uint8_t get_blue_raw() const { return *this->blue_; }
81  uint8_t get_white() const {
82  if (this->white_ == nullptr)
83  return 0;
84  return this->color_correction_->color_uncorrect_white(*this->white_);
85  }
86  uint8_t get_white_raw() const {
87  if (this->white_ == nullptr)
88  return 0;
89  return *this->white_;
90  }
91  uint8_t get_effect_data() const {
92  if (this->effect_data_ == nullptr)
93  return 0;
94  return *this->effect_data_;
95  }
96  void raw_set_color_correction(const ESPColorCorrection *color_correction) {
97  this->color_correction_ = color_correction;
98  }
99 
100  protected:
101  uint8_t *const red_;
102  uint8_t *const green_;
103  uint8_t *const blue_;
104  uint8_t *const white_;
105  uint8_t *const effect_data_;
107 };
108 
109 } // namespace light
110 } // namespace esphome
void set_green(uint8_t green) override
void darken(uint8_t delta) override
void fade_to_white(uint8_t amnt) override
void lighten(uint8_t delta) override
virtual void set_white(uint8_t white)=0
const ESPColorCorrection * color_correction_
virtual void fade_to_white(uint8_t amnt)=0
void set_white(uint8_t white) override
void set_effect_data(uint8_t effect_data) override
uint8_t g
Definition: color.h:18
ESPColorView & operator=(const Color &rhs)
ESPColorView & operator=(const ESPHSVColor &rhs)
void set_hsv(const ESPHSVColor &color)
void fade_to_black(uint8_t amnt) override
virtual void set_red(uint8_t red)=0
void set_red(uint8_t red) override
void set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white)
virtual void fade_to_black(uint8_t amnt)=0
ESPColorView(uint8_t *red, uint8_t *green, uint8_t *blue, uint8_t *white, uint8_t *effect_data, const ESPColorCorrection *color_correction)
virtual void set_effect_data(uint8_t effect_data)=0
void set_rgb(uint8_t red, uint8_t green, uint8_t blue)
void raw_set_color_correction(const ESPColorCorrection *color_correction)
uint8_t b
Definition: color.h:22
virtual void lighten(uint8_t delta)=0
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t r
Definition: color.h:14
virtual void darken(uint8_t delta)=0
void set_blue(uint8_t blue) override
uint8_t get_effect_data() const
virtual void set_blue(uint8_t blue)=0
virtual void set_green(uint8_t green)=0