roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1#pragma once
2
3#include <inttypes.h>
4
5#include "roo_backport.h"
6#include "roo_backport/byte.h"
7#include "roo_io.h"
8#include "roo_io/memory/fill.h"
9
10namespace roo_display {
11
12/// ARGB8888 color stored as a 32-bit unsigned integer.
13///
14/// This is a lightweight, trivially-constructible type that can be passed by
15/// value, with convenience accessors and type safety.
16class Color {
17 public:
18 /// Construct transparent black.
19 Color() : argb_(0) {}
20
21 /// Construct opaque color from RGB.
23 : argb_(0xFF000000LL | (r << 16) | (g << 8) | b) {}
24
25 /// Construct ARGB color from components.
27 : argb_((a << 24) | (r << 16) | (g << 8) | b) {}
28
29 /// Construct from a packed ARGB value.
30 constexpr Color(uint32_t argb) : argb_(argb) {}
31
32 /// Return packed ARGB value.
33 constexpr uint32_t asArgb() const { return argb_; }
34
35 /// Alpha channel.
36 constexpr uint8_t a() const { return (argb_ >> 24) & 0xFF; }
37 /// Red channel.
38 constexpr uint8_t r() const { return (argb_ >> 16) & 0xFF; }
39 /// Green channel.
40 constexpr uint8_t g() const { return (argb_ >> 8) & 0xFF; }
41 /// Blue channel.
42 constexpr uint8_t b() const { return argb_ & 0xFF; }
43
44 /// Set alpha channel.
45 void set_a(uint8_t a) {
46 argb_ &= 0x00FFFFFF;
47 argb_ |= ((uint32_t)a) << 24;
48 }
49
50 /// Set red channel.
51 void set_r(uint8_t r) {
52 argb_ &= 0xFF00FFFF;
53 argb_ |= ((uint32_t)r) << 16;
54 }
55
56 /// Set green channel.
57 void set_g(uint8_t g) {
58 argb_ &= 0xFFFF00FF;
59 argb_ |= ((uint32_t)g) << 8;
60 }
61
62 /// Set blue channel.
63 void set_b(uint8_t g) {
64 argb_ &= 0xFFFFFF00;
65 argb_ |= ((uint32_t)g);
66 }
67
68 /// Return a copy with the specified alpha channel.
69 constexpr Color withA(uint8_t a) const {
70 return Color(a << 24 | (argb_ & 0x00FFFFFF));
71 }
72
73 /// Return a copy with the specified red channel.
74 constexpr Color withR(uint8_t r) const {
75 return Color(r << 16 | (argb_ & 0xFF00FFFF));
76 }
77
78 /// Return a copy with the specified green channel.
79 constexpr Color withG(uint8_t g) const {
80 return Color(g << 8 | (argb_ & 0xFFFF00FF));
81 }
82
83 /// Return a copy with the specified blue channel.
84 constexpr Color withB(uint8_t b) const {
85 return Color(b << 0 | (argb_ & 0xFFFFFF00));
86 }
87
88 /// Return true if the color is fully opaque (alpha = 255).
89 constexpr bool isOpaque() const { return a() == 0xFF; }
90
91 /// Return a fully opaque copy (alpha = 255).
92 constexpr Color toOpaque() { return Color(asArgb() | 0xFF000000); }
93
94 private:
95 uint32_t argb_;
96};
97
98/// Equality operator for colors.
99inline constexpr bool operator==(const Color &a, const Color &b) {
100 return a.asArgb() == b.asArgb();
101}
102
103/// Inequality operator for colors.
104inline constexpr bool operator!=(const Color &a, const Color &b) {
105 return a.asArgb() != b.asArgb();
106}
107
108/// Fill an array with a single color.
109inline void FillColor(Color *buf, uint32_t count, Color color) {
110 roo_io::PatternFill<sizeof(Color)>((roo::byte *)buf, count,
111 (const roo::byte *)(&color));
112}
113
114template <typename ColorMode>
115/// Truncate a color to a given color mode and back to ARGB.
116inline constexpr Color TruncateColor(Color color,
117 ColorMode mode = ColorMode()) {
118 return mode.toArgbColor(mode.fromArgbColor(color));
119}
120
121/// Return an opaque gray with r = g = b = level.
122inline constexpr Color Graylevel(uint8_t level) {
123 return Color(level, level, level);
124}
125
126namespace color {
127
128/// Transparent color (special behavior in fills).
129///
130/// When drawn in `FillMode::kExtents`, substituted by the current surface's
131/// background color. When drawn in `FillMode::kVisible`, pixels are not pushed
132/// to the device, leaving previous content intact.
133static constexpr auto Transparent = Color(0);
134
135/// Background color placeholder.
136///
137/// Substituted by the current surface background regardless of fill mode.
138/// Use this to force background pixels to be pushed even in
139/// `FillMode::kVisible`.
140static constexpr auto Background = Color(0x00FFFFFF);
141
142} // namespace color
143
144} // namespace roo_display
ARGB8888 color stored as a 32-bit unsigned integer.
Definition color.h:16
constexpr Color withR(uint8_t r) const
Return a copy with the specified red channel.
Definition color.h:74
constexpr Color withG(uint8_t g) const
Return a copy with the specified green channel.
Definition color.h:79
constexpr Color(uint8_t r, uint8_t g, uint8_t b)
Construct opaque color from RGB.
Definition color.h:22
void set_b(uint8_t g)
Set blue channel.
Definition color.h:63
void set_g(uint8_t g)
Set green channel.
Definition color.h:57
constexpr bool isOpaque() const
Return true if the color is fully opaque (alpha = 255).
Definition color.h:89
constexpr Color(uint8_t a, uint8_t r, uint8_t g, uint8_t b)
Construct ARGB color from components.
Definition color.h:26
void set_r(uint8_t r)
Set red channel.
Definition color.h:51
constexpr uint8_t a() const
Alpha channel.
Definition color.h:36
constexpr uint8_t b() const
Blue channel.
Definition color.h:42
constexpr uint8_t g() const
Green channel.
Definition color.h:40
constexpr uint8_t r() const
Red channel.
Definition color.h:38
constexpr Color(uint32_t argb)
Construct from a packed ARGB value.
Definition color.h:30
constexpr Color withA(uint8_t a) const
Return a copy with the specified alpha channel.
Definition color.h:69
constexpr Color withB(uint8_t b) const
Return a copy with the specified blue channel.
Definition color.h:84
constexpr Color toOpaque()
Return a fully opaque copy (alpha = 255).
Definition color.h:92
Color()
Construct transparent black.
Definition color.h:19
void set_a(uint8_t a)
Set alpha channel.
Definition color.h:45
constexpr uint32_t asArgb() const
Return packed ARGB value.
Definition color.h:33
Defines 140 opaque HTML named colors.
constexpr Color Graylevel(uint8_t level)
Return an opaque gray with r = g = b = level.
Definition color.h:122
constexpr bool operator==(const Color &a, const Color &b)
Equality operator for colors.
Definition color.h:99
constexpr bool operator!=(const Color &a, const Color &b)
Inequality operator for colors.
Definition color.h:104
void FillColor(Color *buf, uint32_t count, Color color)
Fill an array with a single color.
Definition color.h:109
constexpr Color TruncateColor(Color color, ColorMode mode=ColorMode())
Truncate a color to a given color mode and back to ARGB.
Definition color.h:116