~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/python/mathutils/mathutils_Color.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *
3
2
 * ***** BEGIN GPL LICENSE BLOCK *****
4
3
 *
5
4
 * This program is free software; you can redistribute it and/or
32
31
 
33
32
#include "BLI_math.h"
34
33
#include "BLI_utildefines.h"
35
 
#include "BLI_dynstr.h"
 
34
 
 
35
#ifndef MATH_STANDALONE
 
36
#  include "BLI_dynstr.h"
 
37
#endif
36
38
 
37
39
#define COLOR_SIZE 3
38
40
 
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)
42
44
{
43
45
        float col[3] = {0.0f, 0.0f, 0.0f};
65
67
        return Color_CreatePyObject(col, Py_NEW, type);
66
68
}
67
69
 
68
 
//-----------------------------METHODS----------------------------
 
70
/* -----------------------------METHODS---------------------------- */
69
71
 
70
72
/* note: BaseMath_ReadCallback must be called beforehand */
71
73
static PyObject *Color_ToTupleExt(ColorObject *self, int ndigits)
114
116
        return Color_copy(self);
115
117
}
116
118
 
117
 
//----------------------------print object (internal)--------------
118
 
//print the object to screen
 
119
/* ----------------------------print object (internal)-------------- */
 
120
/* print the object to screen */
119
121
 
120
122
static PyObject *Color_repr(ColorObject *self)
121
123
{
132
134
        return ret;
133
135
}
134
136
 
 
137
#ifndef MATH_STANDALONE
135
138
static PyObject *Color_str(ColorObject *self)
136
139
{
137
140
        DynStr *ds;
146
149
 
147
150
        return mathutils_dynstr_to_py(ds); /* frees ds */
148
151
}
 
152
#endif
149
153
 
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)
153
157
{
154
158
        PyObject *res;
185
189
        return Py_INCREF(res), res;
186
190
}
187
191
 
188
 
//---------------------SEQUENCE PROTOCOLS------------------------
189
 
//----------------------------len(object)------------------------
190
 
//sequence length
 
192
/* ---------------------SEQUENCE PROTOCOLS------------------------ */
 
193
/* ----------------------------len(object)------------------------ */
 
194
/* sequence length */
191
195
static int Color_len(ColorObject *UNUSED(self))
192
196
{
193
197
        return COLOR_SIZE;
194
198
}
195
 
//----------------------------object[]---------------------------
196
 
//sequence accessor (get)
 
199
/* ----------------------------object[]--------------------------- */
 
200
/* sequence accessor (get) */
197
201
static PyObject *Color_item(ColorObject *self, int i)
198
202
{
199
203
        if (i < 0) i = COLOR_SIZE - i;
211
215
        return PyFloat_FromDouble(self->col[i]);
212
216
 
213
217
}
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)
217
221
{
218
222
        float f = PyFloat_AsDouble(value);
219
223
 
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");
239
243
 
240
244
        return 0;
241
245
}
242
 
//----------------------------object[z:y]------------------------
243
 
//sequence slice (get)
 
246
/* ----------------------------object[z:y]------------------------ */
 
247
/* sequence slice (get) */
244
248
static PyObject *Color_slice(ColorObject *self, int begin, int end)
245
249
{
246
250
        PyObject *tuple;
261
265
 
262
266
        return tuple;
263
267
}
264
 
//----------------------------object[z:y]------------------------
265
 
//sequence slice (set)
 
268
/* ----------------------------object[z:y]------------------------ */
 
269
/* sequence slice (set) */
266
270
static int Color_ass_slice(ColorObject *self, int begin, int end, PyObject *seq)
267
271
{
268
272
        int i, size;
307
311
        else if (PySlice_Check(item)) {
308
312
                Py_ssize_t start, stop, step, slicelength;
309
313
 
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)
311
315
                        return NULL;
312
316
 
313
317
                if (slicelength <= 0) {
343
347
        else if (PySlice_Check(item)) {
344
348
                Py_ssize_t start, stop, step, slicelength;
345
349
 
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)
347
351
                        return -1;
348
352
 
349
353
                if (step == 1)
362
366
        }
363
367
}
364
368
 
365
 
//-----------------PROTCOL DECLARATIONS--------------------------
 
369
/* -----------------PROTCOL DECLARATIONS-------------------------- */
366
370
static PySequenceMethods Color_SeqMethods = {
367
371
        (lenfunc) Color_len,                    /* sq_length */
368
372
        (binaryfunc) NULL,                      /* sq_concat */
566
570
        return NULL;
567
571
}
568
572
 
569
 
/* mulplication in-place: obj *= obj */
 
573
/* multiplication in-place: obj *= obj */
570
574
static PyObject *Color_imul(PyObject *v1, PyObject *v2)
571
575
{
572
576
        ColorObject *color = (ColorObject *)v1;
592
596
        return v1;
593
597
}
594
598
 
595
 
/* mulplication in-place: obj *= obj */
 
599
/* multiplication in-place: obj *= obj */
596
600
static PyObject *Color_idiv(PyObject *v1, PyObject *v2)
597
601
{
598
602
        ColorObject *color = (ColorObject *)v1;
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)
725
729
                return -1;
