-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
rgba.h
70 lines (55 loc) · 1.53 KB
/
rgba.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#pragma once
#include <stdexcept>
#include <fmt/format.h>
#include "drake/common/drake_copyable.h"
namespace drake {
namespace geometry {
/** Defines RGBA (red, green, blue, alpha) values on the range [0, 1]. */
class Rgba {
public:
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(Rgba);
/** Constructs with given (r, g, b, a) values.
@pre All values are within the range of [0, 1]. */
Rgba(double r, double g, double b, double a = 1.) {
set(r, g, b, a);
}
/** Red. */
double r() const { return r_; }
/** Green. */
double g() const { return g_; }
/** Blue. */
double b() const { return b_; }
/** Alpha. */
double a() const { return a_; }
/** Sets (r, g, b, a) values.
@pre All values are within the range of [0, 1]. The values are not updated
if this precondition is not met. */
void set(double r, double g, double b, double a = 1.) {
if ((r < 0 || r > 1) ||
(g < 0 || g > 1) ||
(b < 0 || b > 1) ||
(a < 0 || a > 1)) {
throw std::runtime_error(fmt::format(
"All values must be within the range [0, 1]. Values provided: "
"(r={}, g={}, b={}, a={})", r, g, b, a));
}
r_ = r;
g_ = g;
b_ = b;
a_ = a;
}
bool operator==(const Rgba& other) const {
return
r_ == other.r_ && g_ == other.g_ && b_ == other.b_ && a_ == other.a_;
}
bool operator!=(const Rgba& other) const {
return !(*this == other);
}
private:
double r_{};
double g_{};
double b_{};
double a_{};
};
} // namespace geometry
} // namespace drake