HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PyImathColor.h
Go to the documentation of this file.
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 HBOOST_BIND_GLOBAL_PLACEHOLDERS
13 #include <hboost/python.hpp>
14 #include <ImathColor.h>
15 #include "PyImath.h"
16 
17 namespace PyImath {
18 
19 template <class T> hboost::python::class_<IMATH_NAMESPACE::Color4<T> > register_Color4();
20 template <class T> hboost::python::class_<PyImath::FixedArray2D<IMATH_NAMESPACE::Color4<T> > > register_Color4Array2D();
21 template <class T> hboost::python::class_<PyImath::FixedArray<IMATH_NAMESPACE::Color4<T> > > register_Color4Array();
22 template <class T> hboost::python::class_<IMATH_NAMESPACE::Color3<T>, hboost::python::bases<IMATH_NAMESPACE::Vec3<T> > > register_Color3();
23 template <class T> hboost::python::class_<PyImath::FixedArray<IMATH_NAMESPACE::Color3<T> > > register_Color3Array();
24 
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 hboost::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 hboost::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  hboost::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  hboost::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  hboost::python::extract <hboost::python::tuple> extractorTuple (p);
95  if (extractorTuple.check())
96  {
97  hboost::python::tuple t = extractorTuple();
98  if (t.attr ("__len__") () == 3)
99  {
100  double a = hboost::python::extract <double> (t[0]);
101  double b = hboost::python::extract <double> (t[1]);
102  double c = hboost::python::extract <double> (t[2]);
103  v->setValue (U(a), U(b), U(c));
104  return 1;
105  }
106  }
107 
108  hboost::python::extract <hboost::python::list> extractorList (p);
109  if (extractorList.check())
110  {
111  hboost::python::list l = extractorList();
112  if (l.attr ("__len__") () == 3)
113  {
114  hboost::python::extract <double> extractor0 (l[0]);
115  hboost::python::extract <double> extractor1 (l[1]);
116  hboost::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  hboost::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  hboost::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  hboost::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  hboost::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  hboost::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  hboost::python::extract <hboost::python::tuple> extractorTuple (p);
175  if (extractorTuple.check())
176  {
177  hboost::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 = hboost::python::extract <double> (t[0]);
185  double b = hboost::python::extract <double> (t[1]);
186  double c = hboost::python::extract <double> (t[2]);
187  double d = hboost::python::extract <double> (t[3]);
188  v->setValue (U(a), U(b), U(c), U(d));
189  return 1;
190  }
191  }
192 
193  hboost::python::extract <hboost::python::list> extractorList (p);
194  if (extractorList.check())
195  {
196  hboost::python::list l = extractorList();
197  if (l.attr ("__len__") () == 4)
198  {
199  hboost::python::extract <double> extractor0 (l[0]);
200  hboost::python::extract <double> extractor1 (l[1]);
201  hboost::python::extract <double> extractor2 (l[2]);
202  hboost::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 
219 typedef Color3f C3f;
220 typedef Color3c C3c;
221 
224 typedef Color4f C4f;
225 typedef Color4c C4c;
226 
227 }
228 
229 #endif
Color3f C3f
Definition: PyImathColor.h:219
FixedArray< IMATH_NAMESPACE::Color3c > C3cArray
Definition: PyImathColor.h:30
const GLdouble * v
Definition: glcorearb.h:837
hboost::python::class_< IMATH_NAMESPACE::Color4< T > > register_Color4()
Vec3< int > V3i
Vec3 of integer.
Definition: ImathVec.h:1058
Color4< float > C4f
4 float channels
Definition: ImathColor.h:345
Color4f C4f
Definition: PyImathColor.h:224
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
hboost::python::class_< PyImath::FixedArray< IMATH_NAMESPACE::Color4< T > > > register_Color4Array()
Color3c C3c
Definition: PyImathColor.h:220
static PyObject * wrap(const IMATH_NAMESPACE::Color4< T > &c)
Definition: PyImathColor.h:67
static int convert(PyObject *p, IMATH_NAMESPACE::Color3< T > *v)
Definition: PyImathColor.h:76
FixedArray< IMATH_NAMESPACE::Color3f > C3fArray
Definition: PyImathColor.h:29
Vec3< double > V3d
Vec3 of double.
Definition: ImathVec.h:1067
C4< float, float > Color4f
Definition: PyImathColor.h:222
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
C3< unsigned char, int > Color3c
Definition: PyImathColor.h:218
C4< unsigned char, int > Color4c
Definition: PyImathColor.h:223
FixedArray< IMATH_NAMESPACE::Color4c > C4cArray
Definition: PyImathColor.h:28
Color4< unsigned char > C4c
4 8-bit integer channels
Definition: ImathColor.h:351
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLdouble t
Definition: glad.h:2397
Vec3< float > V3f
Vec3 of float.
Definition: ImathVec.h:1064
hboost::python::class_< PyImath::FixedArray< IMATH_NAMESPACE::Color3< T > > > register_Color3Array()
static int convert(PyObject *p, IMATH_NAMESPACE::Color4< T > *v)
Definition: PyImathColor.h:156
FixedArray< IMATH_NAMESPACE::Color4f > C4fArray
Definition: PyImathColor.h:27
hboost::python::class_< IMATH_NAMESPACE::Color3< T >, hboost::python::bases< IMATH_NAMESPACE::Vec3< T > > > register_Color3()
hboost::python::class_< PyImath::FixedArray2D< IMATH_NAMESPACE::Color4< T > > > register_Color4Array2D()
Color4c C4c
Definition: PyImathColor.h:225
FixedArray2D< IMATH_NAMESPACE::Color4f > Color4fArray
Definition: PyImathColor.h:25
static PyObject * wrap(const IMATH_NAMESPACE::Color3< T > &c)
Definition: PyImathColor.h:58
Color3< float > C3f
3 float channels
Definition: ImathColor.h:330
C3< float, float > Color3f
Definition: PyImathColor.h:217
Color3< unsigned char > C3c
3 8-bit integer channels
Definition: ImathColor.h:333
FixedArray2D< IMATH_NAMESPACE::Color4c > Color4cArray
Definition: PyImathColor.h:26