726
730
 
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);
729
733
        hsv[i] = f;
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);
731
735
 
732
736
        if (BaseMath_WriteCallback(self) == -1)
733
737
                return -1;
765
769
        CLAMP(hsv[1], 0.0f, 1.0f);
766
770
        CLAMP(hsv[2], 0.0f, 1.0f);
767
771
 
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);
769
773
 
770
774
        if (BaseMath_WriteCallback(self) == -1)
771
775
                return -1;
793
797
};
794
798
 
795
799
 
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}
802
806
};
803
807
 
804
 
//------------------PY_OBECT DEFINITION--------------------------
 
808
/* ------------------PY_OBECT DEFINITION-------------------------- */
805
809
PyDoc_STRVAR(color_doc,
806
810
"This object gives access to Colors in Blender."
807
811
);
808
812
PyTypeObject color_Type = {
809
813
        PyVarObject_HEAD_INIT(NULL, 0)
810
 
        "Color",                        //tp_name
811
 
        sizeof(ColorObject),            //tp_basicsize
812
 
        0,                              //tp_itemsize
813
 
        (destructor)BaseMathObject_dealloc,     //tp_dealloc
814
 
        NULL,                           //tp_print
815
 
        NULL,                           //tp_getattr
816
 
        NULL,                           //tp_setattr
817
 
        NULL,                           //tp_compare
818
 
        (reprfunc) Color_repr,          //tp_repr
819
 
        &Color_NumMethods,              //tp_as_number
820
 
        &Color_SeqMethods,              //tp_as_sequence
821
 
        &Color_AsMapping,               //tp_as_mapping
822
 
        NULL,                           //tp_hash
823
 
        NULL,                           //tp_call
824
 
        (reprfunc) Color_str,           //tp_str
825
 
        NULL,                           //tp_getattro
826
 
        NULL,                           //tp_setattro
827
 
        NULL,                           //tp_as_buffer
828
 
        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, //tp_flags
829
 
        color_doc, //tp_doc
830
 
        (traverseproc)BaseMathObject_traverse,  //tp_traverse
831
 
        (inquiry)BaseMathObject_clear,  //tp_clear
832
 
        (richcmpfunc)Color_richcmpr,    //tp_richcompare
833
 
        0,                              //tp_weaklistoffset
834
 
        NULL,                           //tp_iter
835
 
        NULL,                           //tp_iternext
836
 
        Color_methods,                  //tp_methods
837
 
        NULL,                           //tp_members
838
 
        Color_getseters,                //tp_getset
839
 
        NULL,                           //tp_base
840
 
        NULL,                           //tp_dict
841
 
        NULL,                           //tp_descr_get
842
 
        NULL,                           //tp_descr_set
843
 
        0,                              //tp_dictoffset
844
 
        NULL,                           //tp_init
845
 
        NULL,                           //tp_alloc
846
 
        Color_new,                      //tp_new
847
 
        NULL,                           //tp_free
848
 
        NULL,                           //tp_is_gc
849
 
        NULL,                           //tp_bases
850
 
        NULL,                           //tp_mro
851
 
        NULL,                           //tp_cache
852
 
        NULL,                           //tp_subclasses
853
 
        NULL,                           //tp_weaklist
854
 
        NULL                            //tp_del
 
814
        "Color",                        /* tp_name */
 
815
        sizeof(ColorObject),            /* tp_basicsize */
 
816
        0,                              /* tp_itemsize */
 
817
        (destructor)BaseMathObject_dealloc,  /* tp_dealloc */
 
818
        NULL,                           /* tp_print */
 
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 */
 
826
        NULL,                           /* tp_hash */
 
827
        NULL,                           /* tp_call */
 
828
#ifndef MATH_STANDALONE
 
829
        (reprfunc) Color_str,           /* tp_str */
 
830
#else
 
831
        NULL,                           /* tp_str */
 
832
#endif
 
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 */
 
842
        NULL,                           /* tp_iter */
 
843
        NULL,                           /* tp_iternext */
 
844
        Color_methods,                  /* tp_methods */
 
845
        NULL,                           /* tp_members */
 
846
        Color_getseters,                /* tp_getset */
 
847
        NULL,                           /* tp_base */
 
848
        NULL,                           /* tp_dict */
 
849
        NULL,                           /* tp_descr_get */
 
850
        NULL,                           /* tp_descr_set */
 
851
        0,                              /* tp_dictoffset */
 
852
        NULL,                           /* tp_init */
 
853
        NULL,                           /* tp_alloc */
 
854
        Color_new,                      /* tp_new */
 
855
        NULL,                           /* tp_free */
 
856
        NULL,                           /* tp_is_gc */
 
857
        NULL,                           /* tp_bases */
 
858
        NULL,                           /* tp_mro */
 
859
        NULL,                           /* tp_cache */
 
860
        NULL,                           /* tp_subclasses */
 
861
        NULL,                           /* tp_weaklist */
 
862
        NULL                            /* tp_del */
855
863
};
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)
863
871
{
864
872
        ColorObject *self;
865
873