Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
colorspace.h
Go to the documentation of this file.
1
5#ifndef COLOR_SPACE_H
6#define COLOR_SPACE_H
7
8#include "utils.h"
9
10namespace lampda {
11namespace utils {
12
14namespace ColorSpace {
15
16class Base
17{
18public:
20 virtual ~Base() = default;
22 virtual COLOR get_rgb() const = 0;
23};
24
25class XYZ : public Base
26{
27public:
29 XYZ(const COLOR& c) { from_rgb(c); };
31 XYZ(double x, double y, double z) : x(x), y(y), z(z) {};
33 static XYZ get_white()
34 {
35 static const XYZ white(95.047, 100.000, 108.883);
36 return white;
37 }
38
40 COLOR get_rgb() const override;
42 void from_rgb(const COLOR& rgb);
43
44 double x;
45 double y;
46 double z;
47};
48
49class RGB : public Base
50{
51public:
53 RGB(uint8_t red, uint8_t green, uint8_t blue)
54 {
55 _color.red = red;
56 _color.green = green;
57 _color.blue = blue;
58 }
60 RGB(const uint32_t color) { _color.color = color; }
62 COLOR get_rgb() const override { return _color; }
63
64private:
65 COLOR _color;
66};
67
68// define some colors
69static const RGB RED(255, 0, 0);
70static const RGB GREEN(0, 255, 0);
71static const RGB BLUE(0, 0, 255);
72static const RGB WHITE(255, 255, 255);
73
74static const RGB BLACK(0, 0, 0);
75static const RGB FUSHIA(255, 0, 255);
76static const RGB TEAL(0, 255, 255);
77static const RGB YELLOW(200, 255, 0);
78static const RGB PINK(255, 20, 147);
79static const RGB TOMATO(255, 99, 71);
80static const RGB ORANGE(255, 140, 0);
81static const RGB DARK_ORANGE(255, 90, 0);
82static const RGB PURPLE(128, 0, 128);
83
84class HSV : public Base
85{
86public:
88 HSV(const COLOR& c) { from_rgb(c); };
90 HSV(double h, double s, double v) : h(h), s(s), v(v) {};
91
93 COLOR get_rgb() const override;
95 void from_rgb(const COLOR& rgb);
96
98 uint16_t get_scaled_hue() const { return static_cast<uint16_t>(h / 360.0 * UINT16_MAX); }
99
100 double h;
101 double s;
102 double v;
103};
104
105class LAB : public Base
106{
107public:
109 LAB(const COLOR& c) { from_rgb(c); };
111 LAB(const double l, const double a, const double b) : l(l), a(a), b(b) {};
112
114 COLOR get_rgb() const override;
116 void from_rgb(const COLOR& rgb);
117
118 double l;
119 double a;
120 double b;
121};
122
123class LCH : public Base
124{
125public:
127 LCH(const COLOR& c) { from_rgb(c); };
129 LCH(const double l, const double c, const double h) : l(l), c(c), h(h) {};
130
132 COLOR get_rgb() const override;
134 void from_rgb(const COLOR& rgb);
135
137 uint16_t get_scaled_hue() const { return static_cast<uint16_t>(h / 360.0 * UINT16_MAX); }
138
139 double l;
140 double c;
141 double h;
142};
143
144class OKLAB : public Base
145{
146public:
148 OKLAB(const COLOR& c) { from_rgb(c); };
150 OKLAB(const double l, const double a, const double b) : l(l), a(a), b(b) {};
151
153 COLOR get_rgb() const override;
154
156 void from_rgb(const COLOR& rgb);
157
158 double l;
159 double a;
160 double b;
161};
162
163class OKLCH : public Base
164{
165public:
167 OKLCH(const COLOR& c) { from_rgb(c); };
169 OKLCH(const double l, const double c, const double h) : l(l), c(c), h(h) {};
170
172 COLOR get_rgb() const override;
173
175 void from_rgb(const COLOR& rgb);
176
178 uint16_t get_scaled_hue() const { return static_cast<uint16_t>(h / 360.0 * UINT16_MAX); }
179
180 double l;
181 double c;
182 double h;
183};
184
185} // namespace ColorSpace
186} // namespace utils
187} // namespace lampda
188
189#endif
Definition: colorspace.h:17
virtual ~Base()=default
virtual destructor
virtual COLOR get_rgb() const =0
return the rgb of this color
Definition: colorspace.h:85
double v
value
Definition: colorspace.h:102
void from_rgb(const COLOR &rgb)
construct from an RGB representation
Definition: colorspace.cpp:91
double s
Saturation.
Definition: colorspace.h:101
HSV(double h, double s, double v)
Construct from values.
Definition: colorspace.h:90
uint16_t get_scaled_hue() const
get the hue scaled between 0 and UINT16_MAX
Definition: colorspace.h:98
COLOR get_rgb() const override
get the rgb form (for display)
Definition: colorspace.cpp:47
HSV(const COLOR &c)
Construct from RGB.
Definition: colorspace.h:88
double h
Hue.
Definition: colorspace.h:100
Definition: colorspace.h:106
LAB(const COLOR &c)
Construct from RGB.
Definition: colorspace.h:109
double a
Alpha.
Definition: colorspace.h:119
double b
Brightness.
Definition: colorspace.h:120
double l
Luminance.
Definition: colorspace.h:118
void from_rgb(const COLOR &rgb)
construct from an RGB representation
Definition: colorspace.cpp:149
LAB(const double l, const double a, const double b)
Construct from values.
Definition: colorspace.h:111
COLOR get_rgb() const override
get the rgb form (for display)
Definition: colorspace.cpp:129
Definition: colorspace.h:124
void from_rgb(const COLOR &rgb)
construct from an RGB representation
Definition: colorspace.cpp:176
COLOR get_rgb() const override
get the rgb form (for display)
Definition: colorspace.cpp:168
uint16_t get_scaled_hue() const
get the hue scaled between 0 and UINT16_MAX
Definition: colorspace.h:137
LCH(const COLOR &c)
Construct from RGB.
Definition: colorspace.h:127
LCH(const double l, const double c, const double h)
Construct from values.
Definition: colorspace.h:129
double h
Hue 0 - 360.
Definition: colorspace.h:141
double l
Luminance.
Definition: colorspace.h:139
double c
Chroma.
Definition: colorspace.h:140
Definition: colorspace.h:145
void from_rgb(const COLOR &rgb)
construct from an RGB representation
Definition: colorspace.cpp:221
double a
Alpha.
Definition: colorspace.h:159
double l
Luminance.
Definition: colorspace.h:158
OKLAB(const COLOR &c)
Construct from RGB.
Definition: colorspace.h:148
double b
Brightness.
Definition: colorspace.h:160
OKLAB(const double l, const double a, const double b)
Construct from values.
Definition: colorspace.h:150
COLOR get_rgb() const override
get the rgb form (for display)
Definition: colorspace.cpp:200
Definition: colorspace.h:164
double l
Luminance.
Definition: colorspace.h:180
COLOR get_rgb() const override
get the rgb form (for display)
Definition: colorspace.cpp:244
OKLCH(const COLOR &c)
Construct from RGB.
Definition: colorspace.h:167
double h
Hue 0 - 360.
Definition: colorspace.h:182
void from_rgb(const COLOR &rgb)
construct from an RGB representation
Definition: colorspace.cpp:252
uint16_t get_scaled_hue() const
get the hue scaled between 0 and UINT16_MAX
Definition: colorspace.h:178
OKLCH(const double l, const double c, const double h)
Construct from values.
Definition: colorspace.h:169
double c
Chroma.
Definition: colorspace.h:181
Definition: colorspace.h:50
COLOR get_rgb() const override
Get the RGB representation.
Definition: colorspace.h:62
RGB(uint8_t red, uint8_t green, uint8_t blue)
construct from values
Definition: colorspace.h:53
RGB(const uint32_t color)
construct from color
Definition: colorspace.h:60
Definition: colorspace.h:26
static XYZ get_white()
Return the white value.
Definition: colorspace.h:33
XYZ(double x, double y, double z)
Construct from values.
Definition: colorspace.h:31
double y
Color space Y.
Definition: colorspace.h:45
double x
Color space X.
Definition: colorspace.h:44
void from_rgb(const COLOR &rgb)
construct from an RGB representation
Definition: colorspace.cpp:32
COLOR get_rgb() const override
get the rgb form (for display)
Definition: colorspace.cpp:11
XYZ(const COLOR &c)
Construct from RGB.
Definition: colorspace.h:29
double z
Color space Z.
Definition: colorspace.h:46
Program scope.
Definition: control_fixed_modes.hpp:12
Use this to convert color to bytes.
Definition: utils.h:128
Define useful functions.