Skip to content

Commit 3a30cbc

Browse files
committed
Add a pybind11 type caster for agg::rgba
1 parent 5584644 commit 3a30cbc

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/_backend_agg_wrapper.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,14 @@ PyRendererAgg_draw_path(RendererAgg *self,
4343
pybind11::object gc_obj,
4444
mpl::PathIterator path,
4545
agg::trans_affine trans,
46-
pybind11::object face_obj)
46+
agg::rgba face)
4747
{
4848
GCAgg gc;
49-
agg::rgba face;
5049

5150
if (!convert_gcagg(gc_obj.ptr(), &gc)) {
5251
throw pybind11::error_already_set();
5352
}
5453

55-
if (!convert_face(face_obj.ptr(), gc, &face)) {
56-
throw pybind11::error_already_set();
57-
}
58-
5954
self->draw_path(gc, path, trans, face);
6055
}
6156

@@ -87,19 +82,14 @@ PyRendererAgg_draw_markers(RendererAgg *self,
8782
agg::trans_affine marker_path_trans,
8883
mpl::PathIterator path,
8984
agg::trans_affine trans,
90-
pybind11::object face_obj)
85+
agg::rgba face)
9186
{
9287
GCAgg gc;
93-
agg::rgba face;
9488

9589
if (!convert_gcagg(gc_obj.ptr(), &gc)) {
9690
throw pybind11::error_already_set();
9791
}
9892

99-
if (!convert_face(face_obj.ptr(), gc, &face)) {
100-
throw pybind11::error_already_set();
101-
}
102-
10393
self->draw_markers(gc, marker_path, marker_path_trans, path, trans, face);
10494
}
10595

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<pybind11::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 pybind11::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)