Debian Codegraph @

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