diff --git a/esphome/core/color.cpp b/esphome/core/color.cpp index 58d995db2f..7e390b2354 100644 --- a/esphome/core/color.cpp +++ b/esphome/core/color.cpp @@ -2,10 +2,8 @@ namespace esphome { -const Color Color::BLACK(0, 0, 0, 0); -const Color Color::WHITE(255, 255, 255, 255); - -const Color COLOR_BLACK(0, 0, 0, 0); -const Color COLOR_WHITE(255, 255, 255, 255); +// C++20 constinit ensures compile-time initialization (stored in ROM) +constinit const Color Color::BLACK(0, 0, 0, 0); +constinit const Color Color::WHITE(255, 255, 255, 255); } // namespace esphome diff --git a/esphome/core/color.h b/esphome/core/color.h index 1c43fd9d3e..2b307bb438 100644 --- a/esphome/core/color.h +++ b/esphome/core/color.h @@ -5,7 +5,9 @@ namespace esphome { -inline static uint8_t esp_scale8(uint8_t i, uint8_t scale) { return (uint16_t(i) * (1 + uint16_t(scale))) / 256; } +inline static constexpr uint8_t esp_scale8(uint8_t i, uint8_t scale) { + return (uint16_t(i) * (1 + uint16_t(scale))) / 256; +} struct Color { union { @@ -31,17 +33,20 @@ struct Color { uint32_t raw_32; }; - inline Color() ESPHOME_ALWAYS_INLINE : r(0), g(0), b(0), w(0) {} // NOLINT - inline Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE : r(red), g(green), b(blue), w(0) {} + inline constexpr Color() ESPHOME_ALWAYS_INLINE : raw_32(0) {} // NOLINT + inline constexpr Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE : r(red), + g(green), + b(blue), + w(0) {} - inline Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE : r(red), - g(green), - b(blue), - w(white) {} - inline explicit Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE : r((colorcode >> 16) & 0xFF), - g((colorcode >> 8) & 0xFF), - b((colorcode >> 0) & 0xFF), - w((colorcode >> 24) & 0xFF) {} + inline constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE : r(red), + g(green), + b(blue), + w(white) {} + inline explicit constexpr Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE : r((colorcode >> 16) & 0xFF), + g((colorcode >> 8) & 0xFF), + b((colorcode >> 0) & 0xFF), + w((colorcode >> 24) & 0xFF) {} inline bool is_on() ESPHOME_ALWAYS_INLINE { return this->raw_32 != 0; } @@ -169,9 +174,4 @@ struct Color { static const Color WHITE; }; -ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21") -extern const Color COLOR_BLACK; -ESPDEPRECATED("Use Color::WHITE instead of COLOR_WHITE", "v1.21") -extern const Color COLOR_WHITE; - } // namespace esphome