~ubuntu-branches/ubuntu/natty/pygame/natty

« back to all changes in this revision

Viewing changes to src/color.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-01-14 17:02:11 UTC
  • mfrom: (1.3.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100114170211-21eop2ja7mr9vdcr
Tags: 1.9.1release-0ubuntu1
* New upstream version (lp: #433304)
* debian/control:
  - build-depends on libportmidi-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
#include "pygamedocs.h"
23
23
#include "pygame.h"
 
24
#include "pgcompat.h"
 
25
#include <ctype.h>
24
26
 
25
27
typedef struct
26
28
{
30
32
    Uint8 g;
31
33
    Uint8 b;
32
34
    Uint8 a;
 
35
    Uint8 len;
33
36
} PyColor;
34
37
 
 
38
typedef enum {
 
39
    TRISTATE_SUCCESS,
 
40
    TRISTATE_FAIL,
 
41
    TRISTATE_ERROR
 
42
} tristate;
 
43
 
35
44
static PyObject *_COLORDICT = NULL;
36
45
 
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[]);
41
51
 
42
52
static PyColor* _color_new_internal (PyTypeObject *type, Uint8 rgba[]);
 
53
static PyColor* _color_new_internal_length (PyTypeObject *type, Uint8 rgba[], Uint8 length);
 
54
 
43
55
static PyObject* _color_new (PyTypeObject *type, PyObject *args,
44
56
    PyObject *kwds);
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);
49
62
 
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);
 
88
#if !PY3
75
89
static int _color_coerce (PyObject **pv, PyObject **pw);
 
90
#endif
76
91
static PyObject* _color_int (PyColor *color);
77
92
static PyObject* _color_long (PyColor *color);
78
93
static PyObject* _color_float (PyColor *color);
 
94
#if !PY3
79
95
static PyObject* _color_oct (PyColor *color);
80
96
static PyObject* _color_hex (PyColor *color);
 
97
#endif
81
98
 
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);
 
106
 
 
107
/* Mapping protocol methods. */
 
108
static PyObject * _color_subscript(PyColor* self, PyObject* item);
 
109
 
 
110
 
 
111
/* Comparison */
 
112
static PyObject* _color_richcompare(PyObject *o1, PyObject *o2, int opid);
86
113
 
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[]);
90
118
 
91
119
/**
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 }
101
131
};
102
132
 
126
156
    (binaryfunc) _color_add, /* nb_add */
127
157
    (binaryfunc) _color_sub, /* nb_subtract */
128
158
    (binaryfunc) _color_mul, /* nb_multiply */
 
159
#if !PY3
129
160
    (binaryfunc) _color_div, /* nb_divide */
 
161
#endif
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 */
136
 
    0,                       /* nb_nonzero */
 
168
    0,                       /* nb_nonzero / nb_bool*/
137
169
    (unaryfunc) _color_inv,  /* nb_invert */
138
170
    0,                       /* nb_lshift */
139
171
    0,                       /* nb_rshift */
140
172
    0,                       /* nb_and */
141
173
    0,                       /* nb_xor */
142
174
    0,                       /* nb_or */
 
175
#if !PY3
143
176
    _color_coerce,           /* nb_coerce */
 
177
#endif
144
178
    (unaryfunc) _color_int,  /* nb_int */
145
179
    (unaryfunc) _color_long, /* nb_long */
146
180
    (unaryfunc) _color_float,/* nb_float */
 
181
#if !PY3
147
182
    (unaryfunc) _color_oct,  /* nb_oct */
148
183
    (unaryfunc) _color_hex,  /* nb_hex */
 
184
#endif
149
185
    0,                       /* nb_inplace_add */
150
186
    0,                       /* nb_inplace_subtract */
151
187
    0,                       /* nb_inplace_multiply */
 
188
#if !PY3
152
189
    0,                       /* nb_inplace_divide */
 
190
#endif
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 */
178
 
    NULL,                              /* sq_slice */
 
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 */
184
222
};
185
223
 
 
224
 
 
225
 
 
226
static PyMappingMethods _color_as_mapping = {
 
227
        (lenfunc) _color_length,
 
228
        (binaryfunc)_color_subscript,
 
229
        NULL
 
230
};
 
