Skip to content

Commit

Permalink
Adds python support for operator[] and operator<< to the Color class.
Browse files Browse the repository at this point in the history
Signed-off-by: LolaSegura <[email protected]>
  • Loading branch information
LolaSegura committed Aug 12, 2021
1 parent 895eb1e commit 71a152d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Color.i
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <ignition/math/config.hh>
%}

%include "std_string.i"

namespace ignition
{
namespace math
Expand Down Expand Up @@ -90,5 +92,20 @@ namespace ignition
private: float b = 0;
private: float a = 1;
};

%extend Color{
float __getitem__(const unsigned int i)
{
return (*$self)[i];
}
}

%extend Color {
std::string __str__() const {
std::ostringstream out;
out << *$self;
return out.str();
}
}
}
}
12 changes: 12 additions & 0 deletions src/Color_TEST.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import unittest
import math
from ignition.math import Color
from ignition.math import Vector3f

Expand Down Expand Up @@ -170,6 +171,13 @@ def test_color(self):
self.assertAlmostEqual(1.0, clr.B())
self.assertAlmostEqual(1.0, clr.A())

clr.SetFromHSV(300, 0.5, 1.0)
self.assertAlmostEqual(1.0, clr[0])
self.assertAlmostEqual(0.5, clr[1])
self.assertAlmostEqual(1.0, clr[2])
self.assertAlmostEqual(1.0, clr[3])
self.assertTrue(math.isnan(clr[4]))

clr.Set(0.1, 0.2, 0.3, 0.4)
clr = clr + 0.2
self.assertTrue(clr == Color(0.3, 0.4, 0.5, 0.6))
Expand Down Expand Up @@ -286,6 +294,10 @@ def test_const_set(self):

self.assertTrue(clr2 != clr)

def test_stream_out(self):
c = Color(0.1, 0.2, 0.3, 0.5)
self.assertAlmostEqual(str(c), "0.1 0.2 0.3 0.5")

def test_HSV(self):
clr = Color()
hsv = clr.HSV()
Expand Down

0 comments on commit 71a152d

Please sign in to comment.