Blender V5.3
mathutils_Color.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
11#include <Python.h>
12
13#include "mathutils.hh"
14
15#include "BLI_utildefines.hh"
16
19
20#ifndef MATH_STANDALONE
21# include "IMB_colormanagement.hh"
22#endif
23
24#ifndef MATH_STANDALONE
25# include "BLI_dynstr.hh"
26#endif
27
28#define COLOR_SIZE 3
29
30namespace blender {
31
32/* -------------------------------------------------------------------- */
35
39static PyObject *Color_to_tuple_ex(ColorObject *self, int ndigits)
40{
41 PyObject *ret;
42 int i;
43
44 ret = PyTuple_New(COLOR_SIZE);
45
46 if (ndigits >= 0) {
47 for (i = 0; i < COLOR_SIZE; i++) {
48 PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round(double(self->col[i]), ndigits)));
49 }
50 }
51 else {
52 for (i = 0; i < COLOR_SIZE; i++) {
53 PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->col[i]));
54 }
55 }
56
57 return ret;
58}
59
61
62/* -------------------------------------------------------------------- */
65
66static PyObject *Color_vectorcall(PyObject *type,
67 PyObject *const *args,
68 const size_t nargsf,
69 PyObject *kwnames)
70{
71 if (kwnames && PyTuple_GET_SIZE(kwnames)) [[unlikely]] {
72 PyErr_SetString(PyExc_TypeError,
73 "mathutils.Color(): "
74 "takes no keyword args");
75 return nullptr;
76 }
77
78 float col[3] = {0.0f, 0.0f, 0.0f};
79
80 const size_t nargs = PyVectorcall_NARGS(nargsf);
81 switch (nargs) {
82 case 0: {
83 break;
84 }
85 case 1: {
86 if (mathutils_array_parse(col, COLOR_SIZE, COLOR_SIZE, args[0], "mathutils.Color()") == -1) {
87 return nullptr;
88 }
89 break;
90 }
91 default: {
92 PyErr_Format(PyExc_TypeError,
93 "mathutils.Color(): "
94 "takes at most 1 argument (%zd given)",
95 nargs);
96 return nullptr;
97 }
98 }
99 return Color_CreatePyObject(col, reinterpret_cast<PyTypeObject *>(type));
100}
101
102static PyObject *Color_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
103{
104 /* Only called on sub-classes. */
105 if (kwds && PyDict_GET_SIZE(kwds)) [[unlikely]] {
106 PyErr_SetString(PyExc_TypeError,
107 "mathutils.Color(): "
108 "takes no keyword args");
109 return nullptr;
110 }
111 PyObject *const *args_array = &PyTuple_GET_ITEM(args, 0);
112 const size_t args_array_num = PyTuple_GET_SIZE(args);
113 return Color_vectorcall(reinterpret_cast<PyObject *>(type), args_array, args_array_num, nullptr);
114}
115
117
118/* -------------------------------------------------------------------- */
121
122#ifndef MATH_STANDALONE
123
125 /* Wrap. */
126 Color_from_scene_linear_to_srgb_doc,
127 ".. method:: from_scene_linear_to_srgb()\n"
128 "\n"
129 " Convert from scene linear to sRGB color space.\n"
130 "\n"
131 " :return: A color in sRGB color space.\n"
132 " :rtype: :class:`Color`\n");
134{
135 float col[3];
137 return Color_CreatePyObject(col, Py_TYPE(self));
138}
139
141 /* Wrap. */
142 Color_from_srgb_to_scene_linear_doc,
143 ".. method:: from_srgb_to_scene_linear()\n"
144 "\n"
145 " Convert from sRGB to scene linear color space.\n"
146 "\n"
147 " :return: A color in scene linear color space.\n"
148 " :rtype: :class:`Color`\n");
150{
151 float col[3];
153 return Color_CreatePyObject(col, Py_TYPE(self));
154}
155
157 /* Wrap. */
158 Color_from_scene_linear_to_xyz_d65_doc,
159 ".. method:: from_scene_linear_to_xyz_d65()\n"
160 "\n"
161 " Convert from scene linear to CIE XYZ (Illuminant D65) color space.\n"
162 "\n"
163 " :return: A color in XYZ color space.\n"
164 " :rtype: :class:`Color`\n");
166{
167 float col[3];
169 return Color_CreatePyObject(col, Py_TYPE(self));
170}
171
173 /* Wrap. */
174 Color_from_xyz_d65_to_scene_linear_doc,
175 ".. method:: from_xyz_d65_to_scene_linear()\n"
176 "\n"
177 " Convert from CIE XYZ (Illuminant D65) to scene linear color space.\n"
178 "\n"
179 " :return: A color in scene linear color space.\n"
180 " :rtype: :class:`Color`\n");
182{
183 float col[3];
185 return Color_CreatePyObject(col, Py_TYPE(self));
186}
187
189 /* Wrap. */
190 Color_from_scene_linear_to_aces_doc,
191 ".. method:: from_scene_linear_to_aces()\n"
192 "\n"
193 " Convert from scene linear to ACES2065-1 linear color space.\n"
194 "\n"
195 " :return: A color in ACES2065-1 linear color space.\n"
196 " :rtype: :class:`Color`\n");
198{
199 float col[3];
201 return Color_CreatePyObject(col, Py_TYPE(self));
202}
203
205 /* Wrap. */
206 Color_from_aces_to_scene_linear_doc,
207 ".. method:: from_aces_to_scene_linear()\n"
208 "\n"
209 " Convert from ACES2065-1 linear to scene linear color space.\n"
210 "\n"
211 " :return: A color in scene linear color space.\n"
212 " :rtype: :class:`Color`\n");
214{
215 float col[3];
217 return Color_CreatePyObject(col, Py_TYPE(self));
218}
219
221 /* Wrap. */
222 Color_from_scene_linear_to_acescg_doc,
223 ".. method:: from_scene_linear_to_acescg()\n"
224 "\n"
225 " Convert from scene linear to ACEScg linear color space.\n"
226 "\n"
227 " :return: A color in ACEScg linear color space.\n"
228 " :rtype: :class:`Color`\n");
230{
231 float col[3];
233 return Color_CreatePyObject(col, Py_TYPE(self));
234}
235
237 /* Wrap. */
238 Color_from_acescg_to_scene_linear_doc,
239 ".. method:: from_acescg_to_scene_linear()\n"
240 "\n"
241 " Convert from ACEScg linear to scene linear color space.\n"
242 "\n"
243 " :return: A color in scene linear color space.\n"
244 " :rtype: :class:`Color`\n");
246{
247 float col[3];
249 return Color_CreatePyObject(col, Py_TYPE(self));
250}
251
253 /* Wrap. */
254 Color_from_scene_linear_to_rec709_linear_doc,
255 ".. method:: from_scene_linear_to_rec709_linear()\n"
256 "\n"
257 " Convert from scene linear to Rec.709 linear color space.\n"
258 "\n"
259 " :return: A color in Rec.709 linear color space.\n"
260 " :rtype: :class:`Color`\n");
262{
263 float col[3];
265 return Color_CreatePyObject(col, Py_TYPE(self));
266}
267
269 /* Wrap. */
270 Color_from_rec709_linear_to_scene_linear_doc,
271 ".. method:: from_rec709_linear_to_scene_linear()\n"
272 "\n"
273 " Convert from Rec.709 linear color space to scene linear color space.\n"
274 "\n"
275 " :return: A color in scene linear color space.\n"
276 " :rtype: :class:`Color`\n");
278{
279 float col[3];
281 return Color_CreatePyObject(col, Py_TYPE(self));
282}
283
285 /* Wrap. */
286 Color_from_scene_linear_to_rec2020_linear_doc,
287 ".. method:: from_scene_linear_to_rec2020_linear()\n"
288 "\n"
289 " Convert from scene linear to Rec.2020 linear color space.\n"
290 "\n"
291 " :return: A color in Rec.2020 linear color space.\n"
292 " :rtype: :class:`Color`\n");
294{
295 float col[3];
297 return Color_CreatePyObject(col, Py_TYPE(self));
298}
299
301 /* Wrap. */
302 Color_from_rec2020_linear_to_scene_linear_doc,
303 ".. method:: from_rec2020_linear_to_scene_linear()\n"
304 "\n"
305 " Convert from Rec.2020 linear color space to scene linear color space.\n"
306 "\n"
307 " :return: A color in scene linear color space.\n"
308 " :rtype: :class:`Color`\n");
310{
311 float col[3];
313 return Color_CreatePyObject(col, Py_TYPE(self));
314}
315
316#endif /* !MATH_STANDALONE */
317
319
320/* -------------------------------------------------------------------- */
323
325 /* Wrap. */
326 Color_copy_doc,
327 ".. method:: copy()\n"
328 "\n"
329 " Returns a copy of this color.\n"
330 "\n"
331 " :return: A copy of the color.\n"
332 " :rtype: :class:`Color`\n"
333 "\n"
334 " .. note:: use this to get a copy of a wrapped color with\n"
335 " no reference to the original data.\n");
336static PyObject *Color_copy(ColorObject *self)
337{
338 if (BaseMath_ReadCallback(self) == -1) {
339 return nullptr;
340 }
341
342 return Color_CreatePyObject(self->col, Py_TYPE(self));
343}
344static PyObject *Color_deepcopy(ColorObject *self, PyObject *args)
345{
346 if (!PyC_CheckArgs_DeepCopy(args)) {
347 return nullptr;
348 }
349 return Color_copy(self);
350}
351
353
354/* -------------------------------------------------------------------- */
357
358static PyObject *Color_repr(ColorObject *self)
359{
360 PyObject *ret, *tuple;
361
362 if (BaseMath_ReadCallback(self) == -1) {
363 return nullptr;
364 }
365
366 tuple = Color_to_tuple_ex(self, -1);
367
368 ret = PyUnicode_FromFormat("Color(%R)", tuple);
369
370 Py_DECREF(tuple);
371 return ret;
372}
373
374#ifndef MATH_STANDALONE
375static PyObject *Color_str(ColorObject *self)
376{
377 DynStr *ds;
378
379 if (BaseMath_ReadCallback(self) == -1) {
380 return nullptr;
381 }
382
383 ds = BLI_dynstr_new();
384
386 ds, "<Color (r=%.4f, g=%.4f, b=%.4f)>", self->col[0], self->col[1], self->col[2]);
387
388 return mathutils_dynstr_to_py(ds); /* frees ds */
389}
390#endif
391
393
394/* -------------------------------------------------------------------- */
397
398static int Color_getbuffer(PyObject *obj, Py_buffer *view, int flags)
399{
400 ColorObject *self = reinterpret_cast<ColorObject *>(obj);
401 if (BaseMath_Prepare_ForBufferAccess(self, view, flags) == -1) [[unlikely]] {
402 return -1;
403 }
404 if (BaseMath_ReadCallback(self) == -1) [[unlikely]] {
405 return -1;
406 }
407
408 memset(view, 0, sizeof(*view));
409
410 view->obj = reinterpret_cast<PyObject *>(self);
411 view->buf = static_cast<void *>(self->col);
412 view->len = Py_ssize_t(COLOR_SIZE * sizeof(float));
413 view->itemsize = sizeof(float);
414 view->ndim = 1;
415 if ((flags & PyBUF_WRITABLE) == 0) {
416 view->readonly = 1;
417 }
418 if (flags & PyBUF_FORMAT) {
419 view->format = const_cast<char *>("f");
420 }
421
422 self->flag |= BASE_MATH_FLAG_HAS_BUFFER_VIEW;
423
424 Py_INCREF(self);
425 return 0;
426}
427
428static void Color_releasebuffer(PyObject * /*exporter*/, Py_buffer *view)
429{
430 ColorObject *self = reinterpret_cast<ColorObject *>(view->obj);
432
433 if (view->readonly == 0) {
434 if (BaseMath_WriteCallback(self) == -1) [[unlikely]] {
435 PyErr_Print();
436 }
437 }
438}
439
440static PyBufferProcs Color_as_buffer = {
441 static_cast<getbufferproc>(Color_getbuffer),
442 static_cast<releasebufferproc>(Color_releasebuffer),
443};
444
446
447/* -------------------------------------------------------------------- */
450
451static PyObject *Color_richcmpr(PyObject *a, PyObject *b, int op)
452{
453 PyObject *res;
454 int ok = -1; /* zero is true */
455
457 ColorObject *colA = reinterpret_cast<ColorObject *>(a);
458 ColorObject *colB = reinterpret_cast<ColorObject *>(b);
459
460 if (BaseMath_ReadCallback(colA) == -1 || BaseMath_ReadCallback(colB) == -1) {
461 return nullptr;
462 }
463
464 ok = EXPP_VectorsAreEqual(colA->col, colB->col, COLOR_SIZE, 1) ? 0 : -1;
465 }
466
467 switch (op) {
468 case Py_NE: {
469 ok = !ok;
471 }
472 case Py_EQ: {
473 res = ok ? Py_False : Py_True;
474 break;
475 }
476 case Py_LT:
477 case Py_LE:
478 case Py_GT:
479 case Py_GE: {
480 res = Py_NotImplemented;
481 break;
482 }
483 default: {
484 PyErr_BadArgument();
485 return nullptr;
486 }
487 }
488
489 return Py_NewRef(res);
490}
491
493
494/* -------------------------------------------------------------------- */
497
498static Py_hash_t Color_hash(ColorObject *self)
499{
500 if (BaseMath_ReadCallback(self) == -1) {
501 return -1;
502 }
503
504 if (BaseMathObject_Prepare_ForHash(self) == -1) {
505 return -1;
506 }
507
508 return mathutils_array_hash(self->col, COLOR_SIZE);
509}
510
512
513/* -------------------------------------------------------------------- */
516
518static Py_ssize_t Color_len(ColorObject * /*self*/)
519{
520 return COLOR_SIZE;
521}
522
524static PyObject *Color_item(ColorObject *self, Py_ssize_t i)
525{
526 if (i < 0 || i >= COLOR_SIZE) {
527 PyErr_SetString(PyExc_IndexError,
528 "color[item]: "
529 "array index out of range");
530 return nullptr;
531 }
532
533 if (BaseMath_ReadIndexCallback(self, i) == -1) {
534 return nullptr;
535 }
536
537 return PyFloat_FromDouble(self->col[i]);
538}
539
541static int Color_ass_item(ColorObject *self, Py_ssize_t i, PyObject *value)
542{
543 float f;
544
545 if (BaseMath_Prepare_ForWrite(self) == -1) {
546 return -1;
547 }
548
549 f = PyFloat_AsDouble(value);
550 if (f == -1 && PyErr_Occurred()) { /* parsed item not a number */
551 PyErr_SetString(PyExc_TypeError,
552 "color[item] = x: "
553 "assigned value not a number");
554 return -1;
555 }
556
557 if (i < 0 || i >= COLOR_SIZE) {
558 PyErr_SetString(PyExc_IndexError,
559 "color[item] = x: "
560 "array assignment index out of range");
561 return -1;
562 }
563
564 self->col[i] = f;
565
566 if (BaseMath_WriteIndexCallback(self, i) == -1) {
567 return -1;
568 }
569
570 return 0;
571}
572
574static PyObject *Color_slice(ColorObject *self,
575 Py_ssize_t start,
576 Py_ssize_t step,
577 Py_ssize_t slice_length)
578{
579 if (BaseMath_ReadCallback(self) == -1) {
580 return nullptr;
581 }
582
583 PyObject *tuple = PyTuple_New(slice_length);
584 Py_ssize_t index = start;
585 for (Py_ssize_t i = 0; i < slice_length; i++, index += step) {
586 BLI_assert(index >= 0 && index < COLOR_SIZE);
587 PyTuple_SET_ITEM(tuple, i, PyFloat_FromDouble(self->col[index]));
588 }
589 return tuple;
590}
591
598 ColorObject *self, Py_ssize_t start, Py_ssize_t step, Py_ssize_t slice_length, PyObject *seq)
599{
600 float col[COLOR_SIZE];
601
602 /* Subset writes merge into existing values, so sync the source first. */
603 if (mathutils_slice_is_subset(start, step, slice_length, COLOR_SIZE)) {
604 if (BaseMath_ReadCallback_ForWrite(self) == -1) {
605 return -1;
606 }
607 }
608 else {
609 if (BaseMath_Prepare_ForWrite(self) == -1) {
610 return -1;
611 }
612 }
613
615 col, slice_length, slice_length, seq, "mathutils.Color[slice] = seq") == -1)
616 {
617 return -1;
618 }
619
620 Py_ssize_t index = start;
621 for (Py_ssize_t i = 0; i < slice_length; i++, index += step) {
622 BLI_assert(index >= 0 && index < COLOR_SIZE);
623 self->col[index] = col[i];
624 }
625
626 (void)BaseMath_WriteCallback(self);
627 return 0;
628}
629
631static PyObject *Color_subscript(ColorObject *self, PyObject *item)
632{
633 if (PyIndex_Check(item)) {
634 Py_ssize_t i;
635 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
636 if (i == -1 && PyErr_Occurred()) {
637 return nullptr;
638 }
639 if (i < 0) {
640 i += COLOR_SIZE;
641 }
642 return Color_item(self, i);
643 }
644 if (PySlice_Check(item)) {
645 Py_ssize_t start, stop, step, slice_length;
646
647 if (PySlice_GetIndicesEx(item, COLOR_SIZE, &start, &stop, &step, &slice_length) < 0) {
648 return nullptr;
649 }
650
651 return Color_slice(self, start, step, slice_length);
652 }
653
654 PyErr_Format(
655 PyExc_TypeError, "color indices must be integers, not %.200s", Py_TYPE(item)->tp_name);
656 return nullptr;
657}
658
660static int Color_ass_subscript(ColorObject *self, PyObject *item, PyObject *value)
661{
662 if (PyIndex_Check(item)) {
663 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
664 if (i == -1 && PyErr_Occurred()) {
665 return -1;
666 }
667 if (i < 0) {
668 i += COLOR_SIZE;
669 }
670 return Color_ass_item(self, i, value);
671 }
672 if (PySlice_Check(item)) {
673 Py_ssize_t start, stop, step, slice_length;
674
675 if (PySlice_GetIndicesEx(item, COLOR_SIZE, &start, &stop, &step, &slice_length) < 0) {
676 return -1;
677 }
678
679 return Color_ass_slice(self, start, step, slice_length, value);
680 }
681
682 PyErr_Format(
683 PyExc_TypeError, "color indices must be integers, not %.200s", Py_TYPE(item)->tp_name);
684 return -1;
685}
686
688
689/* -------------------------------------------------------------------- */
692
694static PyObject *Color_add(PyObject *v1, PyObject *v2)
695{
696 ColorObject *color1 = nullptr, *color2 = nullptr;
697 float col[COLOR_SIZE];
698
699 if (!ColorObject_Check(v1) || !ColorObject_Check(v2)) {
700 PyErr_Format(PyExc_TypeError,
701 "Color addition: (%s + %s) "
702 "invalid type for this operation",
703 Py_TYPE(v1)->tp_name,
704 Py_TYPE(v2)->tp_name);
705 return nullptr;
706 }
707 color1 = reinterpret_cast<ColorObject *>(v1);
708 color2 = reinterpret_cast<ColorObject *>(v2);
709
710 if (BaseMath_ReadCallback(color1) == -1 || BaseMath_ReadCallback(color2) == -1) {
711 return nullptr;
712 }
713
714 add_vn_vnvn(col, color1->col, color2->col, COLOR_SIZE);
715
716 return Color_CreatePyObject(col, Py_TYPE(v1));
717}
718
720static PyObject *Color_iadd(PyObject *v1, PyObject *v2)
721{
722 ColorObject *color1 = nullptr, *color2 = nullptr;
723
724 if (!ColorObject_Check(v1) || !ColorObject_Check(v2)) {
725 PyErr_Format(PyExc_TypeError,
726 "Color addition: (%s += %s) "
727 "invalid type for this operation",
728 Py_TYPE(v1)->tp_name,
729 Py_TYPE(v2)->tp_name);
730 return nullptr;
731 }
732 color1 = reinterpret_cast<ColorObject *>(v1);
733 color2 = reinterpret_cast<ColorObject *>(v2);
734
735 if (BaseMath_ReadCallback_ForWrite(color1) == -1 || BaseMath_ReadCallback(color2) == -1) {
736 return nullptr;
737 }
738
739 add_vn_vn(color1->col, color2->col, COLOR_SIZE);
740
741 (void)BaseMath_WriteCallback(color1);
742 Py_INCREF(v1);
743 return v1;
744}
745
747static PyObject *Color_sub(PyObject *v1, PyObject *v2)
748{
749 ColorObject *color1 = nullptr, *color2 = nullptr;
750 float col[COLOR_SIZE];
751
752 if (!ColorObject_Check(v1) || !ColorObject_Check(v2)) {
753 PyErr_Format(PyExc_TypeError,
754 "Color subtraction: (%s - %s) "
755 "invalid type for this operation",
756 Py_TYPE(v1)->tp_name,
757 Py_TYPE(v2)->tp_name);
758 return nullptr;
759 }
760 color1 = reinterpret_cast<ColorObject *>(v1);
761 color2 = reinterpret_cast<ColorObject *>(v2);
762
763 if (BaseMath_ReadCallback(color1) == -1 || BaseMath_ReadCallback(color2) == -1) {
764 return nullptr;
765 }
766
767 sub_vn_vnvn(col, color1->col, color2->col, COLOR_SIZE);
768
769 return Color_CreatePyObject(col, Py_TYPE(v1));
770}
771
773static PyObject *Color_isub(PyObject *v1, PyObject *v2)
774{
775 ColorObject *color1 = nullptr, *color2 = nullptr;
776
777 if (!ColorObject_Check(v1) || !ColorObject_Check(v2)) {
778 PyErr_Format(PyExc_TypeError,
779 "Color subtraction: (%s -= %s) "
780 "invalid type for this operation",
781 Py_TYPE(v1)->tp_name,
782 Py_TYPE(v2)->tp_name);
783 return nullptr;
784 }
785 color1 = reinterpret_cast<ColorObject *>(v1);
786 color2 = reinterpret_cast<ColorObject *>(v2);
787
788 if (BaseMath_ReadCallback_ForWrite(color1) == -1 || BaseMath_ReadCallback(color2) == -1) {
789 return nullptr;
790 }
791
792 sub_vn_vn(color1->col, color2->col, COLOR_SIZE);
793
794 (void)BaseMath_WriteCallback(color1);
795 Py_INCREF(v1);
796 return v1;
797}
798
799static PyObject *color_mul_float(ColorObject *color, const float scalar)
800{
801 float tcol[COLOR_SIZE];
802 mul_vn_vn_fl(tcol, color->col, COLOR_SIZE, scalar);
803 return Color_CreatePyObject(tcol, Py_TYPE(color));
804}
805
807static PyObject *Color_mul(PyObject *v1, PyObject *v2)
808{
809 ColorObject *color1 = nullptr, *color2 = nullptr;
810 float scalar;
811
812 if (ColorObject_Check(v1)) {
813 color1 = reinterpret_cast<ColorObject *>(v1);
814 if (BaseMath_ReadCallback(color1) == -1) {
815 return nullptr;
816 }
817 }
818 if (ColorObject_Check(v2)) {
819 color2 = reinterpret_cast<ColorObject *>(v2);
820 if (BaseMath_ReadCallback(color2) == -1) {
821 return nullptr;
822 }
823 }
824
825 /* make sure v1 is always the vector */
826 if (color1 && color2) {
827 /* col * col, don't support yet! */
828 }
829 else if (color1) {
830 if (((scalar = PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) == 0) { /* COLOR * FLOAT */
831 return color_mul_float(color1, scalar);
832 }
833 }
834 else if (color2) {
835 if (((scalar = PyFloat_AsDouble(v1)) == -1.0f && PyErr_Occurred()) == 0) { /* FLOAT * COLOR */
836 return color_mul_float(color2, scalar);
837 }
838 }
839 else {
840 BLI_assert_msg(0, "internal error");
841 }
842
843 PyErr_Format(PyExc_TypeError,
844 "Color multiplication: not supported between "
845 "'%.200s' and '%.200s' types",
846 Py_TYPE(v1)->tp_name,
847 Py_TYPE(v2)->tp_name);
848 return nullptr;
849}
850
852static PyObject *Color_div(PyObject *v1, PyObject *v2)
853{
854 ColorObject *color1 = nullptr;
855 float scalar;
856
857 if (ColorObject_Check(v1)) {
858 color1 = reinterpret_cast<ColorObject *>(v1);
859 if (BaseMath_ReadCallback(color1) == -1) {
860 return nullptr;
861 }
862 }
863 else {
864 PyErr_SetString(PyExc_TypeError, "Color division not supported in this order");
865 return nullptr;
866 }
867
868 /* make sure v1 is always the vector */
869 if (((scalar = PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) == 0) { /* COLOR * FLOAT */
870 if (scalar == 0.0f) {
871 PyErr_SetString(PyExc_ZeroDivisionError, "Color division: divide by zero error");
872 return nullptr;
873 }
874 return color_mul_float(color1, 1.0f / scalar);
875 }
876
877 PyErr_Format(PyExc_TypeError,
878 "Color multiplication: not supported between "
879 "'%.200s' and '%.200s' types",
880 Py_TYPE(v1)->tp_name,
881 Py_TYPE(v2)->tp_name);
882 return nullptr;
883}
884
886static PyObject *Color_imul(PyObject *v1, PyObject *v2)
887{
888 ColorObject *color = reinterpret_cast<ColorObject *>(v1);
889 float scalar;
890
892 return nullptr;
893 }
894
895 /* only support color *= float */
896 if (((scalar = PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) == 0) { /* COLOR *= FLOAT */
897 mul_vn_fl(color->col, COLOR_SIZE, scalar);
898 }
899 else {
900 PyErr_Format(PyExc_TypeError,
901 "Color multiplication: (%s *= %s) "
902 "invalid type for this operation",
903 Py_TYPE(v1)->tp_name,
904 Py_TYPE(v2)->tp_name);
905 return nullptr;
906 }
907
909 Py_INCREF(v1);
910 return v1;
911}
912
914static PyObject *Color_idiv(PyObject *v1, PyObject *v2)
915{
916 ColorObject *color = reinterpret_cast<ColorObject *>(v1);
917 float scalar;
918
920 return nullptr;
921 }
922
923 /* only support color /= float */
924 if (((scalar = PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) == 0) { /* COLOR /= FLOAT */
925 if (scalar == 0.0f) {
926 PyErr_SetString(PyExc_ZeroDivisionError, "Color division: divide by zero error");
927 return nullptr;
928 }
929
930 mul_vn_fl(color->col, COLOR_SIZE, 1.0f / scalar);
931 }
932 else {
933 PyErr_Format(PyExc_TypeError,
934 "Color division: (%s /= %s) "
935 "invalid type for this operation",
936 Py_TYPE(v1)->tp_name,
937 Py_TYPE(v2)->tp_name);
938 return nullptr;
939 }
940
942 Py_INCREF(v1);
943 return v1;
944}
945
947static PyObject *Color_neg(ColorObject *self)
948{
949 float tcol[COLOR_SIZE];
950
951 if (BaseMath_ReadCallback(self) == -1) {
952 return nullptr;
953 }
954
955 negate_vn_vn(tcol, self->col, COLOR_SIZE);
956 return Color_CreatePyObject(tcol, Py_TYPE(self));
957}
958
960
961/* -------------------------------------------------------------------- */
964
965static PySequenceMethods Color_SeqMethods = {
966 /*sq_length*/ reinterpret_cast<lenfunc>(Color_len),
967 /*sq_concat*/ nullptr,
968 /*sq_repeat*/ nullptr,
969 /*sq_item*/ reinterpret_cast<ssizeargfunc>(Color_item),
970 /*was_sq_slice*/ nullptr, /* DEPRECATED. */
971 /*sq_ass_item*/ reinterpret_cast<ssizeobjargproc>(Color_ass_item),
972 /*was_sq_ass_slice*/ nullptr, /* DEPRECATED. */
973 /*sq_contains*/ nullptr,
974 /*sq_inplace_concat*/ nullptr,
975 /*sq_inplace_repeat*/ nullptr,
976};
977
978static PyMappingMethods Color_AsMapping = {
979 /*mp_length*/ reinterpret_cast<lenfunc>(Color_len),
980 /*mp_subscript*/ reinterpret_cast<binaryfunc>(Color_subscript),
981 /*mp_ass_subscript*/ reinterpret_cast<objobjargproc>(Color_ass_subscript),
982};
983
984static PyNumberMethods Color_NumMethods = {
985 /*nb_add*/ static_cast<binaryfunc>(Color_add),
986 /*nb_subtract*/ static_cast<binaryfunc>(Color_sub),
987 /*nb_multiply*/ static_cast<binaryfunc>(Color_mul),
988 /*nb_remainder*/ nullptr,
989 /*nb_divmod*/ nullptr,
990 /*nb_power*/ nullptr,
991 /*nb_negative*/ reinterpret_cast<unaryfunc>(Color_neg),
992 /*nb_positive*/ reinterpret_cast<unaryfunc>(Color_copy),
993 /*nb_absolute*/ nullptr,
994 /*nb_bool*/ nullptr,
995 /*nb_invert*/ nullptr,
996 /*nb_lshift*/ nullptr,
997 /*nb_rshift*/ nullptr,
998 /*nb_and*/ nullptr,
999 /*nb_xor*/ nullptr,
1000 /*nb_or*/ nullptr,
1001 /*nb_int*/ nullptr,
1002 /*nb_reserved*/ nullptr,
1003 /*nb_float*/ nullptr,
1004 /*nb_inplace_add*/ Color_iadd,
1005 /*nb_inplace_subtract*/ Color_isub,
1006 /*nb_inplace_multiply*/ Color_imul,
1007 /*nb_inplace_remainder*/ nullptr,
1008 /*nb_inplace_power*/ nullptr,
1009 /*nb_inplace_lshift*/ nullptr,
1010 /*nb_inplace_rshift*/ nullptr,
1011 /*nb_inplace_and*/ nullptr,
1012 /*nb_inplace_xor*/ nullptr,
1013 /*nb_inplace_or*/ nullptr,
1014 /*nb_floor_divide*/ nullptr,
1015 /*nb_true_divide*/ Color_div,
1016 /*nb_inplace_floor_divide*/ nullptr,
1017 /*nb_inplace_true_divide*/ Color_idiv,
1018 /*nb_index*/ nullptr,
1019 /*nb_matrix_multiply*/ nullptr,
1020 /*nb_inplace_matrix_multiply*/ nullptr,
1021};
1022
1024
1025/* -------------------------------------------------------------------- */
1028
1029/* Color channel (RGB): `color.r/g/b`. */
1030
1032 /* Wrap. */
1033 Color_channel_r_doc,
1034 "Red color channel.\n"
1035 "\n"
1036 ":type: float\n");
1038 /* Wrap. */
1039 Color_channel_g_doc,
1040 "Green color channel.\n"
1041 "\n"
1042 ":type: float\n");
1044 /* Wrap. */
1045 Color_channel_b_doc,
1046 "Blue color channel.\n"
1047 "\n"
1048 ":type: float\n");
1049
1050static PyObject *Color_channel_get(ColorObject *self, void *type)
1051{
1052 return Color_item(self, POINTER_AS_INT(type));
1053}
1054
1055static int Color_channel_set(ColorObject *self, PyObject *value, void *type)
1056{
1057 return Color_ass_item(self, POINTER_AS_INT(type), value);
1058}
1059
1060/* Color channel (HSV): `color.h/s/v`. */
1061
1063 /* Wrap. */
1064 Color_channel_hsv_h_doc,
1065 "HSV Hue component in [0, 1].\n"
1066 "\n"
1067 ":type: float\n");
1069 /* Wrap. */
1070 Color_channel_hsv_s_doc,
1071 "HSV Saturation component in [0, 1].\n"
1072 "\n"
1073 ":type: float\n");
1075 /* Wrap. */
1076 Color_channel_hsv_v_doc,
1077 "HSV Value component in [0, 1].\n"
1078 "\n"
1079 ":type: float\n");
1080
1081static PyObject *Color_channel_hsv_get(ColorObject *self, void *type)
1082{
1083 float hsv[3];
1084 const int i = POINTER_AS_INT(type);
1085
1086 if (BaseMath_ReadCallback(self) == -1) {
1087 return nullptr;
1088 }
1089
1090 rgb_to_hsv(self->col[0], self->col[1], self->col[2], &(hsv[0]), &(hsv[1]), &(hsv[2]));
1091
1092 return PyFloat_FromDouble(hsv[i]);
1093}
1094
1095static int Color_channel_hsv_set(ColorObject *self, PyObject *value, void *type)
1096{
1097 float hsv[3];
1098 const int i = POINTER_AS_INT(type);
1099 float f = PyFloat_AsDouble(value);
1100
1101 if (f == -1 && PyErr_Occurred()) {
1102 PyErr_SetString(PyExc_TypeError,
1103 "color.h/s/v = value: "
1104 "assigned value not a number");
1105 return -1;
1106 }
1107
1108 if (BaseMath_ReadCallback_ForWrite(self) == -1) {
1109 return -1;
1110 }
1111
1112 rgb_to_hsv_v(self->col, hsv);
1113 CLAMP(f, 0.0f, 1.0f);
1114 hsv[i] = f;
1115 hsv_to_rgb_v(hsv, self->col);
1116
1117 if (BaseMath_WriteCallback(self) == -1) {
1118 return -1;
1119 }
1120
1121 return 0;
1122}
1123
1125 /* Wrap. */
1126 Color_hsv_doc,
1127 "HSV Values in [0, 1].\n"
1128 "\n"
1129 ":type: tuple[float, float, float]\n");
1131static PyObject *Color_hsv_get(ColorObject *self, void * /*closure*/)
1132{
1133 float hsv[3];
1134 PyObject *ret;
1135
1136 if (BaseMath_ReadCallback(self) == -1) {
1137 return nullptr;
1138 }
1139
1140 rgb_to_hsv(self->col[0], self->col[1], self->col[2], &(hsv[0]), &(hsv[1]), &(hsv[2]));
1141
1142 ret = PyTuple_New(3);
1144 ret, PyFloat_FromDouble(hsv[0]), PyFloat_FromDouble(hsv[1]), PyFloat_FromDouble(hsv[2]));
1145 return ret;
1146}
1147
1149static int Color_hsv_set(ColorObject *self, PyObject *value, void * /*closure*/)
1150{
1151 float hsv[3];
1152
1153 if (mathutils_array_parse(hsv, 3, 3, value, "mathutils.Color.hsv = value") == -1) {
1154 return -1;
1155 }
1156
1157 if (BaseMath_Prepare_ForWrite(self) == -1) {
1158 return -1;
1159 }
1160
1161 clamp_v3(hsv, 0.0f, 1.0f);
1162 hsv_to_rgb_v(hsv, self->col);
1163
1164 if (BaseMath_WriteCallback(self) == -1) {
1165 return -1;
1166 }
1167
1168 return 0;
1169}
1170
1172
1173/* -------------------------------------------------------------------- */
1176
1177static PyGetSetDef Color_getseters[] = {
1178 {"r",
1179 reinterpret_cast<getter>(Color_channel_get),
1180 reinterpret_cast<setter>(Color_channel_set),
1181 Color_channel_r_doc,
1182 POINTER_FROM_INT(0)},
1183 {"g",
1184 reinterpret_cast<getter>(Color_channel_get),
1185 reinterpret_cast<setter>(Color_channel_set),
1186 Color_channel_g_doc,
1187 POINTER_FROM_INT(1)},
1188 {"b",
1189 reinterpret_cast<getter>(Color_channel_get),
1190 reinterpret_cast<setter>(Color_channel_set),
1191 Color_channel_b_doc,
1192 POINTER_FROM_INT(2)},
1193
1194 {"h",
1195 reinterpret_cast<getter>(Color_channel_hsv_get),
1196 reinterpret_cast<setter>(Color_channel_hsv_set),
1197 Color_channel_hsv_h_doc,
1198 POINTER_FROM_INT(0)},
1199 {"s",
1200 reinterpret_cast<getter>(Color_channel_hsv_get),
1201 reinterpret_cast<setter>(Color_channel_hsv_set),
1202 Color_channel_hsv_s_doc,
1203 POINTER_FROM_INT(1)},
1204 {"v",
1205 reinterpret_cast<getter>(Color_channel_hsv_get),
1206 reinterpret_cast<setter>(Color_channel_hsv_set),
1207 Color_channel_hsv_v_doc,
1208 POINTER_FROM_INT(2)},
1209
1210 {"hsv",
1211 reinterpret_cast<getter>(Color_hsv_get),
1212 reinterpret_cast<setter>(Color_hsv_set),
1213 Color_hsv_doc,
1214 nullptr},
1215
1216 {"is_wrapped",
1217 reinterpret_cast<getter>(BaseMathObject_is_wrapped_get),
1218 static_cast<setter>(nullptr),
1220 nullptr},
1221 {"is_frozen",
1222 reinterpret_cast<getter>(BaseMathObject_is_frozen_get),
1223 static_cast<setter>(nullptr),
1225 nullptr},
1226 {"is_valid",
1227 reinterpret_cast<getter>(BaseMathObject_is_valid_get),
1228 static_cast<setter>(nullptr),
1230 nullptr},
1231 {"owner",
1232 reinterpret_cast<getter>(BaseMathObject_owner_get),
1233 static_cast<setter>(nullptr),
1235 nullptr},
1236 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
1237};
1238
1240
1241/* -------------------------------------------------------------------- */
1244
1245#ifdef __GNUC__
1246# ifdef __clang__
1247# pragma clang diagnostic push
1248# pragma clang diagnostic ignored "-Wcast-function-type"
1249# else
1250# pragma GCC diagnostic push
1251# pragma GCC diagnostic ignored "-Wcast-function-type"
1252# endif
1253#endif
1254
1255static PyMethodDef Color_methods[] = {
1256 {"copy", reinterpret_cast<PyCFunction>(Color_copy), METH_NOARGS, Color_copy_doc},
1257 {"__copy__", reinterpret_cast<PyCFunction>(Color_copy), METH_NOARGS, Color_copy_doc},
1258 {"__deepcopy__", reinterpret_cast<PyCFunction>(Color_deepcopy), METH_VARARGS, Color_copy_doc},
1259
1260 /* base-math methods */
1261 {"freeze",
1262 reinterpret_cast<PyCFunction>(BaseMathObject_freeze),
1263 METH_NOARGS,
1265
1266/* Color-space methods. */
1267#ifndef MATH_STANDALONE
1268 {"from_scene_linear_to_srgb",
1269 reinterpret_cast<PyCFunction>(Color_from_scene_linear_to_srgb),
1270 METH_NOARGS,
1271 Color_from_scene_linear_to_srgb_doc},
1272 {"from_srgb_to_scene_linear",
1273 reinterpret_cast<PyCFunction>(Color_from_srgb_to_scene_linear),
1274 METH_NOARGS,
1275 Color_from_srgb_to_scene_linear_doc},
1276 {"from_scene_linear_to_xyz_d65",
1277 reinterpret_cast<PyCFunction>(Color_from_scene_linear_to_xyz_d65),
1278 METH_NOARGS,
1279 Color_from_scene_linear_to_xyz_d65_doc},
1280 {"from_xyz_d65_to_scene_linear",
1281 reinterpret_cast<PyCFunction>(Color_from_xyz_d65_to_scene_linear),
1282 METH_NOARGS,
1283 Color_from_xyz_d65_to_scene_linear_doc},
1284 {"from_scene_linear_to_aces",
1285 reinterpret_cast<PyCFunction>(Color_from_scene_linear_to_aces),
1286 METH_NOARGS,
1287 Color_from_scene_linear_to_aces_doc},
1288 {"from_aces_to_scene_linear",
1289 reinterpret_cast<PyCFunction>(Color_from_aces_to_scene_linear),
1290 METH_NOARGS,
1291 Color_from_aces_to_scene_linear_doc},
1292 {"from_scene_linear_to_acescg",
1293 reinterpret_cast<PyCFunction>(Color_from_scene_linear_to_acescg),
1294 METH_NOARGS,
1295 Color_from_scene_linear_to_acescg_doc},
1296 {"from_acescg_to_scene_linear",
1297 reinterpret_cast<PyCFunction>(Color_from_acescg_to_scene_linear),
1298 METH_NOARGS,
1299 Color_from_acescg_to_scene_linear_doc},
1300 {"from_scene_linear_to_rec709_linear",
1301 reinterpret_cast<PyCFunction>(Color_from_scene_linear_to_rec709_linear),
1302 METH_NOARGS,
1303 Color_from_scene_linear_to_rec709_linear_doc},
1304 {"from_rec709_linear_to_scene_linear",
1305 reinterpret_cast<PyCFunction>(Color_from_rec709_linear_to_scene_linear),
1306 METH_NOARGS,
1307 Color_from_rec709_linear_to_scene_linear_doc},
1308 {"from_scene_linear_to_rec2020_linear",
1309 reinterpret_cast<PyCFunction>(Color_from_scene_linear_to_rec2020_linear),
1310 METH_NOARGS,
1311 Color_from_scene_linear_to_rec2020_linear_doc},
1312 {"from_rec2020_linear_to_scene_linear",
1313 reinterpret_cast<PyCFunction>(Color_from_rec2020_linear_to_scene_linear),
1314 METH_NOARGS,
1315 Color_from_rec2020_linear_to_scene_linear_doc},
1316#endif /* !MATH_STANDALONE */
1317
1318 {nullptr, nullptr, 0, nullptr},
1319};
1320
1321#ifdef __GNUC__
1322# ifdef __clang__
1323# pragma clang diagnostic pop
1324# else
1325# pragma GCC diagnostic pop
1326# endif
1327#endif
1328
1330
1331/* -------------------------------------------------------------------- */
1334
1335#ifdef MATH_STANDALONE
1336# define Color_str nullptr
1337#endif
1338
1340 /* Wrap. */
1341 color_doc,
1342 ".. class:: Color(rgb=(0.0, 0.0, 0.0), /)\n"
1343 "\n"
1344 " This object gives access to Colors in Blender.\n"
1345 "\n"
1346 " Most colors returned by Blender APIs are in scene linear color space, as defined by "
1347 "the OpenColorIO configuration. The notable exception is user interface theming colors, "
1348 "which are in sRGB color space.\n"
1349 "\n"
1350 " :param rgb: (red, green, blue) color values "
1351 "where (0, 0, 0) is black & (1, 1, 1) is white.\n"
1352 " :type rgb: Sequence[float]\n");
1353PyTypeObject color_Type = {
1354 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
1355 /*tp_name*/ "Color",
1356 /*tp_basicsize*/ sizeof(ColorObject),
1357 /*tp_itemsize*/ 0,
1358 /*tp_dealloc*/ reinterpret_cast<destructor>(BaseMathObject_dealloc),
1359 /*tp_vectorcall_offset*/ 0,
1360 /*tp_getattr*/ nullptr,
1361 /*tp_setattr*/ nullptr,
1362 /*tp_as_async*/ nullptr,
1363 /*tp_repr*/ reinterpret_cast<reprfunc>(Color_repr),
1364 /*tp_as_number*/ &Color_NumMethods,
1365 /*tp_as_sequence*/ &Color_SeqMethods,
1366 /*tp_as_mapping*/ &Color_AsMapping,
1367 /*tp_hash*/ reinterpret_cast<hashfunc>(Color_hash),
1368 /*tp_call*/ nullptr,
1369 /*tp_str*/ reinterpret_cast<reprfunc>(Color_str),
1370 /*tp_getattro*/ nullptr,
1371 /*tp_setattro*/ nullptr,
1372 /*tp_as_buffer*/ &Color_as_buffer,
1373 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1374 /*tp_doc*/ color_doc,
1375 /*tp_traverse*/ reinterpret_cast<traverseproc>(BaseMathObject_traverse),
1376 /*tp_clear*/ reinterpret_cast<inquiry>(BaseMathObject_clear),
1377 /*tp_richcompare*/ static_cast<richcmpfunc>(Color_richcmpr),
1378 /*tp_weaklistoffset*/ 0,
1379 /*tp_iter*/ nullptr,
1380 /*tp_iternext*/ nullptr,
1381 /*tp_methods*/ Color_methods,
1382 /*tp_members*/ nullptr,
1383 /*tp_getset*/ Color_getseters,
1384 /*tp_base*/ nullptr,
1385 /*tp_dict*/ nullptr,
1386 /*tp_descr_get*/ nullptr,
1387 /*tp_descr_set*/ nullptr,
1388 /*tp_dictoffset*/ 0,
1389 /*tp_init*/ nullptr,
1390 /*tp_alloc*/ nullptr,
1391 /*tp_new*/ Color_new,
1392 /*tp_free*/ nullptr,
1393 /*tp_is_gc*/ reinterpret_cast<inquiry>(BaseMathObject_is_gc),
1394 /*tp_bases*/ nullptr,
1395 /*tp_mro*/ nullptr,
1396 /*tp_cache*/ nullptr,
1397 /*tp_subclasses*/ nullptr,
1398 /*tp_weaklist*/ nullptr,
1399 /*tp_del*/ nullptr,
1400 /*tp_version_tag*/ 0,
1401 /*tp_finalize*/ nullptr,
1402 /*tp_vectorcall*/ Color_vectorcall,
1403};
1404
1405#ifdef MATH_STANDALONE
1406# define Color_str
1407#endif
1408
1410
1411/* -------------------------------------------------------------------- */
1414
1415PyObject *Color_CreatePyObject(const float col[3], PyTypeObject *base_type)
1416{
1417 ColorObject *self;
1418 float *col_alloc;
1419
1420 col_alloc = static_cast<float *>(PyMem_Malloc(COLOR_SIZE * sizeof(float)));
1421 if (col_alloc == nullptr) [[unlikely]] {
1422 PyErr_SetString(PyExc_MemoryError,
1423 "Color(): "
1424 "problem allocating data");
1425 return nullptr;
1426 }
1427
1428 self = BASE_MATH_NEW(ColorObject, color_Type, base_type);
1429 if (self) {
1430 self->col = col_alloc;
1431
1432 /* init callbacks as nullptr */
1433 self->cb_user = nullptr;
1434 self->cb_type = self->cb_subtype = 0;
1435
1436 /* NEW */
1437 if (col) {
1438 copy_v3_v3(self->col, col);
1439 }
1440 else {
1441 zero_v3(self->col);
1442 }
1443
1444 self->flag = BASE_MATH_FLAG_DEFAULT;
1445 }
1446 else {
1447 PyMem_Free(col_alloc);
1448 }
1449
1450 return reinterpret_cast<PyObject *>(self);
1451}
1452
1453PyObject *Color_CreatePyObject_wrap(float col[3], PyTypeObject *base_type)
1454{
1455 ColorObject *self;
1456
1457 self = BASE_MATH_NEW(ColorObject, color_Type, base_type);
1458 if (self) {
1459 /* init callbacks as nullptr */
1460 self->cb_user = nullptr;
1461 self->cb_type = self->cb_subtype = 0;
1462
1463 /* WRAP */
1464 self->col = col;
1466 }
1467
1468 return reinterpret_cast<PyObject *>(self);
1469}
1470
1471PyObject *Color_CreatePyObject_cb(PyObject *cb_user, uchar cb_type, uchar cb_subtype)
1472{
1473 ColorObject *self = reinterpret_cast<ColorObject *>(Color_CreatePyObject(nullptr, nullptr));
1474 if (self) {
1475 Py_INCREF(cb_user);
1476 self->cb_user = cb_user;
1477 self->cb_type = cb_type;
1478 self->cb_subtype = cb_subtype;
1479 BLI_assert(!PyObject_GC_IsTracked((PyObject *)self));
1480 PyObject_GC_Track(self);
1481 }
1482
1483 return reinterpret_cast<PyObject *>(self);
1484}
1485
1487
1488} // namespace blender
#define ATTR_FALLTHROUGH
A dynamically sized string ADT.
#define CLAMP(a, b, c)
#define POINTER_FROM_INT(i)
#define POINTER_AS_INT(i)
#define BLI_assert_msg(a, msg)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
#define BaseMath_ReadCallback_ForWrite(_self)
Definition mathutils.hh:157
#define BaseMath_Prepare_ForBufferAccess(_self, _view, _flags)
Definition mathutils.hh:189
#define BaseMath_ReadIndexCallback(_self, _index)
Definition mathutils.hh:151
#define BaseMath_WriteCallback(_self)
Definition mathutils.hh:149
#define BASE_MATH_NEW(struct_name, root_type, base_type)
Definition mathutils.hh:29
#define BaseMathObject_Prepare_ForHash(_self)
Definition mathutils.hh:172
#define BASE_MATH_FLAG_DEFAULT
Definition mathutils.hh:54
#define BaseMath_Prepare_ForWrite(_self)
Definition mathutils.hh:167
#define BaseMath_ReadCallback(_self)
Definition mathutils.hh:147
#define BaseMath_WriteIndexCallback(_self, _index)
Definition mathutils.hh:153
#define COLOR_SIZE
#define ColorObject_Check(v)
BMesh Iterators.
static void Color_releasebuffer(PyObject *, Py_buffer *view)
char BaseMathObject_owner_doc[]
Definition mathutils.cc:686
static PyObject * Color_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void mul_vn_fl(float *array_tar, int size, float f)
char BaseMathObject_freeze_doc[]
Definition mathutils.cc:716
int EXPP_VectorsAreEqual(const float *vecA, const float *vecB, int size, int floatSteps)
Definition mathutils.cc:511
void BLI_dynstr_appendf(DynStr *__restrict ds, const char *__restrict format,...) ATTR_PRINTF_FORMAT(2
bool mathutils_slice_is_subset(Py_ssize_t start, Py_ssize_t step, Py_ssize_t slice_length, Py_ssize_t array_num)
Definition mathutils.hh:241
static PyObject * Color_isub(PyObject *v1, PyObject *v2)
void BaseMathObject_dealloc(BaseMathObject *self)
Definition mathutils.cc:766
BLI_INLINE void IMB_colormanagement_xyz_to_scene_linear(float scene_linear[3], const float xyz[3])
static int Color_ass_item(ColorObject *self, Py_ssize_t i, PyObject *value)
MINLINE void copy_v3_v3(float r[3], const float a[3])
int PyC_CheckArgs_DeepCopy(PyObject *args)
static PyMappingMethods Color_AsMapping
void sub_vn_vnvn(float *array_tar, const float *array_src_a, const float *array_src_b, int size)
static PyObject * Color_from_srgb_to_scene_linear(ColorObject *self)
static PyObject * Color_imul(PyObject *v1, PyObject *v2)
static int Color_channel_hsv_set(ColorObject *self, PyObject *value, void *type)
static PyObject * Color_from_rec2020_linear_to_scene_linear(ColorObject *self)
static PyObject * Color_subscript(ColorObject *self, PyObject *item)
Py_hash_t mathutils_array_hash(const float *array, size_t array_len)
Definition mathutils.cc:69
void add_vn_vn(float *array_tar, const float *array_src, int size)
static PyObject * Color_from_scene_linear_to_rec2020_linear(ColorObject *self)
PyObject * mathutils_dynstr_to_py(DynStr *ds)
Definition mathutils.cc:523
static PyObject * Color_channel_hsv_get(ColorObject *self, void *type)
void mul_vn_vn_fl(float *array_tar, const float *array_src, int size, float f)
static PyObject * Color_hsv_get(ColorObject *self, void *)
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
PyObject * BaseMathObject_owner_get(BaseMathObject *self, void *)
Definition mathutils.cc:690
static PyObject * Color_channel_get(ColorObject *self, void *type)
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
static PyObject * Color_mul(PyObject *v1, PyObject *v2)
static PyObject * Color_from_scene_linear_to_xyz_d65(ColorObject *self)
static PyObject * Color_add(PyObject *v1, PyObject *v2)
static PyNumberMethods Color_NumMethods
double double_round(double x, int ndigits)
Definition math_base.cc:30
int BaseMathObject_clear(BaseMathObject *self)
Definition mathutils.cc:748
void rgb_to_hsv(float r, float g, float b, float *r_h, float *r_s, float *r_v)
PyObject * BaseMathObject_freeze(BaseMathObject *self)
Definition mathutils.cc:725
static PyObject * Color_iadd(PyObject *v1, PyObject *v2)
BLI_INLINE void IMB_colormanagement_acescg_to_scene_linear(float scene_linear[3], const float acescg[3])
static PyObject * Color_repr(ColorObject *self)
int BaseMathObject_is_gc(BaseMathObject *self)
Definition mathutils.cc:788
static Py_hash_t Color_hash(ColorObject *self)
static PyObject * Color_from_acescg_to_scene_linear(ColorObject *self)
static int Color_channel_set(ColorObject *self, PyObject *value, void *type)
PyObject * BaseMathObject_is_frozen_get(BaseMathObject *self, void *)
Definition mathutils.cc:705
static PyObject * Color_from_scene_linear_to_acescg(ColorObject *self)
static PyGetSetDef Color_getseters[]
static PyObject * Color_to_tuple_ex(ColorObject *self, int ndigits)
PyObject * BaseMathObject_is_valid_get(BaseMathObject *self, void *)
Definition mathutils.cc:711
unsigned char uchar
Definition BKE_text.h:58
static PyObject * color_mul_float(ColorObject *color, const float scalar)
BLI_INLINE void IMB_colormanagement_rec2020_to_scene_linear(float scene_linear[3], const float rec2020[3])
BLI_INLINE void IMB_colormanagement_rec709_to_scene_linear(float scene_linear[3], const float rec709[3])
static PyObject * Color_str(ColorObject *self)
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition math_color.cc:61
static PyObject * Color_from_rec709_linear_to_scene_linear(ColorObject *self)
BLI_INLINE void IMB_colormanagement_scene_linear_to_rec2020(float rec2020[3], const float scene_linear[3])
static int Color_ass_subscript(ColorObject *self, PyObject *item, PyObject *value)
BLI_INLINE void IMB_colormanagement_scene_linear_to_acescg(float acescg[3], const float scene_linear[3])
static PyObject * Color_from_scene_linear_to_rec709_linear(ColorObject *self)
BLI_INLINE void IMB_colormanagement_srgb_to_scene_linear_v3(float scene_linear[3], const float srgb[3])
static PyObject * Color_sub(PyObject *v1, PyObject *v2)
@ BASE_MATH_FLAG_IS_WRAP
Definition mathutils.hh:39
@ BASE_MATH_FLAG_HAS_BUFFER_VIEW
Definition mathutils.hh:52
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
Definition mathutils.cc:97
PyObject * Color_CreatePyObject(const float col[3], PyTypeObject *base_type)
BLI_INLINE void IMB_colormanagement_scene_linear_to_rec709(float rec709[3], const float scene_linear[3])
static PyObject * Color_from_scene_linear_to_srgb(ColorObject *self)
static PyObject * Color_vectorcall(PyObject *type, PyObject *const *args, const size_t nargsf, PyObject *kwnames)
static PyObject * Color_copy(ColorObject *self)
static PyObject * Color_idiv(PyObject *v1, PyObject *v2)
static PyObject * Color_deepcopy(ColorObject *self, PyObject *args)
PyTypeObject color_Type
MINLINE void zero_v3(float r[3])
static PySequenceMethods Color_SeqMethods
static PyObject * Color_slice(ColorObject *self, Py_ssize_t start, Py_ssize_t step, Py_ssize_t slice_length)
void add_vn_vnvn(float *array_tar, const float *array_src_a, const float *array_src_b, int size)
static PyObject * Color_from_scene_linear_to_aces(ColorObject *self)
BLI_INLINE void IMB_colormanagement_scene_linear_to_aces(float aces[3], const float scene_linear[3])
BLI_INLINE void IMB_colormanagement_aces_to_scene_linear(float scene_linear[3], const float aces[3])
static PyObject * Color_div(PyObject *v1, PyObject *v2)
static int Color_getbuffer(PyObject *obj, Py_buffer *view, int flags)
PyObject * BaseMathObject_is_wrapped_get(BaseMathObject *self, void *)
Definition mathutils.cc:698
char BaseMathObject_is_valid_doc[]
Definition mathutils.cc:710
void sub_vn_vn(float *array_tar, const float *array_src, int size)
static Py_ssize_t Color_len(ColorObject *)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
int BaseMathObject_traverse(BaseMathObject *self, visitproc visit, void *arg)
Definition mathutils.cc:742
static PyObject * Color_from_aces_to_scene_linear(ColorObject *self)
MINLINE void clamp_v3(float vec[3], float min, float max)
void negate_vn_vn(float *array_tar, const float *array_src, int size)
char BaseMathObject_is_frozen_doc[]
Definition mathutils.cc:703
static PyObject * Color_neg(ColorObject *self)
PyObject * Color_CreatePyObject_cb(PyObject *cb_user, uchar cb_type, uchar cb_subtype)
BLI_INLINE void IMB_colormanagement_scene_linear_to_srgb_v3(float srgb[3], const float scene_linear[3])
static int Color_ass_slice(ColorObject *self, Py_ssize_t start, Py_ssize_t step, Py_ssize_t slice_length, PyObject *seq)
static PyObject * Color_from_xyz_d65_to_scene_linear(ColorObject *self)
static PyBufferProcs Color_as_buffer
static PyObject * Color_item(ColorObject *self, Py_ssize_t i)
static PyMethodDef Color_methods[]
DynStr * BLI_dynstr_new() ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_dynstr.cc:39
PyObject * Color_CreatePyObject_wrap(float col[3], PyTypeObject *base_type)
static PyObject * Color_richcmpr(PyObject *a, PyObject *b, int op)
BLI_INLINE void IMB_colormanagement_scene_linear_to_xyz(float xyz[3], const float scene_linear[3])
static int Color_hsv_set(ColorObject *self, PyObject *value, void *)
char BaseMathObject_is_wrapped_doc[]
Definition mathutils.cc:696
header-only utilities
#define PyTuple_SET_ITEMS(op_arg,...)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float