231
 
 
232
 
 
233
 
 
234
 
 
235
 
 
236
 
 
237
 
 
238
#define DEFERRED_ADDRESS(ADDR) 0
 
239
 
186
240
static PyTypeObject PyColor_Type =
187
241
{
188
 
    PyObject_HEAD_INIT(NULL)
189
 
    0,
 
242
    TYPE_HEAD (NULL, 0)
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
 
255
    0,
 
256
#else
 
257
    &_color_as_mapping,          /* tp_as_mapping */
 
258
#endif
202
259
    0,                          /* tp_hash */
203
260
    0,                          /* tp_call */
204
261
    0,                          /* tp_str */
209
266
    DOC_PYGAMECOLOR,            /* tp_doc */
210
267
    0,                          /* tp_traverse */
211
268
    0,                          /* tp_clear */
212
 
    0,                          /* tp_richcompare */
 
269
    _color_richcompare,         /* tp_richcompare */
213
270
    0,                          /* tp_weaklistoffset */
214
271
    0,                          /* tp_iter */
215
272
    0,                          /* tp_iternext */
224
281
    0,                          /* tp_init */
225
282
    0,                          /* tp_alloc */
226
283
    _color_new,                 /* tp_new */
 
284
#ifndef __SYMBIAN32__
227
285
    0,                          /* tp_free */
228
286
    0,                          /* tp_is_gc */
229
287
    0,                          /* tp_bases */
232
290
    0,                          /* tp_subclasses */
233
291
    0,                          /* tp_weaklist */
234
292
    0                           /* tp_del */
 
293
#endif    
235
294
};
236
295
 
237
296
#define PyColor_Check(o) \
260
319
    if (!val || !color)
261
320
        return 0;
262
321
 
 
322
#if !PY3
263
323
    if (PyInt_Check (val))
264
324
    {
265
325
        long intval = PyInt_AsLong (val);
271
331
        *color = (Uint32) intval;
272
332
        return 1;
273
333
    }
274
 
    else if (PyLong_Check (val))
 
334
#endif
 
335
    if (PyLong_Check (val))
275
336
    {
276
337
        unsigned long longval = PyLong_AsUnsignedLong (val);
277
338
        if (PyErr_Occurred ())
282
343
        *color = (Uint32) longval;
283
344
        return 1;
284
345
    }
285
 
    else
286
 
        PyErr_SetString (PyExc_ValueError, "invalid color argument");
 
346
    
 
347
    /* Failed */
 
348
    PyErr_SetString (PyExc_ValueError, "invalid color argument");
287
349
    return 0;
288
350
}
289
351
 
