8000 Add a pybind11 type caster for agg::rgba · matplotlib/matplotlib@a47e26b · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a47e26b

Browse files
committed
Add a pybind11 type caster for agg::rgba
1 parent 96dd843 commit a47e26b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/py_converters_11.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace py = pybind11;
1010

1111
#include "agg_basics.h"
12+
#include "agg_color_rgba.h"
1213
#include "agg_trans_affine.h"
1314
#include "path_converters.h"
1415

@@ -58,6 +59,36 @@ namespace PYBIND11_NAMESPACE { namespace detail {
5859
}
5960
};
6061

62+
template <> struct type_caster<agg::rgba> {
63+
public:
64+
PYBIND11_TYPE_CASTER(agg::rgba, const_name("rgba"));
65+
66+
bool load(handle src, bool) {
67+
if (src.is_none()) {
68+
value.r = 0.0;
69+
value.g = 0.0;
70+
value.b = 0.0;
71+
value.a = 0.0;
72+
} else {
73+
auto rgbatuple = src.cast<py::tuple>();
74+
value.r = rgbatuple[0].cast<double>();
75+
value.g = rgbatuple[1].cast<double>();
76+
value.b = rgbatuple[2].cast<double>();
77+
switch (rgbatuple.size()) {
78+
case 4:
79+
value.a = rgbatuple[3].cast<double>();
80+
break;
81+
case 3:
82+
value.a = 1.0;
83+
break;
84+
default:
85+
throw py::value_error("RGBA value must be 3- or 4-tuple");
86+
}
87+
}
88+
return true;
89+
}
90+
};
91+
6192
template <> struct type_caster<agg::trans_affine> {
6293
public:
6394
PYBIND11_TYPE_CASTER(agg::trans_affine, const_name("trans_affine"));

0 commit comments

Comments
 (0)
0