Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make alpha optional when parsing a Color from an input stream #106

Merged
merged 4 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Ignition Math 6.x.x

1. Make alpha optional when parsing a Color from an input stream.
* [Pull request 106](https://github.com/ignitionrobotics/ign-math/pull/106)

1. Added a Gauss-Markov Process class.
* [BitBucket pull request 342](https://osrf-migration.github.io/ignition-gh-pages/#!/ignitionrobotics/ign-math/pull-requests/342)

Expand Down
18 changes: 16 additions & 2 deletions include/ignition/math/Color.hh
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,27 @@ namespace ignition
}

/// \brief Stream insertion operator
/// \param[in] _in the input stream
/// \param[in] _in the input stream. If the input stream does not include
/// an alpha value, a default alpha value of 1.0 will be used.
/// \param[in] _pt
public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
{
// Skip white spaces
_in.setf(std::ios_base::skipws);
_in >> _pt.r >> _pt.g >> _pt.b >> _pt.a;
_in >> _pt.r >> _pt.g >> _pt.b;
// Since alpha is optional, check if it's there before parsing
while (!_in.eof() && std::isspace(_in.peek()))
{
_in.get();
}
if (!_in.eof())
{
_in >> _pt.a;
}
else
{
_pt.a = 1.0;
}
return _in;
}

Expand Down
38 changes: 38 additions & 0 deletions src/Color_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,44 @@ TEST(Color, OperatorStreamOut)
EXPECT_EQ(stream.str(), "0.1 0.2 0.3 0.5");
}

/////////////////////////////////////////////////
TEST(Color, OperatorStreamIn)
{
math::Color c(0.1f, 0.2f, 0.3f, 0.5f);
math::Color test;
std::stringstream ss("0.1 0.2 0.3 0.5");
ss >> test;
EXPECT_EQ(c, test);
}

/////////////////////////////////////////////////
TEST(Color, OperatorStreamInWithoutAlpha)
{
math::Color c(0.1f, 0.2f, 0.3f, 1.0f);
{
math::Color test;
std::stringstream ss("0.1 0.2 0.3");
ss.exceptions(std::stringstream::failbit);
EXPECT_NO_THROW(ss >> test);
EXPECT_EQ(c, test);
}
{
math::Color test;
std::stringstream ss("0.1 0.2 \t0.3 \t");
ss.exceptions(std::stringstream::failbit);
EXPECT_NO_THROW(ss >> test);
EXPECT_EQ(c, test);
}

{
math::Color test(0.5f, 0.6f, 0.7f, 0.8f);
std::stringstream ss("0.1 0.2 \t0.3 \t");
ss.exceptions(std::stringstream::failbit);
EXPECT_NO_THROW(ss >> test);
EXPECT_EQ(c, test);
}
}

/////////////////////////////////////////////////
TEST(Color, HSV)
{
Expand Down