290
352
static int
291
353
_hextoint (char *hex, Uint8 *val)
292
354
{
293
 
    char part[3] = { '\0' };
294
 
    char *eptr;
295
 
    part[0] = hex[0];
296
 
    part[1] = hex[1];
297
 
 
298
 
    *val = strtol (part, &eptr, 16);
299
 
    if (eptr == part) /* Failure */
300
 
        return 0;
 
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.
 
358
     */
 
359
    Uint8 temp = 0;
 
360
 
 
361
    switch (toupper(hex[0]))
 
362
    {
 
363
    case '0':
 
364
        break;
 
365
    case '1':
 
366
        temp += 0x10;
 
367
        break;
 
368
    case '2':
 
369
        temp += 0x20;
 
370
        break;
 
371
    case '3':
 
372
        temp += 0x30;
 
373
        break;
 
374
    case '4':
 
375
        temp += 0x40;
 
376
        break;
 
377
    case '5':
 
378
        temp += 0x50;
 
379
        break;
 
380
    case '6':
 
381
        temp += 0x60;
 
382
        break;
 
383
    case '7':
 
384
        temp += 0x70;
 
385
        break;
 
386
    case '8':
 
387
        temp += 0x80;
 
388
        break;
 
389
    case '9':
 
390
        temp += 0x90;
 
391
        break;
 
392
    case 'A':
 
393
        temp += 0xA0;
 
394
        break;
 
395
    case 'B':
 
396
        temp += 0xB0;
 
397
        break;
 
398
    case 'C':
 
399
        temp += 0xC0;
 
400
        break;
 
401
    case 'D':
 
402
        temp += 0xD0;
 
403
        break;
 
404
    case 'E':
 
405
        temp += 0xE0;
 
406
        break;
 
407
    case 'F':
 
408
        temp += 0xF0;
 
409
        break;
 
410
    default:
 
411
        return 0;
 
412
    }
 
413
 
 
414
    switch (toupper(hex[1]))
 
415
    {
 
416
    case '0':
 
417
        break;
 
418
    case '1':
 
419
        temp += 0x01;
 
420
        break;
 
421
    case '2':
 
422
        temp += 0x02;
 
423
        break;
 
424
    case '3':
 
425
        temp += 0x03;
 
426
        break;
 
427
    case '4':
 
428
        temp += 0x04;
 
429
        break;
 
430
    case '5':
 
431
        temp += 0x05;
 
432
        break;
 
433
    case '6':
 
434
        temp += 0x06;
 
435
        break;
 
436
    case '7':
 
437
        temp += 0x07;
 
438
        break;
 
439
    case '8':
 
440
        temp += 0x08;
 
441
        break;
 
442
    case '9':
 
443
        temp += 0x09;
 
444
        break;
 
445
    case 'A':
 
446
        temp += 0x0A;
 
447
        break;
 
448
    case 'B':
 
449
        temp += 0x0B;
 
450
        break;
 
451
    case 'C':
 
452
        temp += 0x0C;
 
453
        break;
 
454
    case 'D':
 
455
        temp += 0x0D;
 
456
        break;
 
457
    case 'E':
 
458
        temp += 0x0E;
 
459
        break;
 
460
    case 'F':
 
461
        temp += 0x0F;
 
462
        break;
 
463
    default:
 
464
        return 0;
 
465
    }
 
466
 
 
467
    *val = temp;
301
468
    return 1;
302
469
}
303
470
 
304
 
static int
 
471
static tristate
305
472
_hexcolor (PyObject *color, Uint8 rgba[])
306
473
{
307
474
    size_t len;
308
 
    char *name = PyString_AsString (color);
309
 
    if (!name)
310
 
        return 0;
 
475
    tristate rcode = TRISTATE_FAIL;
 
476
    char *name;
 
477
#if PY3
 
478
    PyObject* ascii = PyUnicode_AsASCIIString (color);
 
479
    if (ascii == NULL) {
 
480
        rcode = TRISTATE_ERROR;
 
481
        goto Fail;
 
482
    }
 
483
    name = PyBytes_AsString (ascii);
 
484
#else
 
485
    name = PyString_AsString (color);
 
486
#endif
 
487
    if (name == NULL) {
 
488
        goto Fail;
 
489
    }
311
490
 
312
491
    len = strlen (name);
313
492
    /* hex colors can be
316
495
     * 0xRRGGBB
317
496
     * 0xRRGGBBAA
318
497
     */
319
 
    if (len < 7)
320
 
        return 0;
 
498
    if (len < 7) {
 
499
        goto Fail;
 
500
    }
321
501
 
322
502
    if (name[0] == '#')
323
503
    {
324
504
        if (len != 7 && len != 9)
325
 
            return 0;
 
505
            goto Fail;
326
506
        if (!_hextoint (name + 1, &rgba[0]))
327
 
            return 0;
 
507
            goto Fail;
328
508
        if (!_hextoint (name + 3, &rgba[1]))
329
 
            return 0;
 
509
            goto Fail;
330
510
        if (!_hextoint (name + 5, &rgba[2]))
331
 
            return 0;
332
 
        rgba[3] = 0;
 
511
            goto Fail;
 
512
        rgba[3] = 255;
333
513
        if (len == 9 && !_hextoint (name + 7, &rgba[3]))
334
 
            return 0;
335
 
        return 1;
 
514
            goto Fail;
 
515
        goto Success;
336
516
    }
337
517
    else if (name[0] == '0' && name[1] == 'x')
338
518
    {
339
519
        if (len != 8 && len != 10)
340
 
            return 0;
 
520
            goto Fail;
341
521
        if (!_hextoint (name + 2, &rgba[0]))
342
 
            return 0;
 
522
            goto Fail;
343
523
        if (!_hextoint (name + 4, &rgba[1]))
344
 
            return 0;
 
524
            goto Fail;
345
525
        if (!_hextoint (name + 6, &rgba[2]))
346
 
            return 0;
347
 
        rgba[3] = 0;
 
526
            goto Fail;
 
527
        rgba[3] = 255;
348
528
        if (len == 10 && !_hextoint (name + 8, &rgba[3]))
349
 
            return 0;
350
 
        return 1;
351
 
    }
 
529
            goto Fail;
 
530
        goto Success;
 
531
    }
 
