TinyGPU
Loading...
Searching...
No Matches
RGB565.h
Go to the documentation of this file.
1#pragma once
2#include <stddef.h>
3#include <stdint.h>
4
5namespace tinygpu {
6
7/**
8 * @brief RGB color stored in 16-bit RGB565 format, byte-swapped from the
9 * conventional bit layout.
10 *
11 * The class provides conversion between 8-bit per channel RGB values and the
12 * packed 16-bit representation commonly used by embedded displays.
13 *
14 * Storage note: `value` holds the two bytes of the conventional RGB565
15 * packing (R:5 G:6 B:5, red in the high bits) swapped - i.e. exactly the
16 * byte order every SPI/QSPI panel driver in this codebase (DisplayDriverSPI,
17 * DisplayDriverQSPI) needs on the wire. That's the overwhelmingly common
18 * consumer, so getValue()/writeData() need no per-pixel swap. Anything that
19 * instead needs the conventional (native) bit layout - LVGL's own pixel
20 * buffer, SDL's SDL_PIXELFORMAT_RGB565 texture upload, or a file format like
21 * AVI/BMP that declares standard RGB565 bitmasks - must go through
22 * getValueSwapped() (or construct via the swapped packed value) to undo it.
23 */
24class RGB565 {
25 public:
26
27 /// Creates a black RGB565 color.
28 RGB565() = default;
29
30 /// Creates a color from 8-bit red, green, and blue components.
31 RGB565(uint8_t r, uint8_t g, uint8_t b) { setValue(r, g, b); }
32
33 /// Creates a color from a packed value already in this class's stored
34 /// (byte-swapped) order - i.e. one that round-trips through getValue().
35 /// To decode a packed value in the conventional (native) bit layout
36 /// instead - e.g. a raw pixel from LVGL's own buffer - byte-swap it
37 /// first: RGB565(swapBytes(nativePacked)).
38 explicit RGB565(uint16_t packed) : value(packed) {}
39
40 /// Creates a color from semantic red/green/blue intensities, regardless
41 /// of this class's own constructor argument order. Prefer this in
42 /// generic code that converts between pixel formats.
43 static RGB565 fromRGB(uint8_t r, uint8_t g, uint8_t b) {
44 return RGB565(r, g, b);
45 }
46
47 /// Byte-swaps a 16-bit value; used to convert between this class's stored
48 /// (wire) order and the conventional (native) RGB565 bit layout.
49 static uint16_t swapBytes(uint16_t v) {
50 return static_cast<uint16_t>((v << 8) | (v >> 8));
51 }
52
53 /// Sets the color from 8-bit red, green, and blue components.
54 void setValue(uint8_t r, uint8_t g, uint8_t b) {
55 const uint16_t packed = (static_cast<uint16_t>(r & 0xF8) << 8) |
56 (static_cast<uint16_t>(g & 0xFC) << 3) |
57 (static_cast<uint16_t>(b) >> 3);
58 value = swapBytes(packed);
59 }
60
61 /// Sets the color from a packed value already in this class's stored
62 /// (byte-swapped) order - see the constructor note above.
63 void setValue(uint16_t packed) { value = packed; }
64
65 /// Returns the red component expanded to 8 bits.
66 uint8_t getRed() const {
67 const uint8_t red = (swapBytes(value) >> 11) & 0x1F;
68 return (red << 3) | (red >> 2);
69 }
70
71 /// Returns the green component expanded to 8 bits.
72 uint8_t getGreen() const {
73 const uint8_t green = (swapBytes(value) >> 5) & 0x3F;
74 return (green << 2) | (green >> 4);
75 }
76
77 /// Returns the blue component expanded to 8 bits.
78 uint8_t getBlue() const {
79 const uint8_t blue = swapBytes(value) & 0x1F;
80 return (blue << 3) | (blue >> 2);
81 }
82
83 /// Returns the packed value in this class's stored (byte-swapped) order -
84 /// i.e. wire-ready for DisplayDriverSPI/DisplayDriverQSPI with no further
85 /// swap needed.
86 uint16_t getValue() const { return value; }
87
88 /// Returns the packed value in the conventional (native) RGB565 bit
89 /// layout (R:5 G:6 B:5, red in the high bits) - the order LVGL, SDL, and
90 /// standard RGB565 file-format bitmasks (e.g. AVI/BMP) expect.
91 uint16_t getValueSwapped() const { return swapBytes(value); }
92
93 /// Returns size in bits
94 static uint8_t size() { return 16; }
95
96 protected:
97 uint16_t value = 0;
98};
99
100inline bool operator!=(const tinygpu::RGB565& lhs, const tinygpu::RGB565& rhs) {
101 return lhs.getValue() != rhs.getValue();
102}
103} // namespace tinygpu
RGB color stored in 16-bit RGB565 format, byte-swapped from the conventional bit layout.
Definition: RGB565.h:24
uint16_t getValue() const
Definition: RGB565.h:86
RGB565()=default
Creates a black RGB565 color.
uint8_t getBlue() const
Returns the blue component expanded to 8 bits.
Definition: RGB565.h:78
RGB565(uint16_t packed)
Definition: RGB565.h:38
static uint8_t size()
Returns size in bits.
Definition: RGB565.h:94
uint8_t getGreen() const
Returns the green component expanded to 8 bits.
Definition: RGB565.h:72
uint16_t getValueSwapped() const
Definition: RGB565.h:91
uint8_t getRed() const
Returns the red component expanded to 8 bits.
Definition: RGB565.h:66
void setValue(uint16_t packed)
Definition: RGB565.h:63
uint16_t value
Definition: RGB565.h:97
static uint16_t swapBytes(uint16_t v)
Definition: RGB565.h:49
RGB565(uint8_t r, uint8_t g, uint8_t b)
Creates a color from 8-bit red, green, and blue components.
Definition: RGB565.h:31
void setValue(uint8_t r, uint8_t g, uint8_t b)
Sets the color from 8-bit red, green, and blue components.
Definition: RGB565.h:54
static RGB565 fromRGB(uint8_t r, uint8_t g, uint8_t b)
Definition: RGB565.h:43
Definition: DSIBusESP32.h:19
bool operator!=(const tinygpu::RGB565 &lhs, const tinygpu::RGB565 &rhs)
Definition: RGB565.h:100