mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	Migrate COLOR constants to Color class & disallow implicit conversions to Color (#2093)
Co-authored-by: Xo Wang <xo@geekshavefeelings.com>
This commit is contained in:
		| @@ -42,7 +42,7 @@ void AdalightLightEffect::reset_frame_(light::AddressableLight &it) { | |||||||
|  |  | ||||||
| void AdalightLightEffect::blank_all_leds_(light::AddressableLight &it) { | void AdalightLightEffect::blank_all_leds_(light::AddressableLight &it) { | ||||||
|   for (int led = it.size(); led-- > 0;) { |   for (int led = it.size(); led-- > 0;) { | ||||||
|     it[led].set(COLOR_BLACK); |     it[led].set(Color::BLACK); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -461,7 +461,7 @@ bool Image::get_pixel(int x, int y) const { | |||||||
| } | } | ||||||
| Color Image::get_color_pixel(int x, int y) const { | Color Image::get_color_pixel(int x, int y) const { | ||||||
|   if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_) |   if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_) | ||||||
|     return 0; |     return Color::BLACK; | ||||||
|   const uint32_t pos = (x + y * this->width_) * 3; |   const uint32_t pos = (x + y * this->width_) * 3; | ||||||
|   const uint32_t color32 = (pgm_read_byte(this->data_start_ + pos + 2) << 0) | |   const uint32_t color32 = (pgm_read_byte(this->data_start_ + pos + 2) << 0) | | ||||||
|                            (pgm_read_byte(this->data_start_ + pos + 1) << 8) | |                            (pgm_read_byte(this->data_start_ + pos + 1) << 8) | | ||||||
| @@ -470,7 +470,7 @@ Color Image::get_color_pixel(int x, int y) const { | |||||||
| } | } | ||||||
| Color Image::get_grayscale_pixel(int x, int y) const { | Color Image::get_grayscale_pixel(int x, int y) const { | ||||||
|   if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_) |   if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_) | ||||||
|     return 0; |     return Color::BLACK; | ||||||
|   const uint32_t pos = (x + y * this->width_); |   const uint32_t pos = (x + y * this->width_); | ||||||
|   const uint8_t gray = pgm_read_byte(this->data_start_ + pos); |   const uint8_t gray = pgm_read_byte(this->data_start_ + pos); | ||||||
|   return Color(gray | gray << 8 | gray << 16 | gray << 24); |   return Color(gray | gray << 8 | gray << 16 | gray << 24); | ||||||
| @@ -493,10 +493,10 @@ bool Animation::get_pixel(int x, int y) const { | |||||||
| } | } | ||||||
| Color Animation::get_color_pixel(int x, int y) const { | Color Animation::get_color_pixel(int x, int y) const { | ||||||
|   if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_) |   if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_) | ||||||
|     return 0; |     return Color::BLACK; | ||||||
|   const uint32_t frame_index = this->width_ * this->height_ * this->current_frame_; |   const uint32_t frame_index = this->width_ * this->height_ * this->current_frame_; | ||||||
|   if (frame_index >= this->width_ * this->height_ * this->animation_frame_count_) |   if (frame_index >= this->width_ * this->height_ * this->animation_frame_count_) | ||||||
|     return 0; |     return Color::BLACK; | ||||||
|   const uint32_t pos = (x + y * this->width_ + frame_index) * 3; |   const uint32_t pos = (x + y * this->width_ + frame_index) * 3; | ||||||
|   const uint32_t color32 = (pgm_read_byte(this->data_start_ + pos + 2) << 0) | |   const uint32_t color32 = (pgm_read_byte(this->data_start_ + pos + 2) << 0) | | ||||||
|                            (pgm_read_byte(this->data_start_ + pos + 1) << 8) | |                            (pgm_read_byte(this->data_start_ + pos + 1) << 8) | | ||||||
| @@ -505,10 +505,10 @@ Color Animation::get_color_pixel(int x, int y) const { | |||||||
| } | } | ||||||
| Color Animation::get_grayscale_pixel(int x, int y) const { | Color Animation::get_grayscale_pixel(int x, int y) const { | ||||||
|   if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_) |   if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_) | ||||||
|     return 0; |     return Color::BLACK; | ||||||
|   const uint32_t frame_index = this->width_ * this->height_ * this->current_frame_; |   const uint32_t frame_index = this->width_ * this->height_ * this->current_frame_; | ||||||
|   if (frame_index >= this->width_ * this->height_ * this->animation_frame_count_) |   if (frame_index >= this->width_ * this->height_ * this->animation_frame_count_) | ||||||
|     return 0; |     return Color::BLACK; | ||||||
|   const uint32_t pos = (x + y * this->width_ + frame_index); |   const uint32_t pos = (x + y * this->width_ + frame_index); | ||||||
|   const uint8_t gray = pgm_read_byte(this->data_start_ + pos); |   const uint8_t gray = pgm_read_byte(this->data_start_ + pos); | ||||||
|   return Color(gray | gray << 8 | gray << 16 | gray << 24); |   return Color(gray | gray << 8 | gray << 16 | gray << 24); | ||||||
|   | |||||||
| @@ -225,7 +225,7 @@ void ILI9341M5Stack::initialize() { | |||||||
|   this->width_ = 320; |   this->width_ = 320; | ||||||
|   this->height_ = 240; |   this->height_ = 240; | ||||||
|   this->invert_display_(true); |   this->invert_display_(true); | ||||||
|   this->fill_internal_(COLOR_BLACK); |   this->fill_internal_(Color::BLACK); | ||||||
| } | } | ||||||
|  |  | ||||||
| //   24_TFT display | //   24_TFT display | ||||||
| @@ -233,7 +233,7 @@ void ILI9341TFT24::initialize() { | |||||||
|   this->init_lcd_(INITCMD_TFT); |   this->init_lcd_(INITCMD_TFT); | ||||||
|   this->width_ = 240; |   this->width_ = 240; | ||||||
|   this->height_ = 320; |   this->height_ = 320; | ||||||
|   this->fill_internal_(COLOR_BLACK); |   this->fill_internal_(Color::BLACK); | ||||||
| } | } | ||||||
|  |  | ||||||
| }  // namespace ili9341 | }  // namespace ili9341 | ||||||
|   | |||||||
| @@ -16,7 +16,7 @@ | |||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace light { | namespace light { | ||||||
|  |  | ||||||
| using ESPColor = Color; | using ESPColor ESPDEPRECATED("esphome::light::ESPColor is deprecated, use esphome::Color instead.") = Color; | ||||||
|  |  | ||||||
| class AddressableLight : public LightOutput, public Component { | class AddressableLight : public LightOutput, public Component { | ||||||
|  public: |  public: | ||||||
|   | |||||||
| @@ -151,7 +151,7 @@ class AddressableScanEffect : public AddressableLightEffect { | |||||||
|   void set_move_interval(uint32_t move_interval) { this->move_interval_ = move_interval; } |   void set_move_interval(uint32_t move_interval) { this->move_interval_ = move_interval; } | ||||||
|   void set_scan_width(uint32_t scan_width) { this->scan_width_ = scan_width; } |   void set_scan_width(uint32_t scan_width) { this->scan_width_ = scan_width; } | ||||||
|   void apply(AddressableLight &it, const Color ¤t_color) override { |   void apply(AddressableLight &it, const Color ¤t_color) override { | ||||||
|     it.all() = COLOR_BLACK; |     it.all() = Color::BLACK; | ||||||
|  |  | ||||||
|     for (auto i = 0; i < this->scan_width_; i++) { |     for (auto i = 0; i < this->scan_width_; i++) { | ||||||
|       it[this->at_led_ + i] = current_color; |       it[this->at_led_ + i] = current_color; | ||||||
| @@ -201,7 +201,7 @@ class AddressableTwinkleEffect : public AddressableLightEffect { | |||||||
|         else |         else | ||||||
|           view.set_effect_data(new_pos); |           view.set_effect_data(new_pos); | ||||||
|       } else { |       } else { | ||||||
|         view = COLOR_BLACK; |         view = Color::BLACK; | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     while (random_float() < this->twinkle_probability_) { |     while (random_float() < this->twinkle_probability_) { | ||||||
| @@ -272,7 +272,7 @@ class AddressableFireworksEffect : public AddressableLightEffect { | |||||||
|   explicit AddressableFireworksEffect(const std::string &name) : AddressableLightEffect(name) {} |   explicit AddressableFireworksEffect(const std::string &name) : AddressableLightEffect(name) {} | ||||||
|   void start() override { |   void start() override { | ||||||
|     auto &it = *this->get_addressable_(); |     auto &it = *this->get_addressable_(); | ||||||
|     it.all() = COLOR_BLACK; |     it.all() = Color::BLACK; | ||||||
|   } |   } | ||||||
|   void apply(AddressableLight &it, const Color ¤t_color) override { |   void apply(AddressableLight &it, const Color ¤t_color) override { | ||||||
|     const uint32_t now = millis(); |     const uint32_t now = millis(); | ||||||
|   | |||||||
| @@ -7,8 +7,6 @@ namespace ssd1306_base { | |||||||
|  |  | ||||||
| static const char *const TAG = "ssd1306"; | static const char *const TAG = "ssd1306"; | ||||||
|  |  | ||||||
| static const uint8_t BLACK = 0; |  | ||||||
| static const uint8_t WHITE = 1; |  | ||||||
| static const uint8_t SSD1306_MAX_CONTRAST = 255; | static const uint8_t SSD1306_MAX_CONTRAST = 255; | ||||||
|  |  | ||||||
| static const uint8_t SSD1306_COMMAND_DISPLAY_OFF = 0xAE; | static const uint8_t SSD1306_COMMAND_DISPLAY_OFF = 0xAE; | ||||||
| @@ -89,7 +87,7 @@ void SSD1306::setup() { | |||||||
|  |  | ||||||
|   set_brightness(this->brightness_); |   set_brightness(this->brightness_); | ||||||
|  |  | ||||||
|   this->fill(BLACK);  // clear display - ensures we do not see garbage at power-on |   this->fill(Color::BLACK);  // clear display - ensures we do not see garbage at power-on | ||||||
|   this->display();           // ...write buffer, which actually clears the display's memory |   this->display();           // ...write buffer, which actually clears the display's memory | ||||||
|  |  | ||||||
|   this->turn_on(); |   this->turn_on(); | ||||||
|   | |||||||
| @@ -106,7 +106,7 @@ void SSD1322::setup() { | |||||||
|   this->data(180); |   this->data(180); | ||||||
|   this->command(SSD1322_ENABLEGRAYSCALETABLE); |   this->command(SSD1322_ENABLEGRAYSCALETABLE); | ||||||
|   set_brightness(this->brightness_); |   set_brightness(this->brightness_); | ||||||
|   this->fill(COLOR_BLACK);  // clear display - ensures we do not see garbage at power-on |   this->fill(Color::BLACK);  // clear display - ensures we do not see garbage at power-on | ||||||
|   this->display();           // ...write buffer, which actually clears the display's memory |   this->display();           // ...write buffer, which actually clears the display's memory | ||||||
|   this->turn_on();           // display ON |   this->turn_on();           // display ON | ||||||
| } | } | ||||||
|   | |||||||
| @@ -7,8 +7,6 @@ namespace ssd1325_base { | |||||||
|  |  | ||||||
| static const char *const TAG = "ssd1325"; | static const char *const TAG = "ssd1325"; | ||||||
|  |  | ||||||
| static const uint8_t BLACK = 0; |  | ||||||
| static const uint8_t WHITE = 15; |  | ||||||
| static const uint8_t SSD1325_MAX_CONTRAST = 127; | static const uint8_t SSD1325_MAX_CONTRAST = 127; | ||||||
| static const uint8_t SSD1325_COLORMASK = 0x0f; | static const uint8_t SSD1325_COLORMASK = 0x0f; | ||||||
| static const uint8_t SSD1325_COLORSHIFT = 4; | static const uint8_t SSD1325_COLORSHIFT = 4; | ||||||
| @@ -114,7 +112,7 @@ void SSD1325::setup() { | |||||||
|   this->command(0x0D | 0x02); |   this->command(0x0D | 0x02); | ||||||
|   this->command(SSD1325_NORMALDISPLAY);  // set display mode |   this->command(SSD1325_NORMALDISPLAY);  // set display mode | ||||||
|   set_brightness(this->brightness_); |   set_brightness(this->brightness_); | ||||||
|   this->fill(BLACK);  // clear display - ensures we do not see garbage at power-on |   this->fill(Color::BLACK);  // clear display - ensures we do not see garbage at power-on | ||||||
|   this->display();           // ...write buffer, which actually clears the display's memory |   this->display();           // ...write buffer, which actually clears the display's memory | ||||||
|   this->turn_on();           // display ON |   this->turn_on();           // display ON | ||||||
| } | } | ||||||
|   | |||||||
| @@ -78,7 +78,7 @@ void SSD1327::setup() { | |||||||
|   this->command(0x1C); |   this->command(0x1C); | ||||||
|   this->command(SSD1327_NORMALDISPLAY);  // set display mode |   this->command(SSD1327_NORMALDISPLAY);  // set display mode | ||||||
|   set_brightness(this->brightness_); |   set_brightness(this->brightness_); | ||||||
|   this->fill(COLOR_BLACK);  // clear display - ensures we do not see garbage at power-on |   this->fill(Color::BLACK);  // clear display - ensures we do not see garbage at power-on | ||||||
|   this->display();           // ...write buffer, which actually clears the display's memory |   this->display();           // ...write buffer, which actually clears the display's memory | ||||||
|   this->turn_on();           // display ON |   this->turn_on();           // display ON | ||||||
| } | } | ||||||
|   | |||||||
| @@ -7,8 +7,6 @@ namespace ssd1331_base { | |||||||
|  |  | ||||||
| static const char *const TAG = "ssd1331"; | static const char *const TAG = "ssd1331"; | ||||||
|  |  | ||||||
| static const uint16_t BLACK = 0; |  | ||||||
| static const uint16_t WHITE = 0xffff; |  | ||||||
| static const uint16_t SSD1331_COLORMASK = 0xffff; | static const uint16_t SSD1331_COLORMASK = 0xffff; | ||||||
| static const uint8_t SSD1331_MAX_CONTRASTA = 0x91; | static const uint8_t SSD1331_MAX_CONTRASTA = 0x91; | ||||||
| static const uint8_t SSD1331_MAX_CONTRASTB = 0x50; | static const uint8_t SSD1331_MAX_CONTRASTB = 0x50; | ||||||
| @@ -78,7 +76,7 @@ void SSD1331::setup() { | |||||||
|   this->command(SSD1331_MASTERCURRENT);  // 0x87 |   this->command(SSD1331_MASTERCURRENT);  // 0x87 | ||||||
|   this->command(0x06); |   this->command(0x06); | ||||||
|   set_brightness(this->brightness_); |   set_brightness(this->brightness_); | ||||||
|   this->fill(BLACK);  // clear display - ensures we do not see garbage at power-on |   this->fill(Color::BLACK);  // clear display - ensures we do not see garbage at power-on | ||||||
|   this->display();           // ...write buffer, which actually clears the display's memory |   this->display();           // ...write buffer, which actually clears the display's memory | ||||||
|   this->turn_on();           // display ON |   this->turn_on();           // display ON | ||||||
| } | } | ||||||
|   | |||||||
| @@ -7,8 +7,6 @@ namespace ssd1351_base { | |||||||
|  |  | ||||||
| static const char *const TAG = "ssd1351"; | static const char *const TAG = "ssd1351"; | ||||||
|  |  | ||||||
| static const uint16_t BLACK = 0; |  | ||||||
| static const uint16_t WHITE = 0xffff; |  | ||||||
| static const uint16_t SSD1351_COLORMASK = 0xffff; | static const uint16_t SSD1351_COLORMASK = 0xffff; | ||||||
| static const uint8_t SSD1351_MAX_CONTRAST = 15; | static const uint8_t SSD1351_MAX_CONTRAST = 15; | ||||||
| static const uint8_t SSD1351_BYTESPERPIXEL = 2; | static const uint8_t SSD1351_BYTESPERPIXEL = 2; | ||||||
| @@ -87,7 +85,7 @@ void SSD1351::setup() { | |||||||
|   this->data(0x80); |   this->data(0x80); | ||||||
|   this->data(0xC8); |   this->data(0xC8); | ||||||
|   set_brightness(this->brightness_); |   set_brightness(this->brightness_); | ||||||
|   this->fill(BLACK);  // clear display - ensures we do not see garbage at power-on |   this->fill(Color::BLACK);  // clear display - ensures we do not see garbage at power-on | ||||||
|   this->display();           // ...write buffer, which actually clears the display's memory |   this->display();           // ...write buffer, which actually clears the display's memory | ||||||
|   this->turn_on();           // display ON |   this->turn_on();           // display ON | ||||||
| } | } | ||||||
|   | |||||||
| @@ -40,7 +40,7 @@ void WLEDLightEffect::stop() { | |||||||
|  |  | ||||||
| void WLEDLightEffect::blank_all_leds_(light::AddressableLight &it) { | void WLEDLightEffect::blank_all_leds_(light::AddressableLight &it) { | ||||||
|   for (int led = it.size(); led-- > 0;) { |   for (int led = it.size(); led-- > 0;) { | ||||||
|     it[led].set(COLOR_BLACK); |     it[led].set(Color::BLACK); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										8
									
								
								esphome/core/color.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								esphome/core/color.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | |||||||
|  | #include "esphome/core/color.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  |  | ||||||
|  | const Color Color::BLACK(0, 0, 0, 0); | ||||||
|  | const Color Color::WHITE(255, 255, 255, 255); | ||||||
|  |  | ||||||
|  | }  // namespace esphome | ||||||
| @@ -38,7 +38,7 @@ struct Color { | |||||||
|                                                                                         g(green), |                                                                                         g(green), | ||||||
|                                                                                         b(blue), |                                                                                         b(blue), | ||||||
|                                                                                         w(white) {} |                                                                                         w(white) {} | ||||||
|   inline Color(uint32_t colorcode) ALWAYS_INLINE : r((colorcode >> 16) & 0xFF), |   inline explicit Color(uint32_t colorcode) ALWAYS_INLINE : r((colorcode >> 16) & 0xFF), | ||||||
|                                                             g((colorcode >> 8) & 0xFF), |                                                             g((colorcode >> 8) & 0xFF), | ||||||
|                                                             b((colorcode >> 0) & 0xFF), |                                                             b((colorcode >> 0) & 0xFF), | ||||||
|                                                             w((colorcode >> 24) & 0xFF) {} |                                                             w((colorcode >> 24) & 0xFF) {} | ||||||
| @@ -143,8 +143,14 @@ struct Color { | |||||||
|   Color fade_to_black(uint8_t amnt) { return *this * amnt; } |   Color fade_to_black(uint8_t amnt) { return *this * amnt; } | ||||||
|   Color lighten(uint8_t delta) { return *this + delta; } |   Color lighten(uint8_t delta) { return *this + delta; } | ||||||
|   Color darken(uint8_t delta) { return *this - delta; } |   Color darken(uint8_t delta) { return *this - delta; } | ||||||
|  |  | ||||||
|  |   static const Color BLACK; | ||||||
|  |   static const Color WHITE; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| static const Color COLOR_BLACK(0, 0, 0); | ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK") | ||||||
|  | static const Color COLOR_BLACK(0, 0, 0, 0); | ||||||
|  | ESPDEPRECATED("Use Color::WHITE instead of COLOR_WHITE") | ||||||
| static const Color COLOR_WHITE(255, 255, 255, 255); | static const Color COLOR_WHITE(255, 255, 255, 255); | ||||||
| };  // namespace esphome |  | ||||||
|  | }  // namespace esphome | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user