idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
color.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
13#include <bit>
14#include <cstdint>
15#include <esp_lcd_panel_io.h>
16
21namespace idfxx::lcd {
22
27enum class rgb_element_order : int {
28 rgb = LCD_RGB_ELEMENT_ORDER_RGB,
29 bgr = LCD_RGB_ELEMENT_ORDER_BGR,
30};
31
36enum class rgb_data_endian : int {
37 big = LCD_RGB_DATA_ENDIAN_BIG,
38 little = LCD_RGB_DATA_ENDIAN_LITTLE,
39};
40
55class rgb565 {
56public:
58 constexpr rgb565() noexcept = default;
59
70 constexpr rgb565(uint8_t r, uint8_t g, uint8_t b) noexcept
71 : _repr(_to_panel_order(static_cast<uint16_t>(((r & 0xF8u) << 8) | ((g & 0xFCu) << 3) | (b >> 3)))) {}
72
80 [[nodiscard]] static constexpr rgb565 from_value(uint16_t value) noexcept {
81 rgb565 c;
82 c._repr = _to_panel_order(value);
83 return c;
84 }
85
91 [[nodiscard]] constexpr uint16_t value() const noexcept { return _to_panel_order(_repr); }
92
94 friend constexpr bool operator==(rgb565, rgb565) noexcept = default;
95
96private:
97 // Byte-swaps a packed value into big-endian panel order (an involution,
98 // so it converts back as well).
99 static constexpr uint16_t _to_panel_order(uint16_t value) noexcept {
100 if constexpr (std::endian::native == std::endian::little) {
101 return std::byteswap(value);
102 } else {
103 return value;
104 }
105 }
106
107 uint16_t _repr = 0;
108};
109
110static_assert(sizeof(rgb565) == 2);
111
112} // namespace idfxx::lcd
A 16-bit RGB565 color, stored in panel byte order.
Definition color.hpp:55
constexpr rgb565() noexcept=default
Creates black.
constexpr uint16_t value() const noexcept
Returns the packed RGB565 value.
Definition color.hpp:91
static constexpr rgb565 from_value(uint16_t value) noexcept
Creates a color from a packed RGB565 value.
Definition color.hpp:80
friend constexpr bool operator==(rgb565, rgb565) noexcept=default
Compares two colors for equality.
LCD driver classes.
Definition color.hpp:21
rgb_element_order
RGB element order for LCD panels.
Definition color.hpp:27
@ bgr
BGR element order.
@ rgb
RGB element order.
rgb_data_endian
RGB data endian for LCD panels.
Definition color.hpp:36
@ little
RGB data endian: LSB first.
@ big
RGB data endian: MSB first.