35
44
static PyObject *_COLORDICT = NULL;
37
46
static int _get_double (PyObject *obj, double *val);
38
47
static int _get_color (PyObject *val, Uint32 *color);
39
48
static int _hextoint (char *hex, Uint8 *val);
40
static int _hexcolor (PyObject *color, Uint8 rgba[]);
49
static tristate _hexcolor (PyObject *color, Uint8 rgba[]);
50
static int _coerce_obj(PyObject *obj, Uint8 rgba[]);
42
52
static PyColor* _color_new_internal (PyTypeObject *type, Uint8 rgba[]);
53
static PyColor* _color_new_internal_length (PyTypeObject *type, Uint8 rgba[], Uint8 length);
43
55
static PyObject* _color_new (PyTypeObject *type, PyObject *args,
45
57
static void _color_dealloc (PyColor *color);
46
58
static PyObject* _color_repr (PyColor *color);
47
59
static PyObject* _color_normalize (PyColor *color);
48
60
static PyObject* _color_correct_gamma (PyColor *color, PyObject *args);
61
static PyObject* _color_set_length (PyColor *color, PyObject *args);
50
63
/* Getters/setters */
51
64
static PyObject* _color_get_r (PyColor *color, void *closure);
72
85
static PyObject* _color_div (PyColor *color1, PyColor *color2);
73
86
static PyObject* _color_mod (PyColor *color1, PyColor *color2);
74
87
static PyObject* _color_inv (PyColor *color);
75
89
static int _color_coerce (PyObject **pv, PyObject **pw);
76
91
static PyObject* _color_int (PyColor *color);
77
92
static PyObject* _color_long (PyColor *color);
78
93
static PyObject* _color_float (PyColor *color);
79
95
static PyObject* _color_oct (PyColor *color);
80
96
static PyObject* _color_hex (PyColor *color);
82
99
/* Sequence protocol methods */
83
100
static Py_ssize_t _color_length (PyColor *color);
84
101
static PyObject* _color_item (PyColor *color, Py_ssize_t _index);
85
102
static int _color_ass_item (PyColor *color, Py_ssize_t _index, PyObject *value);
103
static PyObject * _color_slice(register PyColor *a,
104
register Py_ssize_t ilow,
105
register Py_ssize_t ihigh);
107
/* Mapping protocol methods. */
108
static PyObject * _color_subscript(PyColor* self, PyObject* item);
112
static PyObject* _color_richcompare(PyObject *o1, PyObject *o2, int opid);
87
114
/* C API interfaces */
88
115
static PyObject* PyColor_New (Uint8 rgba[]);
116
static PyObject* PyColor_NewLength (Uint8 rgba[], Uint8 length);
89
117
static int RGBAFromColorObj (PyObject *color, Uint8 rgba[]);
97
125
DOC_COLORNORMALIZE },
98
126
{ "correct_gamma", (PyCFunction) _color_correct_gamma, METH_VARARGS,
99
127
DOC_COLORCORRECTGAMMA },
128
{ "set_length", (PyCFunction) _color_set_length, METH_VARARGS,
129
DOC_COLORSETLENGTH },
100
130
{ NULL, NULL, 0, NULL }
126
156
(binaryfunc) _color_add, /* nb_add */
127
157
(binaryfunc) _color_sub, /* nb_subtract */
128
158
(binaryfunc) _color_mul, /* nb_multiply */
129
160
(binaryfunc) _color_div, /* nb_divide */
130
162
(binaryfunc) _color_mod, /* nb_remainder */
131
163
0, /* nb_divmod */
132
164
0, /* nb_power */
133
165
0, /* nb_negative */
134
166
0, /* nb_positive */
135
167
0, /* nb_absolute */
168
0, /* nb_nonzero / nb_bool*/
137
169
(unaryfunc) _color_inv, /* nb_invert */
138
170
0, /* nb_lshift */
139
171
0, /* nb_rshift */
143
176
_color_coerce, /* nb_coerce */
144
178
(unaryfunc) _color_int, /* nb_int */
145
179
(unaryfunc) _color_long, /* nb_long */
146
180
(unaryfunc) _color_float,/* nb_float */
147
182
(unaryfunc) _color_oct, /* nb_oct */
148
183
(unaryfunc) _color_hex, /* nb_hex */
149
185
0, /* nb_inplace_add */
150
186
0, /* nb_inplace_subtract */
151
187
0, /* nb_inplace_multiply */
152
189
0, /* nb_inplace_divide */
153
191
0, /* nb_inplace_remainder */
154
192
0, /* nb_inplace_power */
155
193
0, /* nb_inplace_lshift */
175
213
NULL, /* sq_concat */
176
214
NULL, /* sq_repeat */
177
215
(ssizeargfunc) _color_item, /* sq_item */
216
(ssizessizeargfunc)_color_slice, /* sq_slice */
179
217
(ssizeobjargproc) _color_ass_item, /* sq_ass_item */
180
NULL, /* sq_ass_slice */
218
NULL, /* sq_ass_slice */
181
219
NULL, /* sq_contains */
182
220
NULL, /* sq_inplace_concat */
183
221
NULL, /* sq_inplace_repeat */
226
static PyMappingMethods _color_as_mapping = {
227
(lenfunc) _color_length,
228
(binaryfunc)_color_subscript,
238
#define DEFERRED_ADDRESS(ADDR) 0
186
240
static PyTypeObject PyColor_Type =
188
PyObject_HEAD_INIT(NULL)
190
243
"pygame.Color", /* tp_name */
191
244
sizeof (PyColor), /* tp_basicsize */
192
245
0, /* tp_itemsize */
198
251
(reprfunc) _color_repr, /* tp_repr */
199
252
&_color_as_number, /* tp_as_number */
200
253
&_color_as_sequence, /* tp_as_sequence */
201
0, /* tp_as_mapping */
254
#if PY_VERSION_HEX < 0x02050000
257
&_color_as_mapping, /* tp_as_mapping */
282
343
*color = (Uint32) longval;
286
PyErr_SetString (PyExc_ValueError, "invalid color argument");
348
PyErr_SetString (PyExc_ValueError, "invalid color argument");
291
353
_hextoint (char *hex, Uint8 *val)
293
char part[3] = { '\0' };
298
*val = strtol (part, &eptr, 16);
299
if (eptr == part) /* Failure */
355
/* 'hex' is a two digit hexadecimal number, no spaces, no signs.
356
* This algorithm is brute force, but it is character system agnostic.
357
* It is definitely not a general purpose solution.
361
switch (toupper(hex[0]))
414
switch (toupper(hex[1]))
305
472
_hexcolor (PyObject *color, Uint8 rgba[])
308
char *name = PyString_AsString (color);
475
tristate rcode = TRISTATE_FAIL;
478
PyObject* ascii = PyUnicode_AsASCIIString (color);
480
rcode = TRISTATE_ERROR;
483
name = PyBytes_AsString (ascii);
485
name = PyString_AsString (color);
312
491
len = strlen (name);
313
492
/* hex colors can be
322
502
if (name[0] == '#')
324
504
if (len != 7 && len != 9)
326
506
if (!_hextoint (name + 1, &rgba[0]))
328
508
if (!_hextoint (name + 3, &rgba[1]))
330
510
if (!_hextoint (name + 5, &rgba[2]))
333
513
if (len == 9 && !_hextoint (name + 7, &rgba[3]))
337
517
else if (name[0] == '0' && name[1] == 'x')
339
519
if (len != 8 && len != 10)
341
521
if (!_hextoint (name + 2, &rgba[0]))
343
523
if (!_hextoint (name + 4, &rgba[1]))
345
525
if (!_hextoint (name + 6, &rgba[2]))
348
528
if (len == 10 && !_hextoint (name + 8, &rgba[3]))
535
rcode = TRISTATE_SUCCESS;
544
_coerce_obj (PyObject *obj, Uint8 rgba[])
546
if (PyType_IsSubtype (obj->ob_type, &PyColor_Type))
548
rgba[0] = ((PyColor *) obj)->r;
549
rgba[1] = ((PyColor *) obj)->g;
550
rgba[2] = ((PyColor *) obj)->b;
551
rgba[3] = ((PyColor *) obj)->a;
554
else if (PyType_IsSubtype (obj->ob_type, &PyTuple_Type))
556
if (RGBAFromObj (obj, rgba))
560
else if (PyErr_Occurred ())
356
570
_color_new_internal (PyTypeObject *type, Uint8 rgba[])
572
/* default length of 4 - r,g,b,a. */
573
return _color_new_internal_length(type, rgba, 4);
578
_color_new_internal_length (PyTypeObject *type, Uint8 rgba[], Uint8 length)
358
580
PyColor *color = (PyColor *) type->tp_alloc (type, 0);
379
602
if (!PyArg_ParseTuple (args, "O|OOO", &obj, &obj1, &obj2, &obj3))
382
if (PyString_Check (obj))
605
if (Text_Check (obj))
384
607
/* Named color */
385
608
PyObject *color = NULL;
609
PyObject *name1 = NULL, *name2 = NULL;
386
610
if (obj1 || obj2 || obj3)
387
611
return RAISE (PyExc_ValueError, "invalid arguments");
389
color = PyDict_GetItem (_COLORDICT, obj);
613
name1 = PyObject_CallMethod(obj, "replace", "(ss)", " ", "");
618
name2 = PyObject_CallMethod(name1, "lower", NULL);
624
color = PyDict_GetItem (_COLORDICT, name2);
392
if (!_hexcolor (obj, rgba))
628
switch (_hexcolor (obj, rgba))
393
631
return RAISE (PyExc_ValueError, "invalid color name");
395
638
else if (!RGBAFromObj (color, rgba))
396
640
return RAISE (PyExc_ValueError, "invalid color");
398
643
return (PyObject *) _color_new_internal (type, rgba);
461
706
PyOS_snprintf (buf, sizeof (buf), "(%d, %d, %d, %d)",
462
707
color->r, color->g, color->b, color->a);
463
return PyString_FromString (buf);
708
return Text_FromUTF8 (buf);
1205
1452
static PyObject*
1206
1453
_color_int (PyColor *color)
1208
unsigned long tmp = (color->r << 24) + (color->g << 16) + (color->b << 8) +
1455
Uint32 tmp = (color->r << 24) + (color->g << 16) +
1456
(color->b << 8) + color->a;
1211
1459
return PyInt_FromLong ((long) tmp);
1212
1461
return PyLong_FromUnsignedLong (tmp);
1218
1467
static PyObject*
1219
1468
_color_long (PyColor *color)
1221
unsigned long tmp = (color->r << 24) + (color->g << 16) + (color->b << 8) +
1470
Uint32 tmp = (color->r << 24) + (color->g << 16) + (color->b << 8) +
1223
1472
return PyLong_FromUnsignedLong (tmp);
1229
1478
static PyObject*
1230
1479
_color_float (PyColor *color)
1232
unsigned long tmp = (color->r << 24) + (color->g << 16) + (color->b << 8) +
1481
Uint32 tmp = (color->r << 24) + (color->g << 16) + (color->b << 8) +
1234
1483
return PyFloat_FromDouble ((double) tmp);
1241
1491
_color_oct (PyColor *color)
1244
unsigned long tmp = (color->r << 24) + (color->g << 16) + (color->b << 8) +
1494
Uint32 tmp = (color->r << 24) + (color->g << 16) + (color->b << 8) +
1247
PyOS_snprintf (buf, sizeof (buf), "0%lo", tmp);
1498
PyOS_snprintf (buf, sizeof (buf), "0%lo", (unsigned long) tmp);
1249
PyOS_snprintf (buf, sizeof (buf), "0%loL", tmp);
1500
PyOS_snprintf (buf, sizeof (buf), "0%loL", (unsigned long) tmp);
1250
1501
return PyString_FromString (buf);
1257
1508
_color_hex (PyColor *color)
1260
unsigned long tmp = (color->r << 24) + (color->g << 16) + (color->b << 8) +
1511
Uint32 tmp = (color->r << 24) + (color->g << 16) + (color->b << 8) +
1263
PyOS_snprintf (buf, sizeof (buf), "0x%lx", tmp);
1514
PyOS_snprintf (buf, sizeof (buf), "0x%lx", (unsigned long) tmp);
1266
1517
#if PY_VERSION_HEX >= 0x02050000
1267
PyOS_snprintf (buf, sizeof (buf), "0x%lxL", tmp);
1518
PyOS_snprintf (buf, sizeof (buf), "0x%lxL", (unsigned long) tmp);
1269
1520
/* <= 2.4 uses capitalised hex chars. */
1270
PyOS_snprintf (buf, sizeof (buf), "0x%lXL", tmp);
1521
PyOS_snprintf (buf, sizeof (buf), "0x%lXL", (unsigned long) tmp);
1273
return PyString_FromString (buf);
1524
return Text_FromUTF8 (buf);
1276
1528
/* Sequence protocol methods */
1281
1533
static Py_ssize_t
1282
1534
_color_length (PyColor *color)
1540
* color.set_length(3)
1544
_color_set_length (PyColor *color, PyObject *args)
1548
if (!PyArg_ParseTuple (args, "k", &clength))
1551
if (clength > 4 || clength < 1) {
1552
return RAISE (PyExc_ValueError, "Length needs to be 1,2,3, or 4.");
1555
color->len = clength;
1595
static PyObject * _color_subscript(PyColor* self, PyObject* item) {
1598
#if PY_VERSION_HEX < 0x02050000
1599
if (PyInt_Check(item)) {
1603
if (PyIndex_Check(item)) {
1605
i = PyNumber_AsSsize_t(item, PyExc_IndexError);
1608
if (i == -1 && PyErr_Occurred())
1612
i += PyList_GET_SIZE(self);
1614
return _color_item(self, i);
1616
if (PySlice_Check(item)) {
1618
Py_ssize_t start, stop, step, slicelength;
1620
if (PySlice_GetIndicesEx((PySliceObject*)item, len, &start, &stop, &step, &slicelength) < 0)
1623
if (slicelength <= 0) {
1624
return PyTuple_New(0);
1626
else if (step == 1) {
1627
return _color_slice(self, start, stop);
1630
PyErr_SetString(PyExc_TypeError, "slice steps not supported");
1635
PyErr_Format(PyExc_TypeError,
1636
"Color indices must be integers, not %.200s",
1637
item->ob_type->tp_name);
1312
1650
_color_ass_item (PyColor *color, Py_ssize_t _index, PyObject *value)
1314
1653
switch (_index)
1673
_color_slice(register PyColor *a,
1674
register Py_ssize_t ilow,
1675
register Py_ssize_t ihigh)
1679
Py_ssize_t c1, c2, c3, c4;
1680
c1=0;c2=0;c3=0;c4=0;
1682
/* printf("ilow :%d:, ihigh:%d:\n", ilow, ihigh); */
1692
/* printf("2 ilow :%d:, ihigh:%d: len:%d:\n", ilow, ihigh, len); */
1699
} else if(ilow == 1) {
1704
} else if(ilow == 2) {
1708
} else if(ilow == 3) {
1714
/* return a tuple depending on which elements are wanted. */
1716
return Py_BuildValue ("(iiii)",c1,c2,c3,c4);
1717
} else if(len == 3) {
1718
return Py_BuildValue ("(iii)",c1,c2,c3);
1719
} else if(len == 2) {
1720
return Py_BuildValue ("(ii)",c1,c2);
1721
} else if(len == 1) {
1722
return Py_BuildValue ("(i)",c1);
1724
return Py_BuildValue ("()");
1740
_color_richcompare(PyObject *o1, PyObject *o2, int opid)
1742
Uint8 rgba1[4], rgba2[4];
1744
switch (_coerce_obj (o1, rgba1))
1753
switch (_coerce_obj (o2, rgba2))
1766
return PyBool_FromLong (*((Uint32 *) rgba1) == *((Uint32 *) rgba2));
1768
return PyBool_FromLong (*((Uint32 *) rgba1) != *((Uint32 *) rgba2));
1774
Py_INCREF (Py_NotImplemented);
1775
return Py_NotImplemented;
1331
1778
/**** C API interfaces ****/
1332
1779
static PyObject*
1333
1780
PyColor_New (Uint8 rgba[])
1335
1782
return (PyObject *) _color_new_internal (&PyColor_Type, rgba);
1786
PyColor_NewLength (Uint8 rgba[], Uint8 length)
1788
if(length < 1 || length > 4) {
1792
return (PyObject *) _color_new_internal_length (&PyColor_Type, rgba, length);
1339
1799
RGBAFromColorObj (PyObject *color, Uint8 rgba[])
1350
1810
return RGBAFromObj (color, rgba);
1354
void initcolor (void)
1813
/*DOC*/ static char _color_doc[] =
1814
/*DOC*/ "color module for pygame";
1816
MODINIT_DEFINE (color)
1356
1818
PyObject *colordict;
1357
1819
PyObject *module;
1359
1820
PyObject *apiobj;
1360
1821
static void* c_api[PYGAMEAPI_COLOR_NUMSLOTS];
1362
if (PyType_Ready (&PyColor_Type) < 0)
1365
/* create the module */
1366
module = Py_InitModule3 ("color", NULL, "color module for pygame");
1367
PyColor_Type.tp_getattro = PyObject_GenericGetAttr;
1368
Py_INCREF (&PyColor_Type);
1369
PyModule_AddObject (module, "Color", (PyObject *) &PyColor_Type);
1370
dict = PyModule_GetDict (module);
1824
static struct PyModuleDef _module = {
1825
PyModuleDef_HEAD_INIT,
1830
NULL, NULL, NULL, NULL
1834
/* imported needed apis; Do this first so if there is an error
1835
the module is not loaded.
1837
import_pygame_base ();
1838
if (PyErr_Occurred ()) {
1372
1842
colordict = PyImport_ImportModule ("pygame.colordict");
1375
1845
PyObject *_dict = PyModule_GetDict (colordict);
1376
1846
PyObject *colors = PyDict_GetItemString (_dict, "THECOLORS");
1377
1847
Py_INCREF (colors);
1378
Py_INCREF (colors); /* Needed for the _AddObject call beneath */
1379
1848
_COLORDICT = colors;
1380
PyModule_AddObject (module, "THECOLORS", colors);
1381
1849
Py_DECREF (colordict);
1384
import_pygame_base ();
1856
/* type preparation */
1857
if (PyType_Ready (&PyColor_Type) < 0)
1859
Py_DECREF (_COLORDICT);
1863
/* create the module */
1865
module = PyModule_Create (&_module);
1867
module = Py_InitModule3 ( MODPREFIX "color", NULL, _color_doc);
1869
if (module == NULL) {
1870
Py_DECREF (_COLORDICT);
1873
PyColor_Type.tp_getattro = PyObject_GenericGetAttr;
1874
Py_INCREF (&PyColor_Type);
1875
if (PyModule_AddObject (module, "Color", (PyObject *) &PyColor_Type)) {
1876
Py_DECREF (&PyColor_Type);
1877
Py_DECREF (_COLORDICT);
1881
Py_INCREF (_COLORDICT);
1882
if (PyModule_AddObject (module, "THECOLORS", _COLORDICT)) {
1883
Py_DECREF (_COLORDICT);
1884
Py_DECREF (_COLORDICT);
1386
1889
c_api[0] = &PyColor_Type;
1387
1890
c_api[1] = PyColor_New;
1388
1891
c_api[2] = RGBAFromColorObj;
1892
c_api[3] = PyColor_NewLength;
1390
1894
apiobj = PyCObject_FromVoidPtr (c_api, NULL);
1391
PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
1895
if (apiobj == NULL) {
1896
Py_DECREF (_COLORDICT);
1900
if (PyModule_AddObject (module, PYGAMEAPI_LOCAL_ENTRY, apiobj)) {
1902
Py_DECREF (_COLORDICT);
1906
MODINIT_RETURN (module);