ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
channel_colors.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <cstring>
6
7#include "light_traits.h"
10
11namespace esphome::light {
12
20 static constexpr uint8_t NO_WHITE = 0xFF;
22 static constexpr uint8_t RESERVED_WHITE = 0xFE;
23
24 constexpr ChannelColors() : r(0), g(0), b(0), w(NO_WHITE) {}
25 constexpr ChannelColors(uint8_t r_offset, uint8_t g_offset, uint8_t b_offset)
26 : r(r_offset), g(g_offset), b(b_offset), w(NO_WHITE) {}
27 constexpr ChannelColors(uint8_t r_offset, uint8_t g_offset, uint8_t b_offset, uint8_t w_offset)
28 : r(r_offset), g(g_offset), b(b_offset), w(w_offset) {}
29
30 union {
31 struct {
32 uint8_t r;
33 uint8_t g;
34 uint8_t b;
35 uint8_t w;
36 };
37 uint8_t raw[4];
38 };
39
40 inline bool has_white() const { return (this->w & 0x80) == 0; }
41
42 inline size_t bytes_per_led() const { return this->w == NO_WHITE ? 3 : 4; }
43
44 inline Color get(const uint8_t *color_data, int32_t index) const ESPHOME_ALWAYS_INLINE {
45 if (this->w == NO_WHITE)
46 return Color(color_data[index * 3 + this->r], color_data[index * 3 + this->g], color_data[index * 3 + this->b]);
47 return Color(color_data[index * 4 + this->r], color_data[index * 4 + this->g], color_data[index * 4 + this->b],
48 this->has_white() ? color_data[index * 4 + this->w] : 0);
49 }
50
51 uint8_t get_channel(const uint8_t *color_data, int32_t index, uint8_t channel) const {
52 const uint8_t offset = this->raw[channel];
53 return (offset & 0x80) == 0 ? color_data[index * this->bytes_per_led() + offset] : 0;
54 }
55
56 void set(uint8_t *color_data, int32_t index, Color color) const {
57 if (this->w == NO_WHITE) {
58 color_data[index * 3 + this->r] = color.red;
59 color_data[index * 3 + this->g] = color.green;
60 color_data[index * 3 + this->b] = color.blue;
61 } else {
62 color_data[index * 4 + this->r] = color.red;
63 color_data[index * 4 + this->g] = color.green;
64 color_data[index * 4 + this->b] = color.blue;
65 if (this->has_white())
66 color_data[index * 4 + this->w] = color.white;
67 }
68 }
69
70 void set_channel(uint8_t *color_data, int32_t index, uint8_t channel, uint8_t value) const {
71 const uint8_t offset = this->raw[channel];
72 if ((offset & 0x80) == 0)
73 color_data[index * this->bytes_per_led() + offset] = value;
74 }
75
80 const char *to_string(char *buf) const {
81 memset(buf, 'X', 4);
82 buf[this->r] = 'R';
83 buf[this->g] = 'G';
84 buf[this->b] = 'B';
85 if (this->has_white()) {
86 buf[this->w] = 'W';
87 }
88 buf[this->bytes_per_led()] = '\0';
89 return buf;
90 }
91
102};
103
104static_assert(offsetof(ChannelColors, raw) == COLOR_CHANNEL_R);
105static_assert(offsetof(ChannelColors, r) == COLOR_CHANNEL_R);
106static_assert(offsetof(ChannelColors, g) == COLOR_CHANNEL_G);
107static_assert(offsetof(ChannelColors, b) == COLOR_CHANNEL_B);
108static_assert(offsetof(ChannelColors, w) == COLOR_CHANNEL_W);
109
110} // namespace esphome::light
uint8_t raw[35]
Definition bl0939.h:0
This class is used to represent the capabilities of a light.
Definition light_traits.h:9
void set_supported_color_modes(ColorModeMask supported_color_modes)
static float float b
@ RGB_WHITE
RGB color output and a separate white output.
@ RGB
RGB color output.
@ WHITE
White output only (use only if the light also has another color mode such as RGB).
uint32_t value
Definition hal.cpp:41
uint8_t red
Definition color.h:37
uint8_t white
Definition color.h:49
uint8_t green
Definition color.h:41
uint8_t blue
Definition color.h:45
Which byte of an addressable LED's data carries each colour.
constexpr ChannelColors(uint8_t r_offset, uint8_t g_offset, uint8_t b_offset)
void set_channel(uint8_t *color_data, int32_t index, uint8_t channel, uint8_t value) const
static constexpr uint8_t NO_WHITE
Value of w for an RGB LED strip that uses 3 bytes per led.
const char * to_string(char *buf) const
Write the order back out as text, e.g.
constexpr ChannelColors(uint8_t r_offset, uint8_t g_offset, uint8_t b_offset, uint8_t w_offset)
Color get(const uint8_t *color_data, int32_t index) const ESPHOME_ALWAYS_INLINE
void set(uint8_t *color_data, int32_t index, Color color) const
LightTraits traits() const
Colour modes of a strip with this channel order.
uint8_t get_channel(const uint8_t *color_data, int32_t index, uint8_t channel) const
static constexpr uint8_t RESERVED_WHITE
Value of w for an RGB LED strip that uses 4 bytes per led where the 4th byte is reserved.