532
    goto Fail;
 
533
 
 
534
Success:
 
535
    rcode = TRISTATE_SUCCESS;
 
536
Fail:
 
537
#if PY3
 
538
    Py_XDECREF (ascii);
 
539
#endif
 
540
    return rcode;
 
541
}
 
542
 
 
543
static int
 
544
_coerce_obj (PyObject *obj, Uint8 rgba[])
 
545
{
 
546
    if (PyType_IsSubtype (obj->ob_type, &PyColor_Type))
 
547
    {
 
548
        rgba[0] = ((PyColor *) obj)->r;
 
549
        rgba[1] = ((PyColor *) obj)->g;
 
550
        rgba[2] = ((PyColor *) obj)->b;
 
551
        rgba[3] = ((PyColor *) obj)->a;
 
552
        return 1;
 
553
    }
 
554
    else if (PyType_IsSubtype (obj->ob_type, &PyTuple_Type))
 
555
    {
 
556
        if (RGBAFromObj (obj, rgba))
 
557
        {
 
558
            return 1;
 
559
        }
 
560
        else if (PyErr_Occurred ())
 
561
        {
 
562
            return -1;
 
563
        }
 
564
    }
 
565
 
352
566
    return 0;
353
567
}
354
568
 
355
569
static PyColor*
356
570
_color_new_internal (PyTypeObject *type, Uint8 rgba[])
357
571
{
 
572
    /* default length of 4 - r,g,b,a. */
 
573
    return _color_new_internal_length(type, rgba, 4);
 
574
}
 
575
 
 
576
 
 
577
static PyColor*
 
578
_color_new_internal_length (PyTypeObject *type, Uint8 rgba[], Uint8 length)
 
579
{
358
580
    PyColor *color = (PyColor *) type->tp_alloc (type, 0);
359
581
    if (!color)
360
582
        return NULL;
363
585
    color->g = rgba[1];
364
586
    color->b = rgba[2];
365
587
    color->a = rgba[3];
 
588
    color->len = length;
366
589
 
367
590
    return color;
368
591
}
379
602
    if (!PyArg_ParseTuple (args, "O|OOO", &obj, &obj1, &obj2, &obj3))
380
603
        return NULL;
381
604
 
382
 
    if (PyString_Check (obj))
 
605
    if (Text_Check (obj))
383
606
    {
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");
388
612
        
389
 
        color = PyDict_GetItem (_COLORDICT, obj);
 
613
        name1 = PyObject_CallMethod(obj, "replace", "(ss)", " ", "");
 
614
        if (!name1)
 
615
        {
 
616
            return NULL;
 
617
        }
 
618
        name2 = PyObject_CallMethod(name1, "lower", NULL);
 
619
        Py_DECREF(name1);
 
620
        if (!name2)
 
621
        {
 
622
            return NULL;
 
623
        }
 
624
        color = PyDict_GetItem (_COLORDICT, name2);
 
625
        Py_DECREF(name2);
390
626
        if (!color)
391
627
        {
392
 
            if (!_hexcolor (obj, rgba))
 
628
            switch (_hexcolor (obj, rgba))
 
629
            {
 
630
                case TRISTATE_FAIL:
393
631
                return RAISE (PyExc_ValueError, "invalid color name");
 
632
                case TRISTATE_ERROR:
 
633
                return NULL;
 
634
                default:
 
635
                break;
 
636
            }
394
637
        }
395
638
        else if (!RGBAFromObj (color, rgba))
 
639
        {
396
640
            return RAISE (PyExc_ValueError, "invalid color");
 
641
        }
397
642
 
398
643
        return (PyObject *) _color_new_internal (type, rgba);
399
644
    }
