Skip to content

Commit 5a3b70b

Browse files
committed
[Sofa.Types] Add RGBAColor binding & tests
1 parent 6bd294b commit 5a3b70b

File tree

4 files changed

+126
-10
lines changed

4 files changed

+126
-10
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/******************************************************************************
2+
* SOFA, Simulation Open-Framework Architecture *
3+
* (c) 2021 INRIA, USTL, UJF, CNRS, MGH *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify it *
6+
* under the terms of the GNU Lesser General Public License as published by *
7+
* the Free Software Foundation; either version 2.1 of the License, or (at *
8+
* your option) any later version. *
9+
* *
10+
* This program is distributed in the hope that it will be useful, but WITHOUT *
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
13+
* for more details. *
14+
* *
15+
* You should have received a copy of the GNU Lesser General Public License *
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17+
*******************************************************************************
18+
* Contact information: [email protected] *
19+
******************************************************************************/
20+
21+
#include <SofaPython3/Sofa/Types/Binding_BoundingBox.h>
22+
23+
#include <sofa/core/objectmodel/BaseData.h>
24+
#include <sofa/core/objectmodel/Data.h>
25+
#include <sofa/type/BoundingBox.h>
26+
#include <SofaPython3/PythonFactory.h>
27+
#include <pybind11/stl.h>
28+
#include <pybind11/operators.h>
29+
using sofa::type::RGBAColor;
30+
31+
namespace py { using namespace pybind11; }
32+
33+
namespace sofapython3 {
34+
35+
void moduleAddRGBAColor(py::module& m)
36+
{
37+
py::class_<sofa::type::RGBAColor> c(m, "RGBAColor");
38+
c.doc() = R"doc(A color)doc";
39+
40+
c.def(py::init<>()); // empty ctor
41+
c.def(py::init<const RGBAColor &>()); // copy ctor
42+
c.def(py::init([](double r, double g, double b, double a) {
43+
return std::make_unique<sofa::type::RGBAColor>( r,g,b,a );
44+
}));
45+
46+
c.def(py::init([](std::array<double,4>& v) {
47+
return std::make_unique<sofa::type::RGBAColor>( v[0], v[1], v[2], v[3] );
48+
}));
49+
50+
c.def("r", [](const RGBAColor& color) { return color.r(); });
51+
c.def("g", [](const RGBAColor& color) { return color.g(); });
52+
c.def("b", [](const RGBAColor& color) { return color.b(); });
53+
c.def("a", [](const RGBAColor& color) { return color.a(); });
54+
55+
c.def("lighten", [](const RGBAColor& color, const SReal factor){
56+
return RGBAColor::lighten(color, factor);
57+
});
58+
59+
c.def("__str__", [](const RGBAColor& color){
60+
std::stringstream tmp;
61+
tmp << "RGBAColor(" << color.r() << "," << color.g() << "," << color.b() << "," << color.a() << ")";
62+
return tmp.str();
63+
});
64+
65+
c.def("__repr__", [](const RGBAColor& color){
66+
std::stringstream tmp;
67+
tmp << "RGBAColor(" << color.r() << "," << color.g() << "," << color.b() << "," << color.a() << ")";
68+
return tmp.str();
69+
});
70+
71+
// Dark magic to define comparison operator using the c++ == and != operator.
72+
c.def(pybind11::self==pybind11::self);
73+
c.def(pybind11::self!=pybind11::self);
74+
}
75+
76+
} // namespace sofapython3
77+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/******************************************************************************
2+
* SOFA, Simulation Open-Framework Architecture *
3+
* (c) 2021 INRIA, USTL, UJF, CNRS, MGH *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify it *
6+
* under the terms of the GNU Lesser General Public License as published by *
7+
* the Free Software Foundation; either version 2.1 of the License, or (at *
8+
* your option) any later version. *
9+
* *
10+
* This program is distributed in the hope that it will be useful, but WITHOUT *
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
13+
* for more details. *
14+
* *
15+
* You should have received a copy of the GNU Lesser General Public License *
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17+
*******************************************************************************
18+
* Contact information: [email protected] *
19+
******************************************************************************/
20+
21+
#pragma once
22+
23+
#include <pybind11/pybind11.h>
24+
25+
namespace sofapython3 {
26+
27+
void moduleAddRGBAColor(pybind11::module& m);
28+
29+
} // namespace sofapython3

bindings/Sofa/src/SofaPython3/Sofa/Types/Submodule_Types.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include <SofaPython3/Sofa/Types/Binding_BoundingBox.h>
2525
#include <SofaPython3/Sofa/Types/Binding_CompressedRowSparseMatrix.h>
26-
26+
#include <SofaPython3/Sofa/Types/Binding_RGBAColor.h>
2727

2828
namespace sofapython3 {
2929
/// The first parameter must be named the same as the module file to load.
@@ -37,6 +37,7 @@ PYBIND11_MODULE(Types, types)
3737
Defines SOFA types (BoundingBox)
3838
)doc";
3939

40+
moduleAddRGBAColor(types);
4041
moduleAddBoundingBox(types);
4142
moduleAddCompressedRowSparseMatrix(types);
4243
}

bindings/Sofa/tests/Types/RGBAColor.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,30 @@
55

66
class Test(unittest.TestCase):
77
def test_constructor_default(self):
8-
v0 = Sofa.PyTypes.RGBAColor()
9-
self.assertEqual( v0.r(), 0 )
10-
self.assertEqual( v0.g(), 0 )
11-
self.assertEqual( v0.b(), 0 )
12-
self.assertEqual( v0.a(), 0 )
8+
v0 = Sofa.Types.RGBAColor()
9+
self.assertEqual( v0.r(), 1 )
10+
self.assertEqual( v0.g(), 1 )
11+
self.assertEqual( v0.b(), 1 )
12+
self.assertEqual( v0.a(), 1 )
1313

1414
def test_constructor_fromList(self):
15-
v0 = Sofa.PyTypes.RGBAColor([1.0,2.0,3.0,4.0])
15+
v0 = Sofa.Types.RGBAColor([1.0,2.0,3.0,4.0])
1616
self.assertEqual( v0.r(), 1.0 )
1717
self.assertEqual( v0.g(), 2.0 )
1818
self.assertEqual( v0.b(), 3.0 )
1919
self.assertEqual( v0.a(), 4.0 )
2020

2121
def test_constructor_fromInvalidList(self):
22-
self.assertRaises(ValueError, Sofa.PyTypes.RGBAColor, [1.0,2.0,3.0])
23-
self.assertRaises(ValueError, Sofa.PyTypes.RGBAColor, [1.0,2.0,3.0,10,100])
24-
self.assertRaises(TypeError, Sofa.PyTypes.RGBAColor, [1.0,2.0,"three",4.0])
22+
self.assertRaises(TypeError, Sofa.Types.RGBAColor, [1.0,2.0,3.0])
23+
self.assertRaises(TypeError, Sofa.Types.RGBAColor, [1.0,2.0,3.0,10,100])
24+
self.assertRaises(TypeError, Sofa.Types.RGBAColor, [1.0,2.0,"three",4.0])
25+
26+
def test_lighten(self):
27+
color = Sofa.Types.RGBAColor(0,0,0,0)
28+
lightcolor = color.lighten(0.5)
29+
self.assertEqual( lightcolor, Sofa.Types.RGBAColor(0.5,0.5,0.5,1.0))
30+
31+
whitecolor = color.lighten(1.0)
32+
self.assertEqual( whitecolor, Sofa.Types.RGBAColor(1.0,1.0,1.0,1.0) )
33+
2534

0 commit comments

Comments
 (0)