diff --git a/src/Color.i b/src/Color.i index 946b6938f..f044e2430 100644 --- a/src/Color.i +++ b/src/Color.i @@ -23,6 +23,8 @@ #include %} +%include "std_string.i" + namespace ignition { namespace math @@ -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(); + } + } } } diff --git a/src/Color_TEST.py b/src/Color_TEST.py index 15a241a26..79b6c7d9d 100644 --- a/src/Color_TEST.py +++ b/src/Color_TEST.py @@ -13,6 +13,7 @@ # limitations under the License. import unittest +import math from ignition.math import Color from ignition.math import Vector3f @@ -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)) @@ -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()