447
692
static void
448
693
_color_dealloc (PyColor *color)
449
694
{
450
 
    color->ob_type->tp_free ((PyObject *) color);
 
695
    Py_TYPE(color)->tp_free ((PyObject *) color);
451
696
}
452
697
 
453
698
/**
460
705
    char buf[21];
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);
464
709
}
465
710
 
466
711
/**
1184
1429
    return (PyObject*) _color_new_internal (&PyColor_Type, rgba);
1185
1430
}
1186
1431
 
 
1432
#if !PY3
1187
1433
/**
1188
1434
 * coerce (color1, color2)
1189
1435
 */
1198
1444
    }
1199
1445
    return 1;
1200
1446
}
 
1447
#endif
1201
1448
 
1202
1449
/**
1203
1450
 * int(color)
1205
1452
static PyObject*
1206
1453
_color_int (PyColor *color)
1207
1454
{
1208
 
    unsigned long tmp = (color->r << 24) + (color->g << 16) + (color->b << 8) +
1209
 
        color->a;
1210
 
    if (tmp < INT_MAX)
 
1455
    Uint32 tmp = (color->r << 24) + (color->g << 16) + 
 
1456
                 (color->b << 8) + color->a;
 
1457
#if !PY3
 
1458
    if (tmp < LONG_MAX)
1211
1459
        return PyInt_FromLong ((long) tmp);
 
1460
#endif
1212
1461
    return PyLong_FromUnsignedLong (tmp);
1213
1462
}
1214
1463
 
1218
1467
static PyObject*
1219
1468
_color_long (PyColor *color)
1220
1469
{
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) +
1222
1471
        color->a;
1223
1472
    return PyLong_FromUnsignedLong (tmp);
1224
1473
}
1229
1478
static PyObject*
1230
1479
_color_float (PyColor *color)
1231
1480
{
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) +
1233
1482
        color->a;
1234
1483
    return PyFloat_FromDouble ((double) tmp);
1235
1484
}
1236
1485
 
 
1486
#if !PY3
1237
1487
/**
1238
1488
 * oct(color)
1239
1489
 */
1241
1491
_color_oct (PyColor *color)
1242
1492
{
1243
1493
    char buf[100];
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) +
1245
1495
        color->a;
1246
 
    if (tmp < INT_MAX)
1247
 
        PyOS_snprintf (buf, sizeof (buf), "0%lo", tmp);
 
1496
 
 
1497
    if (tmp < LONG_MAX)
 
1498
        PyOS_snprintf (buf, sizeof (buf), "0%lo", (unsigned long) tmp);
1248
1499
    else
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);
1251
1502
}
1252
1503
 
1257
1508
_color_hex (PyColor *color)
1258
1509
{
1259
1510
    char buf[100];
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) +
1261
1512
        color->a;
1262
 
    if (tmp < INT_MAX)
1263
 
        PyOS_snprintf (buf, sizeof (buf), "0x%lx", tmp);
 
1513
    if (tmp < LONG_MAX)
 
1514
        PyOS_snprintf (buf, sizeof (buf), "0x%lx", (unsigned long) tmp);
1264
1515
    else
1265
1516
    {
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);
1268
1519
#else
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);
1271
1522
#endif
1272
1523
    }
1273
 
    return PyString_FromString (buf);
 
1524
    return Text_FromUTF8 (buf);
1274
1525
}
 
1526
#endif
1275
1527
 
