Skip to content

Commit d1593d6

Browse files
author
Olivier ROMAN
committed
avoid the use of union
1 parent 8a544e1 commit d1593d6

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

src/displayapp/Colors.h

+9-21
Original file line numberDiff line numberDiff line change
@@ -35,47 +35,35 @@ namespace Colors {
3535

3636
class Color {
3737
public:
38-
constexpr Color(uint32_t color) {
39-
data.hexcode = color;
38+
constexpr Color(uint32_t color) : data {color} {
4039
}
4140

4241
operator uint32_t() const {
43-
return data.hexcode;
42+
return data;
4443
}
4544

46-
constexpr Color(uint8_t r, uint8_t g, uint8_t b) {
47-
data.argb.red = r;
48-
data.argb.green = g;
49-
data.argb.blue = b;
45+
constexpr Color(uint8_t r, uint8_t g, uint8_t b)
46+
: data {(static_cast<uint32_t>(r) << 16) | (static_cast<uint32_t>(g) << 8) | static_cast<uint32_t>(b)} {
5047
}
5148

5249
operator lv_color_t() const {
53-
return lv_color_hex(data.hexcode);
50+
return lv_color_hex(data);
5451
}
5552

5653
uint8_t red() const {
57-
return data.argb.red;
54+
return (data & 0xFF0000) >> 16;
5855
}
5956

6057
uint8_t green() const {
61-
return data.argb.green;
58+
return (data & 0x00FF00) >> 8;
6259
}
6360

6461
uint8_t blue() const {
65-
return data.argb.blue;
62+
return (data & 0x0000FF);
6663
}
6764

6865
private:
69-
union {
70-
uint32_t hexcode = 0;
71-
72-
struct {
73-
uint8_t blue = 0;
74-
uint8_t green = 0;
75-
uint8_t red = 0;
76-
uint8_t alpha = 0;
77-
} argb;
78-
} data;
66+
uint32_t data;
7967
};
8068

8169
Color linear_gradient(Color startingColor, Color endingColor, uint8_t progress);

0 commit comments

Comments
 (0)