codac 2.0.0
Loading...
Searching...
No Matches
codac2_Color.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <string>
13#include <iomanip>
14#include <math.h>
15
16#include"codac2_assert.h"
17#include"codac2_Vector.h"
18
19namespace codac2
20{
21
28 enum Model { RGB, HSV };
29
43 struct Color : public std::array<float,4>
44 {
45 protected:
46 Model m;
47
48 public:
49
50 // Constructors
51
57 explicit Color();
58
65 explicit Color(const std::array<float,3>& xyz, Model m_ = Model::RGB);
66
73 explicit Color(const std::array<float,4>& xyza, Model m_ = Model::RGB);
74
81 explicit Color(const std::initializer_list<float> xyza, Model m_ = Model::RGB);
82
88 explicit Color(const std::string& hex_str);
89
95 const Model& model() const { return m; }
96
97
98 // other formats
99
105 std::string hex_str() const;
106
107
114
115 // Conversions
116
122 Color rgb() const;
123
129 Color hsv() const;
130
131 // Overload flux operator
132
141 friend std::ostream& operator<<(std::ostream& os, const Color& c)
142 {
143 if (c.m == Model::RGB)
144 os << "RGB Color (" << c[0] << "," << c[1] << "," << c[2] << "," << c[3] << ")";
145 else if (c.m == Model::HSV)
146 os << "HSV Color (" << c[0] << "," << c[1] << "," << c[2] << "," << c[3] << ")";
147 return os;
148 }
149
150 // Predefined colors
151
155 static Color none()
156 {
157 return Color({255,255,255, 0.});
158 };
159
163 static Color random()
164 {
165 return Color({(float)Interval(0,360).rand(),100,100}, Model::HSV);
166 };
167
168 #define DEFINE_COLOR(NAME, R, G, B) \
169 static Color NAME(float alpha = 1.) { \
170 return Color({(float)(R), (float)(G), (float)(B), (float)(alpha * 255.)}); \
171 }
172
173 #define DEFINE_COLOR(NAME, R, G, B) \
174 static Color NAME(float alpha = 1.) { \
175 return Color({(float)(R), (float)(G), (float)(B), (float)(alpha * 255.)}); \
176 }
177
178 // Predefined colors
179
180 DEFINE_COLOR(black, 0, 0, 0)
181 DEFINE_COLOR(white, 255, 255, 255)
182
183 DEFINE_COLOR(light_gray, 217, 217, 217)
184 DEFINE_COLOR(gray, 180, 180, 180)
185 DEFINE_COLOR(dark_gray, 112, 112, 112)
186
187 DEFINE_COLOR(light_green, 184, 233, 118)
188 DEFINE_COLOR(green, 144, 242, 0)
189 DEFINE_COLOR(dark_green, 94, 158, 0)
190
191 DEFINE_COLOR(light_blue, 75, 207, 250)
192 DEFINE_COLOR(blue, 45, 152, 218)
193 DEFINE_COLOR(dark_blue, 34, 112, 147)
194
195 DEFINE_COLOR(light_cyan, 129, 236, 236)
196 DEFINE_COLOR(cyan, 109, 200, 200)
197 DEFINE_COLOR(dark_cyan, 82, 151, 151)
198
199 DEFINE_COLOR(light_yellow, 255, 250, 101)
200 DEFINE_COLOR(yellow, 255, 211, 42)
201 DEFINE_COLOR(dark_yellow, 225, 177, 44)
202
203 DEFINE_COLOR(light_orange, 253, 150, 68)
204 DEFINE_COLOR(orange, 255, 159, 26)
205 DEFINE_COLOR(dark_orange, 214, 134, 22)
206
207 DEFINE_COLOR(light_red, 231, 127, 103)
208 DEFINE_COLOR(red, 209, 59, 0)
209 DEFINE_COLOR(dark_red, 179, 57, 57)
210
211 DEFINE_COLOR(light_brown, 208, 151, 71)
212 DEFINE_COLOR(brown, 151, 109, 52)
213 DEFINE_COLOR(dark_brown, 99, 72, 34)
214
215 DEFINE_COLOR(light_purple, 205, 132, 241)
216 DEFINE_COLOR(purple, 154, 0, 170)
217 DEFINE_COLOR(dark_purple, 108, 0, 119)
218
219 DEFINE_COLOR(light_pink, 253, 167, 223)
220 DEFINE_COLOR(pink, 243, 104, 224)
221 DEFINE_COLOR(dark_pink, 185, 79, 171)
222 };
223}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:49
double rand() const
Returns a random value inside the interval.
Definition codac2_Interval_impl.h:137
Definition codac2_OctaSym.h:21
Eigen::Matrix< double,-1, 1 > Vector
Alias for a dynamically-sized column vector of doubles.
Definition codac2_Vector.h:24
Model
Color model (RGB or HSV)
Definition codac2_Color.h:28
codac2::Vector vec() const
Converts the color to a codac2::Vector.
Color(const std::initializer_list< float > xyza, Model m_=Model::RGB)
Constructor from an initializer list of floats.
Color(const std::array< float, 3 > &xyz, Model m_=Model::RGB)
Constructor from an array of 3 floats.
Color(const std::array< float, 4 > &xyza, Model m_=Model::RGB)
Constructor from an array of 4 floats.
static Color random()
Random color (full opacity)
Definition codac2_Color.h:163
Color rgb() const
Converts the color to RGB format.
Color(const std::string &hex_str)
Constructor from a hex string.
Color hsv() const
Converts the color to HSV format.
static Color none()
Empty color (transparent white)
Definition codac2_Color.h:155
std::string hex_str() const
Converts the color to a hex string in html format.
friend std::ostream & operator<<(std::ostream &os, const Color &c)
Overload of the << operator for the Color class.
Definition codac2_Color.h:141
const Model & model() const
Getter for the color model.
Definition codac2_Color.h:95
Color()
Default constructor.