ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Color.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <algorithm>
11 #include <cmath>
12 
13 namespace cloudViewer {
14 namespace visualization {
15 namespace gui {
16 
17 Color::Color() : rgba_{0.0f, 0.0f, 0.0f, 1.0f} {}
18 
19 Color::Color(float r, float g, float b, float a /*= 1.0*/)
20  : rgba_{r, g, b, a} {}
21 
22 Color::Color(const Eigen::Vector3f& rgb)
23  : rgba_{rgb.x(), rgb.y(), rgb.z(), 1.0f} {}
24 
25 bool Color::operator==(const Color& rhs) const {
26  return (this->rgba_[0] == rhs.rgba_[0] && this->rgba_[1] == rhs.rgba_[1] &&
27  this->rgba_[2] == rhs.rgba_[2] && this->rgba_[3] == rhs.rgba_[3]);
28 }
29 
30 bool Color::operator!=(const Color& rhs) const {
31  return !this->operator==(rhs);
32 }
33 
34 float Color::GetRed() const { return rgba_[0]; }
35 float Color::GetGreen() const { return rgba_[1]; }
36 float Color::GetBlue() const { return rgba_[2]; }
37 float Color::GetAlpha() const { return rgba_[3]; }
38 
39 void Color::SetColor(const float r,
40  const float g,
41  const float b,
42  const float a /*= 1.0 */) {
43  rgba_[0] = r;
44  rgba_[1] = g;
45  rgba_[2] = b;
46  rgba_[3] = a;
47 }
48 
49 const float* Color::GetPointer() const { return rgba_; }
50 float* Color::GetMutablePointer() { return rgba_; }
51 
52 Color Color::Lightened(float amount) {
53  amount = std::max(0.0f, std::min(1.0f, amount));
54  return Color((1.0f - amount) * GetRed() + amount * 1.0f,
55  (1.0f - amount) * GetGreen() + amount * 1.0f,
56  (1.0f - amount) * GetBlue() + amount * 1.0f, GetAlpha());
57 }
58 
59 unsigned int Color::ToABGR32() const {
60  unsigned int a = (unsigned int)std::round(GetAlpha() * 255.0f);
61  unsigned int b = (unsigned int)std::round(GetBlue() * 255.0f);
62  unsigned int g = (unsigned int)std::round(GetGreen() * 255.0f);
63  unsigned int r = (unsigned int)std::round(GetRed() * 255.0f);
64  return ((a << 24) | (b << 16) | (g << 8) | r);
65 }
66 
67 } // namespace gui
68 } // namespace visualization
69 } // namespace cloudViewer
unsigned int ToABGR32() const
Definition: Color.cpp:59
bool operator==(const Color &rhs) const
Definition: Color.cpp:25
const float * GetPointer() const
Definition: Color.cpp:49
void SetColor(float r, float g, float b, float a=1.0)
Definition: Color.cpp:39
bool operator!=(const Color &rhs) const
Definition: Color.cpp:30
Color Lightened(float amount)
Definition: Color.cpp:52
int min(int a, int b)
Definition: cutil_math.h:53
int max(int a, int b)
Definition: cutil_math.h:48
Generic file read and write utility for python interface.