25class Color :
public Eigen::Vector4f
28 using BaseType = Eigen::Vector4f;
30 static Color empty() {
return Color(0, 0, 0, 0); }
31 static Color zero() {
return Color(0, 0, 0, 0); }
32 static Color black() {
return Color(0, 0, 0); }
33 static Color white() {
return Color(1, 1, 1); }
35 static Color red() {
return Color(1, 0, 0); }
36 static Color green() {
return Color(0, 1, 0); }
37 static Color blue() {
return Color(0, 0, 1); }
39 static Color cyan() {
return Color(0, 1, 1); }
40 static Color yellow() {
return Color(1, 1, 0); }
41 static Color purple() {
return Color(1, 0, 1); }
43 Color(
const Eigen::Vector4f& color)
44 : Eigen::Vector4f(color)
47 Color(
const Eigen::Vector3f& rgb,
float alpha = 1.0f)
48 : Eigen::Vector4f(rgb.x(), rgb.y(), rgb.z(), alpha)
51 Color(
const float r,
const float g,
const float b,
const float a)
52 : Color(Eigen::Vector4f(r, g, b, a))
55 Color(
const float r,
const float g,
const float b)
56 : Color(r, g, b, 1.0f)
60 : Color(v, v, v, 1.0f)
67 template <
typename Derived>
68 Color(
const Eigen::MatrixBase<Derived>& p)
72 template <
typename Derived>
73 Color& operator=(
const Eigen::MatrixBase<Derived>& p)
75 this->Eigen::Vector4f::operator=(p);
79 float& r() {
return x(); }
80 float r()
const {
return x(); }
82 float& g() {
return y(); }
83 float g()
const {
return y(); }
85 float& b() {
return z(); }
86 float b()
const {
return z(); }
88 float& a() {
return coeffRef(3); }
89 float a()
const {
return coeff(3); }
91 Eigen::Vector3f to_vec3()
const {
return Eigen::Vector3f(x(), y(), z()); }
92 Eigen::Vector4f to_vec4()
const {
return Eigen::Vector4f(x(), y(), z(), a()); }
94 Color operator+(
const float v)
const {
return Color(r() + v, g() + v, b() + v, a()); }
95 Color operator-(
const float v)
const {
return Color(r() - v, g() - v, b() - v, a()); }
96 Color operator/(
const float v)
const {
return Color(r() / v, g() / v, b() / v, a()); }
97 Color operator*(
const float v)
const {
return Color(r() * v, g() * v, b() * v, a()); }
99 Color operator+(
const Color c)
const
101 return Color(r() + c.r(), g() + c.g(), b() + c.b(), std::max(a(), c.a()));
103 Color operator-(
const Color c)
const
105 return Color(r() - c.r(), g() - c.g(), b() - c.b(), std::max(a(), c.a()));
113 if (r() < 0.0f) r() = 0.0f;
114 if (r() > 1.0f) r() = 1.0f;
115 if (g() < 0.0f) g() = 0.0f;
116 if (g() > 1.0f) g() = 1.0f;
117 if (b() < 0.0f) b() = 0.0f;
118 if (b() > 1.0f) b() = 1.0f;
121 Color clamped()
const
124 std::max(std::min(r(), 1.0f), 0.0f),
125 std::max(std::min(g(), 1.0f), 0.0f),
126 std::max(std::min(b(), 1.0f), 0.0f),
127 std::max(std::min(a(), 1.0f), 0.0f));
130 inline bool is_white() {
return (r() + g() + b()) >= 3; }
132 inline bool is_black() {
return (r() == 0 && g() == 0 && b() == 0); }
134 float distance(
const Color c)
const
136 return std::abs(c.r() - r()) + std::abs(c.g() - g()) + std::abs(c.b() - b());
148 template <
typename URBG>
153 std::uniform_real_distribution<float> dist(0.f, 2.f * lagrange::internal::pi);
154 float tau = dist(urbg);
156 const float value =
static_cast<float>(lagrange::internal::pi) / 3.0f;
161 std::sin(tau + 0.0f * value) * width + center,
162 std::sin(tau + 2.0f * value) * width + center,
163 std::sin(tau + 4.0f * value) * width + center);
168 static const Color random(
int i)
170 float tau = (float)i;
171 float value = (float)lagrange::internal::pi / 3;
175 std::sin(tau + 0.0f * value) * width + center,
176 std::sin(tau + 2.0f * value) * width + center,
177 std::sin(tau + 4.0f * value) * width + center);
180 static Color integer_to_color(
int i)
182 int r = (i & 0x000000FF);
183 int g = (i & 0x0000FF00) >> 8;
184 int b = (i & 0x00FF0000) >> 16;
185 return Color(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f);
187 int to_integer()
const
189 int r = int(x() * 255.0f);
190 int g = int(y() * 255.0f) << 8;
191 int b = int(z() * 255.0f) << 16;