Reduce RAM usage by optimizing Color constant storage (#9339)

This commit is contained in:
J. Nick Koston 2025-07-05 22:30:18 -05:00 committed by GitHub
parent f7019a4ed7
commit 20ba035e3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 21 deletions

View File

@ -2,10 +2,8 @@
namespace esphome { namespace esphome {
const Color Color::BLACK(0, 0, 0, 0); // C++20 constinit ensures compile-time initialization (stored in ROM)
const Color Color::WHITE(255, 255, 255, 255); constinit const Color Color::BLACK(0, 0, 0, 0);
constinit const Color Color::WHITE(255, 255, 255, 255);
const Color COLOR_BLACK(0, 0, 0, 0);
const Color COLOR_WHITE(255, 255, 255, 255);
} // namespace esphome } // namespace esphome

View File

@ -5,7 +5,9 @@
namespace esphome { 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 { struct Color {
union { union {
@ -31,14 +33,17 @@ struct Color {
uint32_t raw_32; uint32_t raw_32;
}; };
inline Color() ESPHOME_ALWAYS_INLINE : r(0), g(0), b(0), w(0) {} // NOLINT inline constexpr Color() ESPHOME_ALWAYS_INLINE : raw_32(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(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), inline constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE : r(red),
g(green), g(green),
b(blue), b(blue),
w(white) {} w(white) {}
inline explicit Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE : r((colorcode >> 16) & 0xFF), inline explicit constexpr Color(uint32_t colorcode) ESPHOME_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) {}
@ -169,9 +174,4 @@ struct Color {
static const Color WHITE; 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 } // namespace esphome