mirror of
https://github.com/esphome/esphome.git
synced 2025-07-28 14:16:40 +00:00
Reduce RAM usage by optimizing Color constant storage (#9339)
This commit is contained in:
parent
f7019a4ed7
commit
20ba035e3b
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user