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