33
32
#include "BLI_math.h"
34
33
#include "BLI_utildefines.h"
35
#include "BLI_dynstr.h"
35
#ifndef MATH_STANDALONE
36
# include "BLI_dynstr.h"
37
39
#define COLOR_SIZE 3
39
//----------------------------------mathutils.Color() -------------------
40
//makes a new color for you to play with
41
/* ----------------------------------mathutils.Color() ------------------- */
42
/* makes a new color for you to play with */
41
43
static PyObject *Color_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
43
45
float col[3] = {0.0f, 0.0f, 0.0f};
147
150
return mathutils_dynstr_to_py(ds); /* frees ds */
150
//------------------------tp_richcmpr
151
//returns -1 execption, 0 false, 1 true
154
/* ------------------------tp_richcmpr */
155
/* returns -1 exception, 0 false, 1 true */
152
156
static PyObject *Color_richcmpr(PyObject *a, PyObject *b, int op)
185
189
return Py_INCREF(res), res;
188
//---------------------SEQUENCE PROTOCOLS------------------------
189
//----------------------------len(object)------------------------
192
/* ---------------------SEQUENCE PROTOCOLS------------------------ */
193
/* ----------------------------len(object)------------------------ */
194
/* sequence length */
191
195
static int Color_len(ColorObject *UNUSED(self))
193
197
return COLOR_SIZE;
195
//----------------------------object[]---------------------------
196
//sequence accessor (get)
199
/* ----------------------------object[]--------------------------- */
200
/* sequence accessor (get) */
197
201
static PyObject *Color_item(ColorObject *self, int i)
199
203
if (i < 0) i = COLOR_SIZE - i;
211
215
return PyFloat_FromDouble(self->col[i]);
214
//----------------------------object[]-------------------------
215
//sequence accessor (set)
218
/* ----------------------------object[]------------------------- */
219
/* sequence accessor (set) */
216
220
static int Color_ass_item(ColorObject *self, int i, PyObject *value)
218
222
float f = PyFloat_AsDouble(value);
220
if (f == -1 && PyErr_Occurred()) { // parsed item not a number
224
if (f == -1 && PyErr_Occurred()) { /* parsed item not a number */
221
225
PyErr_SetString(PyExc_TypeError,
222
226
"color[item] = x: "
223
227
"argument not a number");
307
311
else if (PySlice_Check(item)) {
308
312
Py_ssize_t start, stop, step, slicelength;
310
if (PySlice_GetIndicesEx((void *)item, COLOR_SIZE, &start, &stop, &step, &slicelength) < 0)
314
if (PySlice_GetIndicesEx(item, COLOR_SIZE, &start, &stop, &step, &slicelength) < 0)
313
317
if (slicelength <= 0) {
343
347
else if (PySlice_Check(item)) {
344
348
Py_ssize_t start, stop, step, slicelength;
346
if (PySlice_GetIndicesEx((void *)item, COLOR_SIZE, &start, &stop, &step, &slicelength) < 0)
350
if (PySlice_GetIndicesEx(item, COLOR_SIZE, &start, &stop, &step, &slicelength) < 0)
645
649
NULL, /*nb_remainder*/
646
650
NULL, /*nb_divmod*/
647
651
NULL, /*nb_power*/
648
(unaryfunc) Color_neg, /*nb_negative*/
649
(unaryfunc) NULL, /*tp_positive*/
652
(unaryfunc) Color_neg, /*nb_negative*/
653
(unaryfunc) Color_copy, /*tp_positive*/
650
654
(unaryfunc) NULL, /*tp_absolute*/
651
655
(inquiry) NULL, /*tp_bool*/
652
656
(unaryfunc) NULL, /*nb_invert*/
724
728
if (BaseMath_ReadCallback(self) == -1)
727
rgb_to_hsv(self->col[0], self->col[1], self->col[2], &(hsv[0]), &(hsv[1]), &(hsv[2]));
731
rgb_to_hsv_v(self->col, hsv);
728
732
CLAMP(f, 0.0f, 1.0f);
730
hsv_to_rgb(hsv[0], hsv[1], hsv[2], &(self->col[0]), &(self->col[1]), &(self->col[2]));
734
hsv_to_rgb_v(hsv, self->col);
732
736
if (BaseMath_WriteCallback(self) == -1)
765
769
CLAMP(hsv[1], 0.0f, 1.0f);
766
770
CLAMP(hsv[2], 0.0f, 1.0f);
768
hsv_to_rgb(hsv[0], hsv[1], hsv[2], &(self->col[0]), &(self->col[1]), &(self->col[2]));
772
hsv_to_rgb_v(hsv, self->col);
770
774
if (BaseMath_WriteCallback(self) == -1)
796
//-----------------------METHOD DEFINITIONS ----------------------
800
/* -----------------------METHOD DEFINITIONS ---------------------- */
797
801
static struct PyMethodDef Color_methods[] = {
798
802
{"copy", (PyCFunction) Color_copy, METH_NOARGS, Color_copy_doc},
799
803
{"__copy__", (PyCFunction) Color_copy, METH_NOARGS, Color_copy_doc},
801
805
{NULL, NULL, 0, NULL}
804
//------------------PY_OBECT DEFINITION--------------------------
808
/* ------------------PY_OBECT DEFINITION-------------------------- */
805
809
PyDoc_STRVAR(color_doc,
806
810
"This object gives access to Colors in Blender."
808
812
PyTypeObject color_Type = {
809
813
PyVarObject_HEAD_INIT(NULL, 0)
811
sizeof(ColorObject), //tp_basicsize
813
(destructor)BaseMathObject_dealloc, //tp_dealloc
818
(reprfunc) Color_repr, //tp_repr
819
&Color_NumMethods, //tp_as_number
820
&Color_SeqMethods, //tp_as_sequence
821
&Color_AsMapping, //tp_as_mapping
824
(reprfunc) Color_str, //tp_str
828
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, //tp_flags
830
(traverseproc)BaseMathObject_traverse, //tp_traverse
831
(inquiry)BaseMathObject_clear, //tp_clear
832
(richcmpfunc)Color_richcmpr, //tp_richcompare
833
0, //tp_weaklistoffset
836
Color_methods, //tp_methods
838
Color_getseters, //tp_getset
852
NULL, //tp_subclasses
814
"Color", /* tp_name */
815
sizeof(ColorObject), /* tp_basicsize */
817
(destructor)BaseMathObject_dealloc, /* tp_dealloc */
819
NULL, /* tp_getattr */
820
NULL, /* tp_setattr */
821
NULL, /* tp_compare */
822
(reprfunc) Color_repr, /* tp_repr */
823
&Color_NumMethods, /* tp_as_number */
824
&Color_SeqMethods, /* tp_as_sequence */
825
&Color_AsMapping, /* tp_as_mapping */
828
#ifndef MATH_STANDALONE
829
(reprfunc) Color_str, /* tp_str */
833
NULL, /* tp_getattro */
834
NULL, /* tp_setattro */
835
NULL, /* tp_as_buffer */
836
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
837
color_doc, /* tp_doc */
838
(traverseproc)BaseMathObject_traverse, /* tp_traverse */
839
(inquiry)BaseMathObject_clear, /* tp_clear */
840
(richcmpfunc)Color_richcmpr, /* tp_richcompare */
841
0, /* tp_weaklistoffset */
843
NULL, /* tp_iternext */
844
Color_methods, /* tp_methods */
845
NULL, /* tp_members */
846
Color_getseters, /* tp_getset */
849
NULL, /* tp_descr_get */
850
NULL, /* tp_descr_set */
851
0, /* tp_dictoffset */
854
Color_new, /* tp_new */
860
NULL, /* tp_subclasses */
861
NULL, /* tp_weaklist */
856
//------------------------Color_CreatePyObject (internal)-------------
857
//creates a new color object
864
/* ------------------------Color_CreatePyObject (internal)------------- */
865
/* creates a new color object */
858
866
/* pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
859
867
* (i.e. it was allocated elsewhere by MEM_mallocN())
860
868
* pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
861
869
* (i.e. it must be created here with PyMEM_malloc())*/
862
PyObject *Color_CreatePyObject(float *col, int type, PyTypeObject *base_type)
870
PyObject *Color_CreatePyObject(float col[3], int type, PyTypeObject *base_type)
864
872
ColorObject *self;