1276
1528
/* Sequence protocol methods */
1277
1529
 
1281
1533
static Py_ssize_t
1282
1534
_color_length (PyColor *color)
1283
1535
{
1284
 
    return 4;
1285
 
}
 
1536
    return color->len;
 
1537
}
 
1538
 
 
1539
/**
 
1540
 * color.set_length(3)
 
1541
 */
 
1542
 
 
1543
static PyObject*
 
1544
_color_set_length (PyColor *color, PyObject *args)
 
1545
{
 
1546
    Py_ssize_t clength;
 
1547
 
 
1548
    if (!PyArg_ParseTuple (args, "k", &clength))
 
1549
        return NULL;
 
1550
 
 
1551
    if (clength > 4 || clength < 1) {
 
1552
        return RAISE (PyExc_ValueError, "Length needs to be 1,2,3, or 4.");
 
1553
    }
 
1554
 
 
1555
    color->len = clength;
 
1556
 
 
1557
    Py_RETURN_NONE;
 
1558
}
 
1559
 
 
1560
 
 
1561
 
1286
1562
 
1287
1563
/**
1288
1564
 * color[x]
1290
1566
static PyObject*
1291
1567
_color_item (PyColor *color, Py_ssize_t _index)
1292
1568
{
 
1569
 
 
1570
    if((_index > (color->len-1)) ) {
 
1571
        return RAISE (PyExc_IndexError, "invalid index");
 
1572
    }
 
1573
 
1293
1574
    switch (_index)
1294
1575
    {
1295
1576
    case 0:
1305
1586
    }
1306
1587
}
1307
1588
 
 
1589
 
 
1590
 
 
1591
 
 
1592
 
 
1593
 
 
1594
 
 
1595
static PyObject * _color_subscript(PyColor* self, PyObject* item) {
 
1596
 
 
1597
 
 
1598
#if PY_VERSION_HEX < 0x02050000
 
1599
    if (PyInt_Check(item)) {
 
1600
        Py_ssize_t i;
 
1601
        i = 0;
 
1602
#else
 
1603
    if (PyIndex_Check(item)) {
 
1604
        Py_ssize_t i;
 
1605
        i = PyNumber_AsSsize_t(item, PyExc_IndexError);
 
1606
#endif
 
1607
 
 
1608
        if (i == -1 && PyErr_Occurred())
 
1609
            return NULL;
 
1610
        /*
 
1611
        if (i < 0)
 
1612
            i += PyList_GET_SIZE(self);
 
1613
        */
 
1614
        return _color_item(self, i);
 
1615
    }
 
1616
    if (PySlice_Check(item)) {
 
1617
        int len= 4;
 
1618
        Py_ssize_t start, stop, step, slicelength;
 
1619
 
 
1620
        if (PySlice_GetIndicesEx((PySliceObject*)item, len, &start, &stop, &step, &slicelength) < 0)
 
1621
            return NULL;
 
1622
 
 
1623
        if (slicelength <= 0) {
 
1624
            return PyTuple_New(0);
 
1625
        }
 
1626
        else if (step == 1) {
 
1627
            return _color_slice(self, start, stop);
 
1628
        }
 
1629
        else {
 
1630
            PyErr_SetString(PyExc_TypeError, "slice steps not supported");
 
1631
            return NULL;
 
1632
        }
 
1633
    }
 
1634
    else {
 
1635
        PyErr_Format(PyExc_TypeError,
 
1636
                     "Color indices must be integers, not %.200s",
 
1637
                     item->ob_type->tp_name);
 
1638
        return NULL;
 
1639
    }
 
1640
}
 
1641
 
 
1642
 
 
1643
 
 
1644
 
 
1645
 
1308
1646
/**
1309
1647
 * color[x] = y
1310
1648
 */
