LCDGFX LCD display driver  1.2.0
Lightweight graphics library for SSD1306, SSD1325, SSD1327, SSD1331, SSD1351, SH1106, SH1107, IL9163, ST7735, ST7789, ILI9341, PCD8544 displays over I2C/SPI
color.h
Go to the documentation of this file.
1 /*
2  MIT License
3 
4  Copyright (c) 2026, Alexey Dynda
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23 */
57 #pragma once
58 
59 #include <stdint.h>
60 
61 namespace lcdgfx
62 {
63 
68 struct Color
69 {
71  uint32_t rgb;
72 
74  static constexpr Color from_rgb(uint8_t r, uint8_t g, uint8_t b)
75  {
76  return Color{ (uint32_t)((uint32_t)r << 16) | (uint32_t)((uint32_t)g << 8) | (uint32_t)b };
77  }
78 
80  static constexpr Color from_rgb888(uint32_t v)
81  {
82  return Color{ v & 0x00FFFFFFu };
83  }
84 
86  static constexpr Color from_rgb565(uint16_t v)
87  {
88  // 5-bit r/b expanded to 8 by replicating high bits into low.
89  return from_rgb(
90  (uint8_t)(((v >> 11) & 0x1F) * 255u / 31u),
91  (uint8_t)(((v >> 5) & 0x3F) * 255u / 63u),
92  (uint8_t)(( v & 0x1F) * 255u / 31u));
93  }
94 
96  static constexpr Color from_rgb332(uint8_t v)
97  {
98  return from_rgb(
99  (uint8_t)(((v >> 5) & 0x07) * 255u / 7u),
100  (uint8_t)(((v >> 2) & 0x07) * 255u / 7u),
101  (uint8_t)(( v & 0x03) * 255u / 3u));
102  }
103 
105  static constexpr Color from_gray4(uint8_t v)
106  {
107  return from_rgb(
108  (uint8_t)(((v & 0x0F) * 255u) / 15u),
109  (uint8_t)(((v & 0x0F) * 255u) / 15u),
110  (uint8_t)(((v & 0x0F) * 255u) / 15u));
111  }
112 
114  static constexpr Color from_mono(uint8_t v)
115  {
116  return v ? from_rgb(0xFF, 0xFF, 0xFF) : from_rgb(0, 0, 0);
117  }
118 
120  constexpr uint8_t r() const { return (uint8_t)((rgb >> 16) & 0xFFu); }
122  constexpr uint8_t g() const { return (uint8_t)((rgb >> 8) & 0xFFu); }
124  constexpr uint8_t b() const { return (uint8_t)(rgb & 0xFFu); }
125 
127  constexpr uint32_t to_rgb888() const { return rgb & 0x00FFFFFFu; }
128 
130  constexpr uint16_t to_rgb565() const
131  {
132  return (uint16_t)(((rgb >> 8) & 0xF800u) |
133  ((rgb >> 5) & 0x07E0u) |
134  ((rgb >> 3) & 0x001Fu));
135  }
136 
138  constexpr uint8_t to_rgb332() const
139  {
140  return (uint8_t)(((rgb >> 16) & 0xE0u) |
141  ((rgb >> 11) & 0x1Cu) |
142  ((rgb >> 6) & 0x03u));
143  }
144 
147  constexpr uint8_t to_gray4() const
148  {
149  // Integer approximation of 0.299*R + 0.587*G + 0.114*B, then >>4.
150  // 77*R + 150*G + 29*B fits in 16 bits and matches the standard
151  // ITU-R BT.601 weighting to within one LSB.
152  return (uint8_t)((((uint32_t)r() * 77u +
153  (uint32_t)g() * 150u +
154  (uint32_t)b() * 29u) >> 8) >> 4);
155  }
156 
158  constexpr uint8_t to_mono() const
159  {
160  return (((uint32_t)r() * 77u +
161  (uint32_t)g() * 150u +
162  (uint32_t)b() * 29u) >= (128u * 256u)) ? 1u : 0u;
163  }
164 
168  template <unsigned Bpp>
169  constexpr uint32_t to_native() const;
170 
171  constexpr bool operator==(const Color &other) const { return to_rgb888() == other.to_rgb888(); }
172  constexpr bool operator!=(const Color &other) const { return !(*this == other); }
173 };
174 
175 template <> constexpr uint32_t Color::to_native<1>() const { return to_mono(); }
176 template <> constexpr uint32_t Color::to_native<4>() const { return to_gray4(); }
177 template <> constexpr uint32_t Color::to_native<8>() const { return to_rgb332(); }
178 template <> constexpr uint32_t Color::to_native<16>() const { return to_rgb565(); }
179 template <> constexpr uint32_t Color::to_native<24>() const { return to_rgb888(); }
180 
181 } // namespace lcdgfx
static constexpr Color from_rgb(uint8_t r, uint8_t g, uint8_t b)
Construct from 8-bit-per-channel RGB.
Definition: color.h:74
static constexpr Color from_rgb565(uint16_t v)
Construct from a 5-6-5 packed 16-bit value, expanding back to RGB888.
Definition: color.h:86
constexpr uint32_t to_native() const
Compile-time-selectable convert: to_native<bpp>().
constexpr uint8_t r() const
Red channel (0..255).
Definition: color.h:120
constexpr uint16_t to_rgb565() const
Pack as 5-6-5 RGB565 (matches RGB_COLOR16 macro for matching inputs).
Definition: color.h:130
constexpr uint32_t to_rgb888() const
Pack as 24-bit RGB888 (0x00RRGGBB).
Definition: color.h:127
constexpr uint8_t b() const
Blue channel (0..255).
Definition: color.h:124
static constexpr Color from_rgb888(uint32_t v)
Construct from a 24-bit packed value (e.g.
Definition: color.h:80
static constexpr Color from_gray4(uint8_t v)
Construct from a 4-bit luminance (0..15) — broadcasts to all channels.
Definition: color.h:105
static constexpr Color from_rgb332(uint8_t v)
Construct from a 3-3-2 packed 8-bit value, expanding back to RGB888.
Definition: color.h:96
24-bit RGB colour with constexpr conversions to all native lcdgfx pixel formats.
Definition: color.h:68
constexpr uint8_t to_gray4() const
Pack as 4-bit luminance using the standard 0.299R + 0.587G + 0.114B weighting, rounded to 4 bits...
Definition: color.h:147
Definition: color.h:61
uint32_t rgb
0x00RRGGBB (R in bits 23..16, G in bits 15..8, B in bits 7..0).
Definition: color.h:71
constexpr uint8_t g() const
Green channel (0..255).
Definition: color.h:122
constexpr uint8_t to_mono() const
Reduce to 1 bit (0 or 1).
Definition: color.h:158
static constexpr Color from_mono(uint8_t v)
Construct from a 1-bit value (0 = black, anything else = white).
Definition: color.h:114
constexpr uint8_t to_rgb332() const
Pack as 3-3-2 RGB332 (matches RGB_COLOR8 macro for matching inputs).
Definition: color.h:138