forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
geometry: Add basic Rgba class (RobotLocomotion#13441)
* geometry: Add basic Rgba class
- Loading branch information
1 parent
8ff2f5a
commit 0ed8015
Showing
4 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "drake/geometry/rgba.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |