-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
rgba_test.cc
109 lines (94 loc) · 3.22 KB
/
rgba_test.cc
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "drake/geometry/rgba.h"
#include <gtest/gtest.h>
#include "drake/common/test_utilities/expect_throws_message.h"
#include "drake/common/yaml/yaml_io.h"
namespace drake {
namespace geometry {
namespace {
GTEST_TEST(RgbaTest, Default) {
Rgba defaulted;
Rgba opaque_white(1, 1, 1, 1);
EXPECT_EQ(defaulted, opaque_white);
}
GTEST_TEST(RgbaTest, Basic) {
const double r = 0.75;
const double g = 0.5;
const double b = 0.25;
const double a = 1.;
Rgba color(r, g, b);
EXPECT_EQ(color.r(), r);
EXPECT_EQ(color.g(), g);
EXPECT_EQ(color.b(), b);
EXPECT_EQ(color.a(), a);
EXPECT_TRUE(color == Rgba(r, g, b, a));
EXPECT_TRUE(color != Rgba(r, g, b, 0.));
const double kEps = 1e-8;
const Rgba color_delta(color.r() + kEps, color.g() - kEps, color.b() + kEps,
color.a() - kEps);
EXPECT_TRUE(color.AlmostEqual(color_delta, 1.001 * kEps));
EXPECT_FALSE(color.AlmostEqual(color_delta, 0.999 * kEps));
color.set(1., 1., 1., 0.);
EXPECT_EQ(color, Rgba(1., 1., 1., 0.));
}
GTEST_TEST(RgbaTest, Errors) {
const Rgba original(0.1, 0.2, 0.3, 0.4);
auto expect_error = [original](double ri, double gi, double bi, double ai) {
const std::string expected_message = fmt::format(
"Rgba values must be within the range \\[0, 1\\]. Values provided: "
"\\(r={}, g={}, b={}, a={}\\)", ri, gi, bi, ai);
DRAKE_EXPECT_THROWS_MESSAGE(
Rgba(ri, gi, bi, ai),
expected_message);
// Check for transaction integrity.
Rgba color = original;
DRAKE_EXPECT_THROWS_MESSAGE(
color.set(ri, gi, bi, ai),
expected_message);
EXPECT_EQ(color, original);
};
const double bad_low = -0.1;
const double bad_high = 1.1;
const double r = 0.75;
const double g = 0.5;
const double b = 0.25;
const double a = 1.;
expect_error(bad_low, g, b, a);
expect_error(bad_high, g, b, a);
expect_error(r, bad_low, b, a);
expect_error(r, bad_high, b, a);
expect_error(r, g, bad_low, a);
expect_error(r, g, bad_high, a);
expect_error(r, g, b, bad_low);
expect_error(r, g, b, bad_high);
}
/** Confirm that this can be serialized appropriately. */
GTEST_TEST(RgbaTest, Serialization) {
{
// Serializing.
const Rgba rgba(0.75, 0.25, 1.0, 0.5);
const std::string y = yaml::SaveYamlString(rgba);
EXPECT_EQ(y, "rgba: [0.75, 0.25, 1.0, 0.5]\n");
}
{
// Deserialize: full specification.
const Rgba rgba = yaml::LoadYamlString<Rgba>("rgba: [0.1, 0.2, 0.3, 0.4]");
EXPECT_EQ(rgba, Rgba(0.1, 0.2, 0.3, 0.4));
}
{
// Deserialize: alpha isn't necessary; alpha = 1 is provided.
const Rgba rgba = yaml::LoadYamlString<Rgba>("rgba: [0.1, 0.2, 0.3]");
EXPECT_EQ(rgba, Rgba(0.1, 0.2, 0.3, 1));
}
// Wrong size array throws.
DRAKE_EXPECT_THROWS_MESSAGE(yaml::LoadYamlString<Rgba>("rgba: [1, 1]"),
"Rgba must contain either 3 or 4.+");
DRAKE_EXPECT_THROWS_MESSAGE(
yaml::LoadYamlString<Rgba>("rgba: [1, 1, 1, 1, 1]"),
"Rgba must contain either 3 or 4.+");
// Values out of range likewise throw.
DRAKE_EXPECT_THROWS_MESSAGE(yaml::LoadYamlString<Rgba>("rgba: [1, 1, 2]"),
".*must be within the range.+");
}
} // namespace
} // namespace geometry
} // namespace drake