Changeset View
Changeset View
Standalone View
Standalone View
source/blender/python/mathutils/mathutils_Color.c
| Show All 32 Lines | |||||
| #include "BLI_utildefines.h" | #include "BLI_utildefines.h" | ||||
| #include "../generic/python_utildefines.h" | #include "../generic/python_utildefines.h" | ||||
| #ifndef MATH_STANDALONE | #ifndef MATH_STANDALONE | ||||
| # include "BLI_dynstr.h" | # include "BLI_dynstr.h" | ||||
| #endif | #endif | ||||
| #define COLOR_SIZE 3 | #define COLOR_SIZE 4 | ||||
| #define COLOR_MINSIZE 3 | |||||
| /* ----------------------------------mathutils.Color() ------------------- */ | /* ----------------------------------mathutils.Color() ------------------- */ | ||||
| /* makes a new color for you to play with */ | /* makes a new color for you to play with */ | ||||
| static PyObject *Color_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | static PyObject *Color_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||||
| { | { | ||||
| float col[3] = {0.0f, 0.0f, 0.0f}; | float col[4] = {0.0f, 0.0f, 0.0f, 1.0f}; | ||||
| if (kwds && PyDict_Size(kwds)) { | if (kwds && PyDict_Size(kwds)) { | ||||
| PyErr_SetString(PyExc_TypeError, | PyErr_SetString(PyExc_TypeError, | ||||
| "mathutils.Color(): " | "mathutils.Color(): " | ||||
| "takes no keyword args"); | "takes no keyword args"); | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| switch (PyTuple_GET_SIZE(args)) { | switch (PyTuple_GET_SIZE(args)) { | ||||
| case 0: | case 0: | ||||
| break; | break; | ||||
| case 1: | case 1: | ||||
| if ((mathutils_array_parse(col, COLOR_SIZE, COLOR_SIZE, PyTuple_GET_ITEM(args, 0), "mathutils.Color()")) == -1) | if ((mathutils_array_parse(col, COLOR_MINSIZE, COLOR_SIZE, PyTuple_GET_ITEM(args, 0), "mathutils.Color()")) == -1) | ||||
| return NULL; | return NULL; | ||||
| break; | break; | ||||
| default: | default: | ||||
| PyErr_SetString(PyExc_TypeError, | PyErr_SetString(PyExc_TypeError, | ||||
| "mathutils.Color(): " | "mathutils.Color(): " | ||||
| "more than a single arg given"); | "more than a single arg given"); | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 627 Lines • ▼ Show 20 Lines | static PyNumberMethods Color_NumMethods = { | ||||
| Color_idiv, /* nb_inplace_true_divide */ | Color_idiv, /* nb_inplace_true_divide */ | ||||
| NULL, /* nb_index */ | NULL, /* nb_index */ | ||||
| }; | }; | ||||
| /* color channel, vector.r/g/b */ | /* color channel, vector.r/g/b */ | ||||
| PyDoc_STRVAR(Color_channel_r_doc, "Red color channel.\n\n:type: float"); | PyDoc_STRVAR(Color_channel_r_doc, "Red color channel.\n\n:type: float"); | ||||
| PyDoc_STRVAR(Color_channel_g_doc, "Green color channel.\n\n:type: float"); | PyDoc_STRVAR(Color_channel_g_doc, "Green color channel.\n\n:type: float"); | ||||
| PyDoc_STRVAR(Color_channel_b_doc, "Blue color channel.\n\n:type: float"); | PyDoc_STRVAR(Color_channel_b_doc, "Blue color channel.\n\n:type: float"); | ||||
| PyDoc_STRVAR(Color_channel_a_doc, "Alpha channel (optional).\n\n:type: float"); | |||||
| static PyObject *Color_channel_get(ColorObject *self, void *type) | static PyObject *Color_channel_get(ColorObject *self, void *type) | ||||
| { | { | ||||
| return Color_item(self, GET_INT_FROM_POINTER(type)); | return Color_item(self, GET_INT_FROM_POINTER(type)); | ||||
| } | } | ||||
| static int Color_channel_set(ColorObject *self, PyObject *value, void *type) | static int Color_channel_set(ColorObject *self, PyObject *value, void *type) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| /*****************************************************************************/ | /*****************************************************************************/ | ||||
| /* Python attributes get/set structure: */ | /* Python attributes get/set structure: */ | ||||
| /*****************************************************************************/ | /*****************************************************************************/ | ||||
| static PyGetSetDef Color_getseters[] = { | static PyGetSetDef Color_getseters[] = { | ||||
| {(char *)"r", (getter)Color_channel_get, (setter)Color_channel_set, Color_channel_r_doc, (void *)0}, | {(char *)"r", (getter)Color_channel_get, (setter)Color_channel_set, Color_channel_r_doc, (void *)0}, | ||||
| {(char *)"g", (getter)Color_channel_get, (setter)Color_channel_set, Color_channel_g_doc, (void *)1}, | {(char *)"g", (getter)Color_channel_get, (setter)Color_channel_set, Color_channel_g_doc, (void *)1}, | ||||
| {(char *)"b", (getter)Color_channel_get, (setter)Color_channel_set, Color_channel_b_doc, (void *)2}, | {(char *)"b", (getter)Color_channel_get, (setter)Color_channel_set, Color_channel_b_doc, (void *)2}, | ||||
| {(char *)"a", (getter)Color_channel_get, (setter)Color_channel_set, Color_channel_a_doc, (void *)3}, | |||||
| {(char *)"h", (getter)Color_channel_hsv_get, (setter)Color_channel_hsv_set, (char *)Color_channel_hsv_h_doc, (void *)0}, | {(char *)"h", (getter)Color_channel_hsv_get, (setter)Color_channel_hsv_set, (char *)Color_channel_hsv_h_doc, (void *)0}, | ||||
| {(char *)"s", (getter)Color_channel_hsv_get, (setter)Color_channel_hsv_set, (char *)Color_channel_hsv_s_doc, (void *)1}, | {(char *)"s", (getter)Color_channel_hsv_get, (setter)Color_channel_hsv_set, (char *)Color_channel_hsv_s_doc, (void *)1}, | ||||
| {(char *)"v", (getter)Color_channel_hsv_get, (setter)Color_channel_hsv_set, (char *)Color_channel_hsv_v_doc, (void *)2}, | {(char *)"v", (getter)Color_channel_hsv_get, (setter)Color_channel_hsv_set, (char *)Color_channel_hsv_v_doc, (void *)2}, | ||||
| {(char *)"hsv", (getter)Color_hsv_get, (setter)Color_hsv_set, (char *)Color_hsv_doc, (void *)0}, | {(char *)"hsv", (getter)Color_hsv_get, (setter)Color_hsv_set, (char *)Color_hsv_doc, (void *)0}, | ||||
| {(char *)"is_wrapped", (getter)BaseMathObject_is_wrapped_get, (setter)NULL, BaseMathObject_is_wrapped_doc, NULL}, | {(char *)"is_wrapped", (getter)BaseMathObject_is_wrapped_get, (setter)NULL, BaseMathObject_is_wrapped_doc, NULL}, | ||||
| ▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | #endif | ||||
| NULL, /* tp_mro */ | NULL, /* tp_mro */ | ||||
| NULL, /* tp_cache */ | NULL, /* tp_cache */ | ||||
| NULL, /* tp_subclasses */ | NULL, /* tp_subclasses */ | ||||
| NULL, /* tp_weaklist */ | NULL, /* tp_weaklist */ | ||||
| NULL /* tp_del */ | NULL /* tp_del */ | ||||
| }; | }; | ||||
| PyObject *Color_CreatePyObject( | PyObject *Color_CreatePyObject( | ||||
| const float col[3], | const float col[4], | ||||
| PyTypeObject *base_type) | PyTypeObject *base_type) | ||||
| { | { | ||||
| ColorObject *self; | ColorObject *self; | ||||
| float *col_alloc; | float *col_alloc; | ||||
| col_alloc = PyMem_Malloc(COLOR_SIZE * sizeof(float)); | col_alloc = PyMem_Malloc(COLOR_SIZE * sizeof(float)); | ||||
| if (UNLIKELY(col_alloc == NULL)) { | if (UNLIKELY(col_alloc == NULL)) { | ||||
| PyErr_SetString(PyExc_MemoryError, | PyErr_SetString(PyExc_MemoryError, | ||||
| "Color(): " | "Color(): " | ||||
| "problem allocating data"); | "problem allocating data"); | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| self = BASE_MATH_NEW(ColorObject, color_Type, base_type); | self = BASE_MATH_NEW(ColorObject, color_Type, base_type); | ||||
| if (self) { | if (self) { | ||||
| self->col = col_alloc; | self->col = col_alloc; | ||||
| /* init callbacks as NULL */ | /* init callbacks as NULL */ | ||||
| self->cb_user = NULL; | self->cb_user = NULL; | ||||
| self->cb_type = self->cb_subtype = 0; | self->cb_type = self->cb_subtype = 0; | ||||
| /* NEW */ | /* NEW */ | ||||
| if (col) | if (col) | ||||
| copy_v3_v3(self->col, col); | copy_v4_v4(self->col, col); | ||||
| else | else | ||||
| zero_v3(self->col); | zero_v4(self->col); | ||||
campbellbarton: We should not assume colors have alpha, some input may be RGB data.
Suggest to have a size… | |||||
| self->flag = BASE_MATH_FLAG_DEFAULT; | self->flag = BASE_MATH_FLAG_DEFAULT; | ||||
| } | } | ||||
| else { | else { | ||||
| PyMem_Free(col_alloc); | PyMem_Free(col_alloc); | ||||
| } | } | ||||
| return (PyObject *)self; | return (PyObject *)self; | ||||
| } | } | ||||
| PyObject *Color_CreatePyObject_wrap( | PyObject *Color_CreatePyObject_wrap( | ||||
| float col[3], | float col[4], | ||||
| PyTypeObject *base_type) | PyTypeObject *base_type) | ||||
| { | { | ||||
| ColorObject *self; | ColorObject *self; | ||||
| self = BASE_MATH_NEW(ColorObject, color_Type, base_type); | self = BASE_MATH_NEW(ColorObject, color_Type, base_type); | ||||
| if (self) { | if (self) { | ||||
| /* init callbacks as NULL */ | /* init callbacks as NULL */ | ||||
| self->cb_user = NULL; | self->cb_user = NULL; | ||||
| Show All 24 Lines | |||||
We should not assume colors have alpha, some input may be RGB data.
Suggest to have a size argument, as vector does.