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.
9 *
10 * The class provides conversion between 8-bit per channel RGB values and the
11 * packed 16-bit representation commonly used by embedded displays.
12 */
13class RGB565 {
14 public:
15
16 /// Creates a black RGB565 color.
17 RGB565() = default;
18
19 /// Creates a color from 8-bit red, green, and blue components.
20 RGB565(uint8_t r, uint8_t g, uint8_t b) { setValue(r, g, b); }
21
22 /// Creates a color from a packed RGB565 value.
23 explicit RGB565(uint16_t packed) : value(packed) {}
24
25 /// Sets the color from 8-bit red, green, and blue components.
26 void setValue(uint8_t r, uint8_t g, uint8_t b) {
27 value = (static_cast<uint16_t>(r & 0xF8) << 8) |
28 (static_cast<uint16_t>(g & 0xFC) << 3) |
29 (static_cast<uint16_t>(b) >> 3);
30 }
31
32 /// Sets the color from a packed RGB565 value.
33 void setValue(uint16_t packed) { value = packed; }
34
35 /// Returns the red component expanded to 8 bits.
36 uint8_t getRed() const {
37 const uint8_t red = (value >> 11) & 0x1F;
38 return (red << 3) | (red >> 2);
39 }
40
41 /// Returns the green component expanded to 8 bits.
42 uint8_t getGreen() const {
43 const uint8_t green = (value >> 5) & 0x3F;
44 return (green << 2) | (green >> 4);
45 }
46
47 /// Returns the blue component expanded to 8 bits.
48 uint8_t getBlue() const {
49 const uint8_t blue = value & 0x1F;
50 return (blue << 3) | (blue >> 2);
51 }
52
53 /// Returns the packed RGB565 value.
54 uint16_t getValue() const { return value; }
55
56 /// Returns size in bits
57 static uint8_t size() { return 16; }
58
59 protected:
60 uint16_t value = 0;
61};
62
63inline bool operator!=(const tinygpu::RGB565& lhs, const tinygpu::RGB565& rhs) {
64 return lhs.getValue() != rhs.getValue();
65}
66} // namespace tinygpu
RGB color stored in 16-bit RGB565 format.
Definition: RGB565.h:13
uint16_t getValue() const
Returns the packed RGB565 value.
Definition: RGB565.h:54
RGB565()=default
Creates a black RGB565 color.
uint8_t getBlue() const
Returns the blue component expanded to 8 bits.
Definition: RGB565.h:48
RGB565(uint16_t packed)
Creates a color from a packed RGB565 value.
Definition: RGB565.h:23
static uint8_t size()
Returns size in bits.
Definition: RGB565.h:57
uint8_t getGreen() const
Returns the green component expanded to 8 bits.
Definition: RGB565.h:42
uint8_t getRed() const
Returns the red component expanded to 8 bits.
Definition: RGB565.h:36
void setValue(uint16_t packed)
Sets the color from a packed RGB565 value.
Definition: RGB565.h:33
uint16_t value
Definition: RGB565.h:60
RGB565(uint8_t r, uint8_t g, uint8_t b)
Creates a color from 8-bit red, green, and blue components.
Definition: RGB565.h:20
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:26
Definition: AVIWriter.h:9
bool operator!=(const tinygpu::RGB565 &lhs, const tinygpu::RGB565 &rhs)
Definition: RGB565.h:63