1311
1649
static int
1312
1650
_color_ass_item (PyColor *color, Py_ssize_t _index, PyObject *value)
1313
1651
{
 
1652
 
1314
1653
    switch (_index)
1315
1654
    {
1316
1655
    case 0:
1328
1667
    return -1;
1329
1668
}
1330
1669
 
 
1670
 
 
1671
 
 
1672
static PyObject *
 
1673
_color_slice(register PyColor *a, 
 
1674
             register Py_ssize_t ilow, 
 
1675
             register Py_ssize_t ihigh)
 
1676
{
 
1677
 
 
1678
        Py_ssize_t len;
 
1679
        Py_ssize_t c1, c2, c3, c4;
 
1680
        c1=0;c2=0;c3=0;c4=0;
 
1681
 
 
1682
        /* printf("ilow :%d:, ihigh:%d:\n", ilow, ihigh); */
 
1683
 
 
1684
        if (ilow < 0)
 
1685
                ilow = 0;
 
1686
        if (ihigh > 3)
 
1687
                ihigh = 4;
 
1688
        if (ihigh < ilow)
 
1689
                ihigh = ilow;
 
1690
 
 
1691
        len = ihigh - ilow;
 
1692
        /* printf("2 ilow :%d:, ihigh:%d: len:%d:\n", ilow, ihigh, len); */
 
1693
        
 
1694
        if(ilow == 0) {
 
1695
            c1 = a->r;
 
1696
            c2 = a->g;
 
1697
            c3 = a->b;
 
1698
            c4 = a->a;
 
1699
        } else if(ilow == 1) {
 
1700
            c1 = a->g;
 
1701
            c2 = a->b;
 
1702
            c3 = a->a;
 
1703
 
 
1704
        } else if(ilow == 2) {
 
1705
            c1 = a->b;
 
1706
            c2 = a->a;
 
1707
 
 
1708
        } else if(ilow == 3) {
 
1709
            c1 = a->a;
 
1710
        }
 
1711
 
 
1712
 
 
1713
 
 
1714
        /* return a tuple depending on which elements are wanted.  */
 
1715
        if(len == 4) {
 
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);
 
1723
        } else {
 
1724
            return Py_BuildValue ("()");
 
1725
        }
 
1726
}
 
1727
 
 
1728
 
 
1729
 
 
1730
 
 
1731
 
 
1732
 
 
1733
 
 
1734
 
 
1735
/*
 
1736
 * colorA == colorB
 
1737
 * colorA != colorB
 
1738
 */
 
1739
static PyObject*
 
1740
_color_richcompare(PyObject *o1, PyObject *o2, int opid)
 
1741
{
 
1742
    Uint8 rgba1[4], rgba2[4];
 
1743
 
 
1744
    switch (_coerce_obj (o1, rgba1))
 
1745
    {
 
1746
    case -1:
 
1747
        return 0;
 
1748
    case 0:
 
1749
        goto Unimplemented;
 
1750
    default:
 
1751
        break;
 
1752
    }
 
1753
    switch (_coerce_obj (o2, rgba2))
 
1754
    {
 
1755
    case -1:
 
1756
        return 0;
 
1757
    case 0:
 
1758
        goto Unimplemented;
 
1759
    default:
 
1760
        break;
 
1761
    }
 
1762
 
 
1763
    switch (opid)
 
1764
    {
 
1765
    case Py_EQ:
 
1766
        return PyBool_FromLong (*((Uint32 *) rgba1) == *((Uint32 *) rgba2));
 
1767
    case Py_NE:
 
1768
        return PyBool_FromLong (*((Uint32 *) rgba1) != *((Uint32 *) rgba2));
 
1769
    default:
 
1770
        break;
 
1771
    }
 
1772
 
 
1773
Unimplemented:
 
1774
    Py_INCREF (Py_NotImplemented);
 
1775
    return Py_NotImplemented;
 
1776
}
 
1777
 
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);
1336
1783
}
1337
1784
 
 
1785
static PyObject*
 
1786
PyColor_NewLength (Uint8 rgba[], Uint8 length)
 
1787
{
 
1788
    if(length < 1 || length > 4) {
 
1789
        return NULL;
 
1790
    }
 
1791
 
 
1792
    return (PyObject *) _color_new_internal_length (&PyColor_Type, rgba, length);
 
1793
}
 
