|
1 | 1 | #pragma once |
2 | 2 | #include <algorithm> |
3 | 3 | #include <cstdint> |
| 4 | +#include <cstring> |
4 | 5 |
|
5 | 6 | struct RGB565 { |
6 | | - uint16_t r : 5; |
7 | | - uint16_t g : 6; |
8 | 7 | uint16_t b : 5; |
| 8 | + uint16_t g : 6; |
| 9 | + uint16_t r : 5; |
9 | 10 |
|
10 | 11 | constexpr RGB565() { |
11 | 12 | } |
12 | 13 |
|
13 | 14 | constexpr RGB565(uint8_t red, uint8_t green, uint8_t blue) |
14 | | - : r(red >> 3) |
| 15 | + : b(blue >> 3) |
15 | 16 | , g(green >> 2) |
16 | | - , b(blue >> 3) { |
| 17 | + , r(red >> 3) { |
17 | 18 | } |
18 | 19 |
|
19 | 20 | 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)) |
21 | 22 | , 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) { |
23 | 30 | } |
24 | 31 |
|
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; |
29 | 36 | } |
30 | 37 |
|
| 38 | + // Returns endianness with r as MSB |
31 | 39 | constexpr uint16_t raw() const { |
32 | 40 | return (r << 11) | (g << 5) | b; |
33 | 41 | } |
|
0 commit comments