23class Color :
public Eigen::Vector4f
26 using BaseType = Eigen::Vector4f;
28 static Color empty() {
return Color(0, 0, 0, 0); }
29 static Color zero() {
return Color(0, 0, 0, 0); }
30 static Color black() {
return Color(0, 0, 0); }
31 static Color white() {
return Color(1, 1, 1); }
34 static Color green() {
return Color(0, 1, 0); }
38 static Color yellow() {
return Color(1, 1, 0); }
39 static Color purple() {
return Color(1, 0, 1); }
41 Color(
const Eigen::Vector4f& color)
42 : Eigen::Vector4f(color)
45 Color(
const Eigen::Vector3f& rgb,
float alpha = 1.0f)
46 : Eigen::Vector4f(rgb.x(), rgb.y(), rgb.z(), alpha)
49 Color(
const float r,
const float g,
const float b,
const float a)
50 :
Color(Eigen::Vector4f(r, g, b, a))
53 Color(
const float r,
const float g,
const float b)
54 :
Color(r, g, b, 1.0f)
58 :
Color(v, v, v, 1.0f)
65 template <
typename Derived>
66 Color(
const Eigen::MatrixBase<Derived>& p)
70 template <
typename Derived>
71 Color& operator=(
const Eigen::MatrixBase<Derived>& p)
73 this->Eigen::Vector4f::operator=(p);
77 float& r() {
return x(); }
78 float r()
const {
return x(); }
80 float& g() {
return y(); }
81 float g()
const {
return y(); }
83 float& b() {
return z(); }
84 float b()
const {
return z(); }
86 float& a() {
return coeffRef(3); }
87 float a()
const {
return coeff(3); }
89 Eigen::Vector3f to_vec3()
const {
return Eigen::Vector3f(x(), y(), z()); }
90 Eigen::Vector4f to_vec4()
const {
return Eigen::Vector4f(x(), y(), z(), a()); }
92 Color operator+(
const float v)
const {
return Color(r() + v, g() + v, b() + v, a()); }
93 Color operator-(
const float v)
const {
return Color(r() - v, g() - v, b() - v, 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()); }
99 return Color(r() + c.r(), g() + c.g(), b() + c.b(), std::max(a(), c.a()));
103 return Color(r() - c.r(), g() - c.g(), b() - c.b(), std::max(a(), c.a()));
111 if (r() < 0.0f) r() = 0.0f;
112 if (r() > 1.0f) r() = 1.0f;
113 if (g() < 0.0f) g() = 0.0f;
114 if (g() > 1.0f) g() = 1.0f;
115 if (b() < 0.0f) b() = 0.0f;
116 if (b() > 1.0f) b() = 1.0f;
119 Color clamped()
const
122 std::max(std::min(r(), 1.0f), 0.0f),
123 std::max(std::min(g(), 1.0f), 0.0f),
124 std::max(std::min(b(), 1.0f), 0.0f),
125 std::max(std::min(a(), 1.0f), 0.0f));
128 inline bool is_white() {
return (r() + g() + b()) >= 3; }
130 inline bool is_black() {
return (r() == 0 && g() == 0 && b() == 0); }
132 float distance(
const Color c)
const
134 return std::abs(c.r() - r()) + std::abs(c.g() - g()) + std::abs(c.b() - b());
146 template <
typename URBG>
151 std::uniform_real_distribution<float> dist(0.f, 2.f * M_PI);
152 float tau = dist(urbg);
154 const float value =
static_cast<float>(M_PI) / 3.0f;
159 std::sin(tau + 0.0f * value) * width + center,
160 std::sin(tau + 2.0f * value) * width + center,
161 std::sin(tau + 4.0f * value) * width + center);
166 static const Color random(
int i)
168 float tau = (float)i;
169 float value = (float)M_PI / 3;
173 std::sin(tau + 0.0f * value) * width + center,
174 std::sin(tau + 2.0f * value) * width + center,
175 std::sin(tau + 4.0f * value) * width + center);
178 static Color integer_to_color(
int i)
180 int r = (i & 0x000000FF);
181 int g = (i & 0x0000FF00) >> 8;
182 int b = (i & 0x00FF0000) >> 16;
183 return Color(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f);
185 int to_integer()
const
187 int r = int(x() * 255.0f);
188 int g = int(y() * 255.0f) << 8;
189 int b = int(z() * 255.0f) << 16;
void clamp()
Clamps the color.
Definition: Color.h:109
static const Color random_from(URBG &&urbg)
Get a random color.
Definition: Color.h:147
Lagrange UI Viewer and mini 3D engine.
Definition: AcceleratedPicking.h:22
Main namespace for Lagrange.
Definition: AABBIGL.h:30