-
Notifications
You must be signed in to change notification settings - Fork 68
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
Color [] changed to call by reference #579
base: gz-math7
Are you sure you want to change the base?
Changes from 7 commits
d5202b1
d550a33
59a0f14
8972884
719dfe2
a81c769
c7b287d
39a5c15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"name": "Linux", | ||
"includePath": [ | ||
"${workspaceFolder}/**" | ||
], | ||
"defines": [], | ||
"compilerPath": "/usr/bin/gcc", | ||
"cStandard": "c17", | ||
"cppStandard": "gnu++17", | ||
"intelliSenseMode": "linux-gcc-x64" | ||
} | ||
], | ||
"version": 4 | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this file. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"files.associations": { | ||
"ostream": "cpp", | ||
"stdexcept": "cpp", | ||
"cmath": "cpp" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,14 +159,14 @@ namespace gz | |
/// 3=alpha) | ||
/// \return r, g, b, or a when _index is 0, 1, 2 or 3. A NAN_F value is | ||
/// returned if the _index is invalid | ||
public: float operator[](const unsigned int _index); | ||
public: float& operator[](const unsigned int _index); | ||
|
||
/// \brief Array index operator, const version | ||
/// \param[in] _index Color component index(0=red, 1=green, 2=blue, | ||
/// 3=alpha) | ||
/// \return r, g, b, or a when _index is 0, 1, 2 or 3. A NAN_F value is | ||
/// returned if the _index is invalid | ||
public: float operator[](const unsigned int _index) const; | ||
public: const float& operator[](const unsigned int _index) const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to return a POD type by value, so please revert this change. |
||
|
||
/// \brief Get as uint32 RGBA packed value | ||
/// \return the color | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
*/ | ||
#include <cmath> | ||
#include <algorithm> | ||
#include<iostream> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to include this ? |
||
|
||
#include "gz/math/Color.hh" | ||
|
||
|
@@ -187,13 +188,28 @@ void Color::SetFromYUV(const float _y, const float _u, const float _v) | |
} | ||
|
||
////////////////////////////////////////////////// | ||
float Color::operator[](const unsigned int _index) | ||
float& Color::operator[](const unsigned int _index) | ||
{ | ||
return (*static_cast<const Color *>(this))[_index]; | ||
switch (_index) | ||
{ | ||
case 0: | ||
return this->r; | ||
case 1: | ||
return this->g; | ||
case 2: | ||
return this->b; | ||
case 3: | ||
return this->a; | ||
default: | ||
break; | ||
} | ||
|
||
std::cerr << "Trying to read index " << _index << " of Color" << std::endl; | ||
throw std::runtime_error("Index Error: Color index out of range"); | ||
Avisheet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
////////////////////////////////////////////////// | ||
float Color::operator[](const unsigned int _index) const | ||
const float& Color::operator[](const unsigned int _index) const | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert this per my comment in the header. |
||
{ | ||
switch (_index) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -190,7 +190,7 @@ TEST(Color, Color) | |
EXPECT_FLOAT_EQ(0.5f, clr[1]); | ||
EXPECT_FLOAT_EQ(1.0f, clr[2]); | ||
EXPECT_FLOAT_EQ(1.0f, clr[3]); | ||
EXPECT_TRUE(std::isnan(clr[4])); | ||
// EXPECT_TRUE(std::isnan(clr[4])); | ||
|
||
clr.R() = 0.1f; | ||
clr.G() = 0.2f; | ||
|
@@ -433,3 +433,32 @@ TEST(Color, HSV) | |
EXPECT_NEAR(clr.B(), 0.3f, 1e-3); | ||
EXPECT_NEAR(clr.A(), 1.0, 1e-3); | ||
} | ||
|
||
TEST(Color, OperatorIndex){ | ||
math::Color clr; | ||
clr[0] = 0.1f; | ||
clr[1] = 0.2f; | ||
clr[2] = 0.3f; | ||
clr[3] = 0.4f; | ||
|
||
EXPECT_FLOAT_EQ(clr[0], 0.1f); | ||
EXPECT_FLOAT_EQ(clr[1], 0.2f); | ||
EXPECT_FLOAT_EQ(clr[2], 0.3f); | ||
EXPECT_FLOAT_EQ(clr[3], 0.4f); | ||
|
||
// const math::Color constClr = clr; | ||
|
||
// // this tests _that_ the expected exception is thrown | ||
EXPECT_THROW({ | ||
try | ||
{ | ||
clr[4] = 0.1f; | ||
} | ||
catch( const std::runtime_error& e ) | ||
{ | ||
// and this tests that it has the correct message | ||
EXPECT_STREQ( "Index Error: Color index out of range", e.what() ); | ||
throw; | ||
} | ||
}, std::runtime_error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be sufficient to check that |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,7 +176,7 @@ def test_color(self): | |
self.assertAlmostEqual(0.5, clr[1]) | ||
self.assertAlmostEqual(1.0, clr[2]) | ||
self.assertAlmostEqual(1.0, clr[3]) | ||
self.assertTrue(math.isnan(clr[4])) | ||
# self.assertTrue(math.isnan(clr[4])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert |
||
|
||
clr.set(0.1, 0.2, 0.3, 0.4) | ||
clr = clr + 0.2 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this file.