Skip to content

Commit

Permalink
Make alpha optional when parsing a Color from an input stream (#106)
Browse files Browse the repository at this point in the history
* Make alpha optional when parsing a Color from an input stream

Signed-off-by: Addisu Z. Taddese <[email protected]>

Co-authored-by: Steven Peters <[email protected]>
  • Loading branch information
azeey and scpeters authored Apr 28, 2020
1 parent 53efe3b commit c272642
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
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

0 comments on commit c272642

Please sign in to comment.