Skip to content

Commit

Permalink
geometry: Add basic Rgba class (RobotLocomotion#13441)
Browse files Browse the repository at this point in the history
* geometry: Add basic Rgba class
  • Loading branch information
EricCousineau-TRI authored Jun 1, 2020
1 parent 8ff2f5a commit 0ed8015
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
18 changes: 18 additions & 0 deletions geometry/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ drake_cc_package_library(
":internal_geometry",
":proximity_engine",
":proximity_properties",
":rgba",
":scene_graph",
":scene_graph_inspector",
":shape_specification",
Expand Down Expand Up @@ -275,6 +276,15 @@ drake_cc_library(
],
)

drake_cc_library(
name = "rgba",
srcs = ["rgba.cc"],
hdrs = ["rgba.h"],
deps = [
"//common",
],
)

# -----------------------------------------------------

filegroup(
Expand Down Expand Up @@ -433,4 +443,12 @@ drake_cc_googletest(
],
)

drake_cc_googletest(
name = "rgba_test",
deps = [
":rgba",
"//common/test_utilities",
],
)

add_lint_tests()
1 change: 1 addition & 0 deletions geometry/rgba.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "drake/geometry/rgba.h"
70 changes: 70 additions & 0 deletions geometry/rgba.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
64 changes: 64 additions & 0 deletions geometry/test/rgba_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "drake/geometry/rgba.h"

#include <gtest/gtest.h>

#include "drake/common/test_utilities/expect_throws_message.h"

namespace drake {
namespace geometry {

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.));
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(
"All 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),
std::runtime_error,
expected_message);
// Check for transaction integrity.
Rgba color = original;
DRAKE_EXPECT_THROWS_MESSAGE(
color.set(ri, gi, bi, ai),
std::runtime_error,
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);
}

} // namespace geometry
} // namespace drake

0 comments on commit 0ed8015

Please sign in to comment.