codac 1.5.6
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
113 codac2::Vector vec() const;
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() { return Color({255,255,255, 0.}); };
156
157 #define DEFINE_COLOR(NAME, R, G, B) \
158 static Color NAME(float alpha = 1.) { \
159 return Color({(float)(R), (float)(G), (float)(B), (float)(alpha * 255.)}); \
160 }
161
162 // Predefined colors
163
164 DEFINE_COLOR(black, 0, 0, 0)
165 DEFINE_COLOR(white, 255, 255, 255)
166
167 DEFINE_COLOR(light_gray, 217, 217, 217)
168 DEFINE_COLOR(gray, 180, 180, 180)
169 DEFINE_COLOR(dark_gray, 112, 112, 112)
170
171 DEFINE_COLOR(light_green, 184, 233, 118)
172 DEFINE_COLOR(green, 144, 242, 0)
173 DEFINE_COLOR(dark_green, 94, 158, 0)
174
175 DEFINE_COLOR(light_blue, 75, 207, 250)
176 DEFINE_COLOR(blue, 45, 152, 218)
177 DEFINE_COLOR(dark_blue, 34, 112, 147)
178
179 DEFINE_COLOR(light_cyan, 129, 236, 236)
180 DEFINE_COLOR(cyan, 109, 200, 200)
181 DEFINE_COLOR(dark_cyan, 82, 151, 151)
182
183 DEFINE_COLOR(light_yellow, 255, 250, 101)
184 DEFINE_COLOR(yellow, 255, 211, 42)
185 DEFINE_COLOR(dark_yellow, 225, 177, 44)
186
187 DEFINE_COLOR(light_orange, 253, 150, 68)
188 DEFINE_COLOR(orange, 255, 159, 26)
189 DEFINE_COLOR(dark_orange, 214, 134, 22)
190
191 DEFINE_COLOR(light_red, 231, 127, 103)
192 DEFINE_COLOR(red, 209, 59, 0)
193 DEFINE_COLOR(dark_red, 179, 57, 57)
194
195 DEFINE_COLOR(light_brown, 208, 151, 71)
196 DEFINE_COLOR(brown, 151, 109, 52)
197 DEFINE_COLOR(dark_brown, 99, 72, 34)
198
199 DEFINE_COLOR(light_purple, 205, 132, 241)
200 DEFINE_COLOR(purple, 154, 0, 170)
201 DEFINE_COLOR(dark_purple, 108, 0, 119)
202
203 DEFINE_COLOR(light_pink, 253, 167, 223)
204 DEFINE_COLOR(pink, 243, 104, 224)
205 DEFINE_COLOR(dark_pink, 185, 79, 171)
206 };
207}
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.
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.