Skip to content

Commit 8bd6cc8

Browse files
committed
Reverse order of RGB565 to match memory layout. Add operator=(uint16_t)
1 parent a705ace commit 8bd6cc8

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

util/colors_rgb565.hh

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
#pragma once
22
#include <algorithm>
33
#include <cstdint>
4+
#include <cstring>
45

56
struct RGB565 {
6-
uint16_t r : 5;
7-
uint16_t g : 6;
87
uint16_t b : 5;
8+
uint16_t g : 6;
9+
uint16_t r : 5;
910

1011
constexpr RGB565() {
1112
}
1213

1314
constexpr RGB565(uint8_t red, uint8_t green, uint8_t blue)
14-
: r(red >> 3)
15+
: b(blue >> 3)
1516
, g(green >> 2)
16-
, b(blue >> 3) {
17+
, r(red >> 3) {
1718
}
1819

1920
constexpr RGB565(float red, float green, float blue)
20-
: r(std::clamp<uint16_t>(red * 32.f, 0, 31))
21+
: b(std::clamp<uint16_t>(blue * 32.f, 0, 31))
2122
, g(std::clamp<uint16_t>(green * 64.f, 0, 63))
22-
, b(std::clamp<uint16_t>(blue * 32.f, 0, 31)) {
23+
, r(std::clamp<uint16_t>(red * 32.f, 0, 31)) {
24+
}
25+
26+
constexpr RGB565(uint32_t rgb888)
27+
: b((rgb888 & 0x0000f8) >> 3)
28+
, g((rgb888 & 0x00fc00) >> 10)
29+
, r((rgb888 & 0xf80000) >> 19) {
2330
}
2431

25-
constexpr RGB565(uint32_t raw)
26-
: r((raw & 0xf80000) >> 19)
27-
, g((raw & 0x00fc00) >> 10)
28-
, b((raw & 0x0000f8) >> 3) {
32+
// Construct from raw u16 RGB565 format
33+
RGB565 operator=(uint16_t raw) {
34+
std::memcpy(this, &raw, 2);
35+
return *this;
2936
}
3037

38+
// Returns endianness with r as MSB
3139
constexpr uint16_t raw() const {
3240
return (r << 11) | (g << 5) | b;
3341
}

0 commit comments

Comments
 (0)