libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdint>
12#include <promeki/core/string.h>
13
15
24class Color {
25 public:
27 Color() = default;
28
31 : _r(r), _g(g), _b(b), _a(a), _valid(true) {}
32
34 bool isValid() const { return _valid; }
35
37 uint8_t r() const { return _r; }
38
40 uint8_t g() const { return _g; }
41
43 uint8_t b() const { return _b; }
44
46 uint8_t a() const { return _a; }
47
49 void setR(uint8_t val) { _r = val; }
50
52 void setG(uint8_t val) { _g = val; }
53
55 void setB(uint8_t val) { _b = val; }
56
58 void setA(uint8_t val) { _a = val; }
59
68 static Color fromHex(const String &hex);
69
75 String toHex(bool includeAlpha = false) const;
76
83 Color lerp(const Color &other, double t) const;
84
90 Color inverted() const {
91 return Color(255 - _r, 255 - _g, 255 - _b, _a);
92 }
93
99 double luminance() const {
100 return (0.2126 * _r + 0.7152 * _g + 0.0722 * _b) / 255.0;
101 }
102
109 return luminance() > 0.5
110 ? Color(0, 0, 0, _a)
111 : Color(255, 255, 255, _a);
112 }
113
121
123 bool operator==(const Color &other) const {
124 return _r == other._r && _g == other._g &&
125 _b == other._b && _a == other._a;
126 }
127
129 bool operator!=(const Color &other) const {
130 return !(*this == other);
131 }
132
133 // Named color constants
134 static const Color Black;
135 static const Color White;
136 static const Color Red;
137 static const Color Green;
138 static const Color Blue;
139 static const Color Yellow;
140 static const Color Cyan;
141 static const Color Magenta;
142 static const Color DarkGray;
143 static const Color LightGray;
144 static const Color Orange;
145 static const Color Transparent;
146 static const Color Ignored;
147
148 private:
149 uint8_t _r = 0;
150 uint8_t _g = 0;
151 uint8_t _b = 0;
152 uint8_t _a = 0;
153 bool _valid = false;
154};
155
General-purpose RGBA color.
Definition color.h:24
bool operator!=(const Color &other) const
Inequality operator.
Definition color.h:129
void setA(uint8_t val)
Sets the alpha component.
Definition color.h:58
uint8_t b() const
Returns the blue component.
Definition color.h:43
static Color fromHex(const String &hex)
Creates a Color from a hex string.
Color contrastingBW() const
Returns black or white, whichever contrasts best.
Definition color.h:108
bool isValid() const
Returns true if this color was explicitly constructed.
Definition color.h:34
String toHex(bool includeAlpha=false) const
Converts this color to a hex string.
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Constructs a color from RGBA components.
Definition color.h:30
uint8_t r() const
Returns the red component.
Definition color.h:37
Color lerp(const Color &other, double t) const
Linearly interpolates between this color and another.
Color complementary() const
Returns the complementary color (hue rotated 180 degrees).
uint8_t g() const
Returns the green component.
Definition color.h:40
Color()=default
Default constructor. Creates an invalid (all-zero) color.
double luminance() const
Returns the perceptual luminance (0.0 to 1.0).
Definition color.h:99
void setR(uint8_t val)
Sets the red component.
Definition color.h:49
uint8_t a() const
Returns the alpha component.
Definition color.h:46
void setB(uint8_t val)
Sets the blue component.
Definition color.h:55
Color inverted() const
Returns the RGB-inverted color (255 - each channel).
Definition color.h:90
void setG(uint8_t val)
Sets the green component.
Definition color.h:52
bool operator==(const Color &other) const
Equality operator.
Definition color.h:123
Dynamic array container wrapping std::vector.
Definition list.h:40
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
TextStream & hex(TextStream &s)
Sets integer base to 16 (hexadecimal).
Definition textstream.h:409