Debian Codegraph @

src/python/PyImath/PyImathColor3.cpp

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 // clang-format off
7
8 //
9 // This .C file was turned into a header file so that instantiations
10 // of the various V3* types can be spread across multiple files in
11 // order to work around MSVC limitations.
12 //
13
14 #include <Python.h>
15 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
16 #include <boost/python.hpp>
17 #include <boost/python/make_constructor.hpp>
18 #include <boost/format.hpp>
19 #include <ImathVec.h>
20 #include <ImathColor.h>
21 #include <ImathColorAlgo.h>
22 #include "PyImath.h"
23 #include "PyImathMathExc.h"
24 #include "PyImathColor.h"
25 #include "PyImathVec.h"
26 #include "PyImathDecorators.h"
27 #include "PyImathExport.h"
28 #include "PyImathColor3ArrayImpl.h"
29
30 namespace PyImath {
31 template <> const char *PyImath::C3cArray::name() { return "C3cArray"; }
32 template <> const char *PyImath::C3fArray::name() { return "C3fArray"; }
33
34 using namespace boost::python;
35 using namespace IMATH_NAMESPACE;
36
37 template <class T> struct Color3Name { static const char *value; };
38 template<> const char *Color3Name<unsigned char>::value = "Color3c";
39 template<> const char *Color3Name<float>::value = "Color3f";
40
41 // create a new default constructor that initializes Color3<T> to zero.
42 template <class T>
43 static Color3<T> * Color3_construct_default()
44 {
45 return new Color3<T>(T(0),T(0),T(0));
46 }
47
48 template <class T, class S>
49 static Color3<T> * Color3_component_construct1(S x, S y, S z)
50 {
51 // Assigning a floating point value to an integer type can cause a
52 // float-point error, which we want to translate into an exception.
53
54 MATH_EXC_ON;
55
56 if(strcmp(Color3Name<T>::value, "Color3c") == 0)
57 {
58 unsigned char r = (unsigned char) x;
59 unsigned char g = (unsigned char) y;
60 unsigned char b = (unsigned char) z;
61
62 return new Color3<T>(r,g,b);
63 }
64 else
65 return new Color3<T>(T (x), T(y), T(z));
66 }
67
68 template <class T, class S>
69 static Color3<T> * Color3_component_construct2(S x)
70 {
71 MATH_EXC_ON;
72
73 if(strcmp(Color3Name<T>::value, "Color3c") == 0)
74 {
75 unsigned char u = (unsigned char) x;
76
77 return new Color3<T>(u,u,u);
78 }
79 else
80 return new Color3<T>(T(x), T(x), T(x));
81 }
82
83 template <class T, class S>
84 static Color3<T> * Color3_color_construct(const Color3<S> &c)
85 {
86 MATH_EXC_ON;
87
88 if(strcmp(Color3Name<T>::value, "Color3c") == 0)
89 {
90 unsigned char r = (unsigned char) c.x;
91 unsigned char g = (unsigned char) c.y;
92 unsigned char b = (unsigned char) c.z;
93
94 return new Color3<T>(r,g,b);
95 }
96 else
97 return new Color3<T>(T (c.x), T(c.y), T(c.z));
98 }
99
100 template <class T, class S>
101 static Color3<T> * Color3_vector_construct(const Vec3<S> &c)
102 {
103 MATH_EXC_ON;
104
105 if(strcmp(Color3Name<T>::value, "Color3c") == 0)
106 {
107 unsigned char r = (unsigned char) c.x;
108 unsigned char g = (unsigned char) c.y;
109 unsigned char b = (unsigned char) c.z;
110
111 return new Color3<T>(r,g,b);
112 }
113 else
114 return new Color3<T>(T (c.x), T(c.y), T(c.z));
115 }
116
117
118 template <class T>
119 static std::string
120 color3_str(const Color3<T> &v)
121 {
122 std::stringstream stream;
123 if(strcmp(Color3Name<T>::value, "Color3c") == 0)
124 {
125 int r = int(v.x);
126 int g = int(v.y);
127 int b = int(v.z);
128
129 stream << Color3Name<T>::value << "(" << r << ", " << g << ", " << b << ")";
130 return stream.str();
131 }
132 else
133 {
134 stream << Color3Name<T>::value << "(" << v.x << ", " << v.y << ", " << v.z << ")";
135 return stream.str();
136 }
137 }
138
139 // Non-specialized repr is same as str
140 template <class T>
141 static std::string
142 color3_repr(const Color3<T> &v)
143 {
144 return color3_str(v);
145 }
146
147 // Specialization for float to full precision
148 template <>
149 std::string
150 color3_repr(const Color3<float> &v)
151 {
152 return (boost::format("%s(%.9g, %.9g, %.9g)")
153 % Color3Name<float>::value % v.x % v.y % v.z).str();
154 }
155
156 // No specialization for double, since we don't instantiate Color3d
157
158
159 template <class T>
160 static Color3<T> * Color3_construct_tuple(const tuple &t)
161 {
162 MATH_EXC_ON;
163 if(t.attr("__len__")() == 3)
164 {
165 return new Color3<T>(extract<T>(t[0]), extract<T>(t[1]), extract<T>(t[2]));
166 }
167 else
168 throw std::invalid_argument ("Color3 expects tuple of length 3");
169 }
170
171 template <class T>
172 static Color3<T> * Color3_construct_list(const list &l)
173 {
174 MATH_EXC_ON;
175 if(l.attr("__len__")() == 3)
176 {
177 return new Color3<T>(extract<T>(l[0]), extract<T>(l[1]), extract<T>(l[2]));
178 }
179 else
180 throw std::invalid_argument ("Color3 expects list of length 3");
181 }
182
183 template <class T>
184 static const Color3<T> &
185 iadd(Color3<T> &color, const Color3<T> &color2)
186 {
187 MATH_EXC_ON;
188 return color += color2;
189 }
190
191 template <class T>
192 static Color3<T>
193 add(Color3<T> &color, const Color3<T> &color2)
194 {
195 MATH_EXC_ON;
196 return color + color2;
197 }
198
199 template <class T>
200 static Color3<T>
201 addTuple(Color3<T> &color, const tuple &t)
202 {
203 MATH_EXC_ON;
204 if(t.attr("__len__")() == 3)
205 return Color3<T>(color.x + extract<T>(t[0]),
206 color.y + extract<T>(t[1]),
207 color.z + extract<T>(t[2]));
208 else
209 throw std::invalid_argument ("Color3 expects tuple of length 3");
210 }
211
212 template <class T>
213 static Color3<T>
214 addT(Color3<T> &v, T a)
215 {
216 MATH_EXC_ON;
217 Color3<T> w(v.x + a, v.y + a, v.z + a);
218 return w;
219 }
220
221 template <class T>
222 static const Color3<T> &
223 isub(Color3<T> &color, const Color3<T> &color2)
224 {
225 MATH_EXC_ON;
226 return color -= color2;
227 }
228
229 template <class T>
230 static Color3<T>
231 sub(Color3<T> &color, const Color3<T> &color2)
232 {
233 MATH_EXC_ON;
234 return color - color2;
235 }
236
237 template <class T>
238 static Color3<T>
239 subtractL(Color3<T> &color, const tuple &t)
240 {
241 MATH_EXC_ON;
242 if(t.attr("__len__")() == 3)
243 return Color3<T>(color.x - extract<T>(t[0]),
244 color.y - extract<T>(t[1]),
245 color.z - extract<T>(t[2]));
246 else
247 throw std::invalid_argument ("Color3 expects tuple of length 3");
248 }
249
250 template <class T>
251 static Color3<T>
252 subtractLT(const Color3<T> &color, T a)
253 {
254 MATH_EXC_ON;
255 return Color3<T>(color.x - a,
256 color.y - a,
257 color.z - a);
258 }
259
260 template <class T>
261 static Color3<T>
262 subtractRT(const Color3<T> &color, T a)
263 {
264 MATH_EXC_ON;
265 return Color3<T>(a - color.x,
266 a - color.y,
267 a - color.z);
268 }
269
270 template <class T>
271 static Color3<T>
272 subtractR(Color3<T> &color, const tuple &t)
273 {
274 MATH_EXC_ON;
275 if(t.attr("__len__")() == 3)
276 return Color3<T>(extract<T>(t[0]) - color.x,
277 extract<T>(t[1]) - color.y,
278 extract<T>(t[2]) - color.z);
279 else
280 throw std::invalid_argument ("Color3 expects tuple of length 3");
281 }
282
283 template <class T>
284 static Color3<T>
285 neg(Color3<T> &color)
286 {
287 MATH_EXC_ON;
288 return -color;
289 }
290
291 template <class T>
292 static const Color3<T> &
293 negate(Color3<T> &color)
294 {
295 MATH_EXC_ON;
296 return color.negate();
297 }
298
299 template <class T>
300 static const Color3<T> &
301 imul(Color3<T> &color, const Color3<T> &color2)
302 {
303 MATH_EXC_ON;
304 return color *= color2;
305 }
306
307 template <class T>
308 static const Color3<T> &
309 imulT(Color3<T> &color, const T &t)
310 {
311 MATH_EXC_ON;
312 return color *= t;
313 }
314
315 template <class T>
316 static Color3<T>
317 mul(Color3<T> &color, const Color3<T> &color2)
318 {
319 MATH_EXC_ON;
320 return color * color2;
321 }
322
323 template <class T>
324 static Color3<T>
325 mulT(Color3<T> &color, const T &t)
326 {
327 MATH_EXC_ON;
328 return color * t;
329 }
330
331 template <class T>
332 static Color3<T>
333 rmulT(Color3<T> &color, const T &t)
334 {
335 MATH_EXC_ON;
336 return t * color;
337 }
338
339 template <class T>
340 static Color3<T>
341 mulTuple(Color3<T> &color, const tuple &t)
342 {
343 MATH_EXC_ON;
344 if(t.attr("__len__")() == 3)
345 return Color3<T>(color.x * extract<T>(t[0]),
346 color.y * extract<T>(t[1]),
347 color.z * extract<T>(t[2]));
348 else
349 throw std::invalid_argument ("Color3 expects tuple of length 3");
350 }
351
352 template <class T>
353 static const Color3<T> &
354 idiv(Color3<T> &color, const Color3<T> &color2)
355 {
356 MATH_EXC_ON;
357 return color /= color2;
358 }
359
360 template <class T>
361 static const Color3<T> &
362 idivT(Color3<T> &color, const T &t)
363 {
364 MATH_EXC_ON;
365 return color /= t;
366 }
367
368 template <class T>
369 static Color3<T>
370 div(Color3<T> &color, const Color3<T> &color2)
371 {
372 MATH_EXC_ON;
373 return color / color2;
374 }
375
376 template <class T>
377 static Color3<T>
378 divT(Color3<T> &color, const T &t)
379 {
380 MATH_EXC_ON;
381 return color / t;
382 }
383
384 template <class T>
385 static Color3<T>
386 divTupleL(Color3<T> &color, const tuple &t)
387 {
388 MATH_EXC_ON;
389 if(t.attr("__len__")() == 3)
390 return Color3<T>(color.x / extract<T>(t[0]),
391 color.y / extract<T>(t[1]),
392 color.z / extract<T>(t[2]));
393 else
394 throw std::invalid_argument ("Color3 expects tuple of length 3");
395 }
396
397 template <class T>
398 static Color3<T>
399 divTupleR(Color3<T> &color, const tuple &t)
400 {
401 MATH_EXC_ON;
402 if(t.attr("__len__")() == 3)
403 return Color3<T>(extract<T>(t[0]) / color.x,
404 extract<T>(t[1]) / color.y,
405 extract<T>(t[2]) / color.z);
406 else
407 throw std::invalid_argument ("Color3 expects tuple of length 3");
408 }
409
410 template <class T>
411 static Color3<T>
412 divTR(Color3<T> &color, T a)
413 {
414 MATH_EXC_ON;
415 return Color3<T>(a / color.x,
416 a / color.y,
417 a / color.z);
418 }
419
420 template <class T>
421 static Color3<T>
422 hsv2rgb(Color3<T> &color)
423 {
424 MATH_EXC_ON;
425 return IMATH_NAMESPACE::hsv2rgb(color);
426 }
427
428 template <class T>
429 static Color3<T>
430 hsv2rgbTuple(const tuple &t)
431 {
432 MATH_EXC_ON;
433 Color3<T> color;
434 if(t.attr("__len__")() == 3)
435 {
436 color.x = extract<T>(t[0]);
437 color.y = extract<T>(t[1]);
438 color.z = extract<T>(t[2]);
439
440 return IMATH_NAMESPACE::hsv2rgb(color);
441 }
442 else
443 throw std::invalid_argument ("Color3 expects tuple of length 3");
444 }
445
446 template <class T>
447 static Color3<T>
448 rgb2hsv(Color3<T> &color)
449 {
450 MATH_EXC_ON;
451 return IMATH_NAMESPACE::rgb2hsv(color);
452 }
453
454 template <class T>
455 static Color3<T>
456 rgb2hsvTuple(const tuple &t)
457 {
458 MATH_EXC_ON;
459 Color3<T> color;
460 if(t.attr("__len__")() == 3)
461 {
462 color.x = extract<T>(t[0]);
463 color.y = extract<T>(t[1]);
464 color.z = extract<T>(t[2]);
465
466 return IMATH_NAMESPACE::rgb2hsv(color);
467 }
468 else
469 throw std::invalid_argument ("Color3 expects tuple of length 3");
470 }
471
472 template <class T>
473 static void
474 setValue1(Color3<T> &color, const T &a, const T &b, const T &c)
475 {
476 MATH_EXC_ON;
477 color.setValue(a, b, c);
478 }
479
480 template <class T>
481 static void
482 setValue2(Color3<T> &color, const Color3<T> &v)
483 {
484 MATH_EXC_ON;
485 color.setValue(v);
486 }
487
488 template <class T>
489 static void
490 setValueTuple(Color3<T> &color, const tuple &t)
491 {
492 MATH_EXC_ON;
493 Color3<T> v;
494 if(t.attr("__len__")() == 3)
495 {
496 v.x = extract<T>(t[0]);
497 v.y = extract<T>(t[1]);
498 v.z = extract<T>(t[2]);
499
500 color.setValue(v);
501 }
502 else
503 throw std::invalid_argument ("Color3 expects tuple of length 3");
504 }
505
506 template <class T>
507 static bool
508 lessThan(Color3<T> &v, const Color3<T> &w)
509 {
510 bool isLessThan = (v.x <= w.x && v.y <= w.y && v.z <= w.z)
511 && v != w;
512
513 return isLessThan;
514 }
515
516 template <class T>
517 static bool
518 greaterThan(Color3<T> &v, const Color3<T> &w)
519 {
520 bool isGreaterThan = (v.x >= w.x && v.y >= w.y && v.z >= w.z)
521 && v != w;
522
523 return isGreaterThan;
524 }
525
526 template <class T>
527 static bool
528 lessThanEqual(Color3<T> &v, const Color3<T> &w)
529 {
530 bool isLessThanEqual = (v.x <= w.x && v.y <= w.y && v.z <= w.z);
531
532 return isLessThanEqual;
533 }
534
535 template <class T>
536 static bool
537 greaterThanEqual(Color3<T> &v, const Color3<T> &w)
538 {
539 bool isGreaterThanEqual = (v.x >= w.x && v.y >= w.y && v.z >= w.z);
540
541 return isGreaterThanEqual;
542 }
543
544 template <class T>
545 class_<Color3<T>, bases<Vec3<T> > >
546 register_Color3()
547 {
548 class_<Color3<T>, bases<Vec3<T> > > color3_class(Color3Name<T>::value, Color3Name<T>::value,init<Color3<T> >("copy construction"));
549 color3_class
550 .def("__init__",make_constructor(Color3_construct_default<T>),"initialize to (0,0,0)")
551 .def("__init__",make_constructor(Color3_construct_tuple<T>), "initialize to (r,g,b) with a python tuple")
552 .def("__init__",make_constructor(Color3_construct_list<T>), "initialize to (r,g,b) with a python list")
553 .def("__init__",make_constructor(Color3_component_construct1<T,float>))
554 .def("__init__",make_constructor(Color3_component_construct1<T,int>))
555 .def("__init__",make_constructor(Color3_component_construct2<T,float>))
556 .def("__init__",make_constructor(Color3_component_construct2<T,int>))
557 .def("__init__",make_constructor(Color3_color_construct<T,float>))
558 .def("__init__",make_constructor(Color3_color_construct<T,int>))
559 .def("__init__",make_constructor(Color3_color_construct<T,unsigned char>))
560 .def("__init__",make_constructor(Color3_vector_construct<T,float>))
561 .def("__init__",make_constructor(Color3_vector_construct<T,double>))
562 .def("__init__",make_constructor(Color3_vector_construct<T,int>))
563
564 .def_readwrite("r", &Color3<T>::x)
565 .def_readwrite("g", &Color3<T>::y)
566 .def_readwrite("b", &Color3<T>::z)
567 .def("__str__", &color3_str<T>)
568 .def("__repr__", &color3_repr<T>)
569 .def(self == self) // NOSONAR - suppress SonarCloud bug report.
570 .def(self != self) // NOSONAR - suppress SonarCloud bug report.
571 .def("__iadd__", &iadd<T>,return_internal_reference<>())
572 .def("__add__", &add<T>)
573 .def("__add__", &addTuple<T>)
574 .def("__add__", &addT<T>)
575 .def("__radd__", &addTuple<T>)
576 .def("__radd__", &addT<T>)
577 .def("__isub__", &isub<T>,return_internal_reference<>())
578 .def("__sub__", &sub<T>)
579 .def("__sub__", &subtractL<T>)
580 .def("__sub__", &subtractLT<T>)
581 .def("__rsub__", &subtractR<T>)
582 .def("__rsub__", &subtractRT<T>)
583 .def("__neg__", &neg<T>)
584 .def("negate",&negate<T>,return_internal_reference<>(),"component-wise multiplication by -1")
585 .def("__imul__", &imul<T>,return_internal_reference<>())
586 .def("__imul__", &imulT<T>,return_internal_reference<>())
587 .def("__mul__", &mul<T>)
588 .def("__mul__", &mulT<T>)
589 .def("__rmul__", &rmulT<T>)
590 .def("__mul__", &mulTuple<T>)
591 .def("__rmul__", &mulTuple<T>)
592 .def("__idiv__", &idiv<T>,return_internal_reference<>())
593 .def("__idiv__", &idivT<T>,return_internal_reference<>())
594 .def("__itruediv__", &idiv<T>,return_internal_reference<>())
595 .def("__itruediv__", &idivT<T>,return_internal_reference<>())
596 .def("__div__", &div<T>)
597 .def("__div__", &divT<T>)
598 .def("__div__", &divTupleL<T>)
599 .def("__truediv__", &div<T>)
600 .def("__truediv__", &divT<T>)
601 .def("__truediv__", &divTupleL<T>)
602 .def("__rdiv__", &divTupleR<T>)
603 .def("__rdiv__", &divTR<T>)
604 .def("__rtruediv__", &divTupleR<T>)
605 .def("__rtruediv__", &divTR<T>)
606 .def("__lt__", &lessThan<T>)
607 .def("__gt__", &greaterThan<T>)
608 .def("__le__", &lessThanEqual<T>)
609 .def("__ge__", &greaterThanEqual<T>)
610 .def("dimensions", &Color3<T>::dimensions,"dimensions() number of dimensions in the color")
611 .staticmethod("dimensions")
612 .def("baseTypeEpsilon", &Color3<T>::baseTypeEpsilon,"baseTypeEpsilon() epsilon value of the base type of the color")
613 .staticmethod("baseTypeEpsilon")
614 .def("baseTypeMax", &Color3<T>::baseTypeMax,"baseTypeMax() max value of the base type of the color")
615 .staticmethod("baseTypeMax")
616 .def("baseTypeLowest", &Color3<T>::baseTypeLowest,"baseTypeLowest() largest negative value of the base type of the color")
617 .staticmethod("baseTypeLowest")
618 .def("baseTypeSmallest", &Color3<T>::baseTypeSmallest,"baseTypeSmallest() smallest value of the base type of the color")
619 .staticmethod("baseTypeSmallest")
620 .def("hsv2rgb", &hsv2rgb<T>,
621 "C.hsv2rgb() -- returns a new color which "
622 "is C converted from RGB to HSV")
623 .def("hsv2rgb", &rgb2hsvTuple<T>)
624
625 .def("rgb2hsv", &rgb2hsv<T>,
626 "C.rgb2hsv() -- returns a new color which "
627 "is C converted from HSV to RGB")
628 .def("rgb2hsv", &rgb2hsvTuple<T>)
629
630 .def("setValue", &setValue1<T>,
631 "C1.setValue(C2)\nC1.setValue(a,b,c) -- "
632 "set C1's elements")
633 .def("setValue", &setValue2<T>)
634 .def("setValue", &setValueTuple<T>)
635 ;
636
637 decoratecopy(color3_class);
638
639 return color3_class;
640 }
641
642 template PYIMATH_EXPORT class_<Color3<float>, bases<Vec3<float> > > register_Color3<float>();
643 template PYIMATH_EXPORT class_<Color3<unsigned char>, bases<Vec3<unsigned char> > > register_Color3<unsigned char>();
644 template PYIMATH_EXPORT class_<FixedArray<Color3<float> > > register_Color3Array<float>();
645 template PYIMATH_EXPORT class_<FixedArray<Color3<unsigned char> > > register_Color3Array<unsigned char>();
646
647 template<> PYIMATH_EXPORT IMATH_NAMESPACE::Color3<float> PyImath::FixedArrayDefaultValue<IMATH_NAMESPACE::Color3<float> >::value()
648 { return IMATH_NAMESPACE::Color3<float>(0,0,0); }
649 template<> PYIMATH_EXPORT IMATH_NAMESPACE::Color3<unsigned char> PyImath::FixedArrayDefaultValue<IMATH_NAMESPACE::Color3<unsigned char> >::value()
650 { return IMATH_NAMESPACE::Color3<unsigned char>(0,0,0); }
651
652 }