1794
 
 
1795
 
 
1796
 
 
1797
 
1338
1798
static int
1339
1799
RGBAFromColorObj (PyObject *color, Uint8 rgba[])
1340
1800
{
1350
1810
        return RGBAFromObj (color, rgba);
1351
1811
}
1352
1812
 
1353
 
PYGAME_EXPORT
1354
 
void initcolor (void)
 
1813
/*DOC*/ static char _color_doc[] =
 
1814
/*DOC*/    "color module for pygame";
 
1815
 
 
1816
MODINIT_DEFINE (color)
1355
1817
{
1356
1818
    PyObject *colordict;
1357
1819
    PyObject *module;
1358
 
    PyObject *dict;
1359
1820
    PyObject *apiobj;
1360
1821
    static void* c_api[PYGAMEAPI_COLOR_NUMSLOTS];
1361
 
 
1362
 
    if (PyType_Ready (&PyColor_Type) < 0)
1363
 
        return;
1364
1822
    
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);
 
1823
#if PY3
 
1824
    static struct PyModuleDef _module = {
 
1825
        PyModuleDef_HEAD_INIT,
 
1826
        "color",
 
1827
        _color_doc,
 
1828
        -1,
 
1829
        _color_methods,
 
1830
        NULL, NULL, NULL, NULL
 
1831
    };
 
1832
#endif
 
1833
 
 
1834
    /* imported needed apis; Do this first so if there is an error
 
1835
       the module is not loaded.
 
1836
    */
 
1837
    import_pygame_base ();
 
1838
    if (PyErr_Occurred ()) {
 
1839
        MODINIT_ERROR;
 
1840
    }
1371
1841
 
1372
1842
    colordict = PyImport_ImportModule ("pygame.colordict");
1373
1843
    if (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);
1382
1850
    }
1383
 
 
1384
 
    import_pygame_base ();
 
1851
    else
 
1852
    {
 
1853
        MODINIT_ERROR;
 
1854
    }
 
1855
    
 
1856
    /* type preparation */
 
1857
    if (PyType_Ready (&PyColor_Type) < 0)
 
1858
    {
 
1859
        Py_DECREF (_COLORDICT);
 
1860
        MODINIT_ERROR;
 
1861
    }
 
1862
    
 
1863
    /* create the module */
 
1864
#if PY3
 
1865
    module = PyModule_Create (&_module);
 
1866
#else
 
1867
    module = Py_InitModule3 ( MODPREFIX "color", NULL, _color_doc);
 
1868
#endif
 
1869
    if (module == NULL) {
 
1870
        Py_DECREF (_COLORDICT);
 
1871
        MODINIT_ERROR;
 
1872
    }
 
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);
 
1878
        DECREF_MOD(module);
 
1879
        MODINIT_ERROR;
 
1880
    }
 
1881
    Py_INCREF (_COLORDICT);
 
1882
    if (PyModule_AddObject (module, "THECOLORS", _COLORDICT)) {
 
1883
        Py_DECREF (_COLORDICT);
 
1884
        Py_DECREF (_COLORDICT);
 
1885
        DECREF_MOD(module);
 
1886
        MODINIT_ERROR;
 
1887
    }
1385
1888
 
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;
1389
1893
 
1390
1894
    apiobj = PyCObject_FromVoidPtr (c_api, NULL);
1391
 
    PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
1392
 
    Py_DECREF (apiobj);
 
1895
    if (apiobj == NULL) {
 
1896
        Py_DECREF (_COLORDICT);
 
1897
        DECREF_MOD(module);
 
1898
        MODINIT_ERROR;
 
1899
    }
 
1900
    if (PyModule_AddObject (module, PYGAMEAPI_LOCAL_ENTRY, apiobj)) {
 
1901
        Py_DECREF (apiobj);
 
1902
        Py_DECREF (_COLORDICT);
 
1903
        DECREF_MOD(module);
 
1904
        MODINIT_ERROR;
 
1905
    }
 
1906
    MODINIT_RETURN (module);
1393
1907
}