Debian Codegraph @

src/python/PyImath/PyImathColor.h

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 // clang-format off
7
8 #ifndef _PyImathColor3_h_
9 #define _PyImathColor3_h_
10
11 #include <Python.h>
12 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
13 #include <boost/python.hpp>
14 #include <ImathColor.h>
15 #include "PyImath.h"
16
17 namespace PyImath {
18
19 template <class T> boost::python::class_<IMATH_NAMESPACE::Color4<T> > register_Color4();
20 template <class T> boost::python::class_<PyImath::FixedArray2D<IMATH_NAMESPACE::Color4<T> > > register_Color4Array2D();
21 template <class T> boost::python::class_<PyImath::FixedArray<IMATH_NAMESPACE::Color4<T> > > register_Color4Array();
22 template <class T> boost::python::class_<IMATH_NAMESPACE::Color3<T>, boost::python::bases<IMATH_NAMESPACE::Vec3<T> > > register_Color3();
23 template <class T> boost::python::class_<PyImath::FixedArray<IMATH_NAMESPACE::Color3<T> > > register_Color3Array();
24
25 typedef FixedArray2D<IMATH_NAMESPACE::Color4f> Color4fArray;
26 typedef FixedArray2D<IMATH_NAMESPACE::Color4c> Color4cArray;
27 typedef FixedArray<IMATH_NAMESPACE::Color4f> C4fArray;
28 typedef FixedArray<IMATH_NAMESPACE::Color4c> C4cArray;
29 typedef FixedArray<IMATH_NAMESPACE::Color3f> C3fArray;
30 typedef FixedArray<IMATH_NAMESPACE::Color3c> C3cArray;
31
32 //
33 // Other code in the Zeno code base assumes the existance of a class with the
34 // same name as the Imath class, and with static functions wrap() and
35 // convert() to produce a PyImath object from an Imath object and vice-versa,
36 // respectively. The class Boost generates from the Imath class does not
37 // have these properties, so we define a companion class here.
38 // The template argument, T, is the element type for the color in C++ (e.g., char,
39 // float). The other argument, U, is how this type is represented in Python
40 // (e.g., int, float).
41
42 template <class T, class U>
43 class C3 {
44 public:
45 static PyObject * wrap (const IMATH_NAMESPACE::Color3<T> &c);
46 static int convert (PyObject *p, IMATH_NAMESPACE::Color3<T> *v);
47 };
48
49 template <class T, class U>
50 class C4 {
51 public:
52 static PyObject * wrap (const IMATH_NAMESPACE::Color4<T> &c);
53 static int convert (PyObject *p, IMATH_NAMESPACE::Color4<T> *v);
54 };
55
56 template <class T, class U>
57 PyObject *
58 C3<T, U>::wrap (const IMATH_NAMESPACE::Color3<T> &c)
59 {
60 typename boost::python::return_by_value::apply < IMATH_NAMESPACE::Color3<T> >::type converter;
61 PyObject *p = converter (c);
62 return p;
63 }
64
65 template <class T, class U>
66 PyObject *
67 C4<T, U>::wrap (const IMATH_NAMESPACE::Color4<T> &c)
68 {
69 typename boost::python::return_by_value::apply < IMATH_NAMESPACE::Color4<T> >::type converter;
70 PyObject *p = converter (c);
71 return p;
72 }
73
74 template <class T, class U>
75 int
76 C3<T, U>::convert (PyObject *p, IMATH_NAMESPACE::Color3<T> *v)
77 {
78 boost::python::extract <IMATH_NAMESPACE::C3c> extractorC3c (p);
79 if (extractorC3c.check())
80 {
81 IMATH_NAMESPACE::C3c c3c = extractorC3c();
82 v->setValue (U(c3c[0]), U(c3c[1]), U(c3c[2]));
83 return 1;
84 }
85
86 boost::python::extract <IMATH_NAMESPACE::C3f> extractorC3f (p);
87 if (extractorC3f.check())
88 {
89 IMATH_NAMESPACE::C3f c3f = extractorC3f();
90 v->setValue (U(c3f[0]), U(c3f[1]), U(c3f[2]));
91 return 1;
92 }
93
94 boost::python::extract <boost::python::tuple> extractorTuple (p);
95 if (extractorTuple.check())
96 {
97 boost::python::tuple t = extractorTuple();
98 if (t.attr ("__len__") () == 3)
99 {
100 double a = boost::python::extract <double> (t[0]);
101 double b = boost::python::extract <double> (t[1]);
102 double c = boost::python::extract <double> (t[2]);
103 v->setValue (U(a), U(b), U(c));
104 return 1;
105 }
106 }
107
108 boost::python::extract <boost::python::list> extractorList (p);
109 if (extractorList.check())
110 {
111 boost::python::list l = extractorList();
112 if (l.attr ("__len__") () == 3)
113 {
114 boost::python::extract <double> extractor0 (l[0]);
115 boost::python::extract <double> extractor1 (l[1]);
116 boost::python::extract <double> extractor2 (l[2]);
117 if (extractor0.check() && extractor1.check() &&
118 extractor2.check())
119 {
120 v->setValue (U(extractor0()), U(extractor1()),
121 U(extractor2()));
122 return 1;
123 }
124 }
125 }
126
127 boost::python::extract <IMATH_NAMESPACE::V3i> extractorV3i (p);
128 if (extractorV3i.check())
129 {
130 IMATH_NAMESPACE::V3i v3i = extractorV3i();
131 v->setValue (U(v3i[0]), U(v3i[1]), U(v3i[2]));
132 return 1;
133 }
134
135 boost::python::extract <IMATH_NAMESPACE::V3f> extractorV3f (p);
136 if (extractorV3f.check())
137 {
138 IMATH_NAMESPACE::V3f v3f = extractorV3f();
139 v->setValue (U(v3f[0]), U(v3f[1]), U(v3f[2]));
140 return 1;
141 }
142
143 boost::python::extract <IMATH_NAMESPACE::V3d> extractorV3d (p);
144 if (extractorV3d.check())
145 {
146 IMATH_NAMESPACE::V3d v3d = extractorV3d();
147 v->setValue (U(v3d[0]), U(v3d[1]), U(v3d[2]));
148 return 1;
149 }
150
151 return 0;
152 }
153
154 template <class T, class U>
155 int
156 C4<T, U>::convert (PyObject *p, IMATH_NAMESPACE::Color4<T> *v)
157 {
158 boost::python::extract <IMATH_NAMESPACE::C4c> extractorC4c (p);
159 if (extractorC4c.check())
160 {
161 IMATH_NAMESPACE::C4c c4c = extractorC4c();
162 v->setValue (U(c4c[0]), U(c4c[1]), U(c4c[2]), U(c4c[3]));
163 return 1;
164 }
165
166 boost::python::extract <IMATH_NAMESPACE::C4f> extractorC4f (p);
167 if (extractorC4f.check())
168 {
169 IMATH_NAMESPACE::C4f c4f = extractorC4f();
170 v->setValue (U(c4f[0]), U(c4f[1]), U(c4f[2]), U(c4f[3]));
171 return 1;
172 }
173
174 boost::python::extract <boost::python::tuple> extractorTuple (p);
175 if (extractorTuple.check())
176 {
177 boost::python::tuple t = extractorTuple();
178 if (t.attr ("__len__") () == 4)
179 {
180 // As with V3<T>, we extract the tuple elements as doubles and
181 // cast them to Ts in setValue(), to avoid any odd cases where
182 // extracting them as Ts from the start would fail.
183
184 double a = boost::python::extract <double> (t[0]);
185 double b = boost::python::extract <double> (t[1]);
186 double c = boost::python::extract <double> (t[2]);
187 double d = boost::python::extract <double> (t[3]);
188 v->setValue (U(a), U(b), U(c), U(d));
189 return 1;
190 }
191 }
192
193 boost::python::extract <boost::python::list> extractorList (p);
194 if (extractorList.check())
195 {
196 boost::python::list l = extractorList();
197 if (l.attr ("__len__") () == 4)
198 {
199 boost::python::extract <double> extractor0 (l[0]);
200 boost::python::extract <double> extractor1 (l[1]);
201 boost::python::extract <double> extractor2 (l[2]);
202 boost::python::extract <double> extractor3 (l[3]);
203 if (extractor0.check() && extractor1.check() &&
204 extractor2.check() && extractor3.check())
205 {
206 v->setValue (U(extractor0()), U(extractor1()),
207 U(extractor2()), U(extractor3()));
208 return 1;
209 }
210 }
211 }
212
213 return 0;
214 }
215
216
217 typedef C3<float, float> Color3f;
218 typedef C3<unsigned char, int> Color3c;
219 typedef Color3f C3f;
220 typedef Color3c C3c;
221
222 typedef C4<float, float> Color4f;
223 typedef C4<unsigned char, int> Color4c;
224 typedef Color4f C4f;
225 typedef Color4c C4c;
226
227 }
228
229 #endif