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
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
162 static Color black(float alpha = 1.) { return Color({0., 0., 0., (float) (alpha*255.)}); };
163
169 static Color white(float alpha = 1.) { return Color({255., 255., 255., (float) (alpha*255.)}); };
170
176 static Color green(float alpha = 1.) { return Color({144., 242., 0., (float) (alpha*255.)}); };
177
183 static Color blue(float alpha = 1.) { return Color({0., 98., 198., (float) (alpha*255.)}); };
184
190 static Color cyan(float alpha = 1.) { return Color({75., 207., 250., (float) (alpha*255.)}); };
191
197 static Color yellow(float alpha = 1.) { return Color({255., 211., 42., (float) (alpha*255.)}); };
198
204 static Color red(float alpha = 1.) { return Color({209., 59., 0., (float) (alpha*255.)}); };
205
211 static Color dark_gray(float alpha = 1.) { return Color({112., 112., 112., (float) (alpha*255.)}); };
212
218 static Color purple(float alpha = 1.) { return Color({154., 0., 170., (float) (alpha*255.)}); };
219
225 static Color dark_green(float alpha = 1.) { return Color({94., 158., 0., (float) (alpha*255.)}); };
226 };
227
228 template <std::size_t N>
229 static std::array<float, N> to_array(const std::initializer_list<float>& list) {
230 assert(list.size() == N);
231 std::array<float, N> arr;
232 std::copy(list.begin(), list.end(), arr.begin());
233 return arr;
234 }
235}
Model
Color model (RGB or HSV)
Definition codac2_Color.h:28
codac2::Vector vec() const
Converts the color to a codac2::Vector.
static Color green(float alpha=1.)
Green color.
Definition codac2_Color.h:176
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 purple(float alpha=1.)
Light gray color.
Definition codac2_Color.h:218
static Color black(float alpha=1.)
Black color.
Definition codac2_Color.h:162
static Color blue(float alpha=1.)
Blue color.
Definition codac2_Color.h:183
static Color red(float alpha=1.)
Red color.
Definition codac2_Color.h:204
static Color dark_green(float alpha=1.)
Dark green color.
Definition codac2_Color.h:225
Color rgb() const
Converts the color to RGB format.
static Color yellow(float alpha=1.)
Yellow color.
Definition codac2_Color.h:197
Color(const std::string &hex_str)
Constructor from a hex string.
Color hsv() const
Converts the color to HSV format.
static Color cyan(float alpha=1.)
Cyan color.
Definition codac2_Color.h:190
static Color white(float alpha=1.)
White color.
Definition codac2_Color.h:169
static Color dark_gray(float alpha=1.)
Dark gray color.
Definition codac2_Color.h:211
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.