|
1
|
|
|
""" |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
""" |
|
4
|
|
|
|
|
5
|
|
|
from __future__ import absolute_import |
|
6
|
|
|
|
|
7
|
|
|
from tcod.libtcod import ffi, lib |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class Color(list): |
|
11
|
|
|
""" |
|
12
|
|
|
|
|
13
|
|
|
Args: |
|
14
|
|
|
r (int): Red value, from 0 to 255. |
|
15
|
|
|
g (int): Green value, from 0 to 255. |
|
16
|
|
|
b (int): Blue value, from 0 to 255. |
|
17
|
|
|
""" |
|
18
|
|
|
|
|
19
|
|
|
def __init__(self, r=0, g=0, b=0): |
|
|
|
|
|
|
20
|
|
|
self[:] = (r & 0xff, g & 0xff, b & 0xff) |
|
21
|
|
|
|
|
22
|
|
|
@property |
|
23
|
|
|
def r(self): |
|
|
|
|
|
|
24
|
|
|
"""int: Red value, always normalised to 0-255.""" |
|
25
|
|
|
return self[0] |
|
26
|
|
|
|
|
27
|
|
|
@r.setter |
|
28
|
|
|
def r(self, value): |
|
|
|
|
|
|
29
|
|
|
self[0] = value & 0xff |
|
30
|
|
|
|
|
31
|
|
|
@property |
|
32
|
|
|
def g(self): |
|
|
|
|
|
|
33
|
|
|
"""int: Green value, always normalised to 0-255.""" |
|
34
|
|
|
return self[1] |
|
35
|
|
|
@g.setter |
|
36
|
|
|
def g(self, value): |
|
|
|
|
|
|
37
|
|
|
self[1] = value & 0xff |
|
38
|
|
|
|
|
39
|
|
|
@property |
|
40
|
|
|
def b(self): |
|
|
|
|
|
|
41
|
|
|
"""int: Blue value, always normalised to 0-255.""" |
|
42
|
|
|
return self[2] |
|
43
|
|
|
@b.setter |
|
44
|
|
|
def b(self, value): |
|
|
|
|
|
|
45
|
|
|
self[2] = value & 0xff |
|
46
|
|
|
|
|
47
|
|
|
@classmethod |
|
48
|
|
|
def _new_from_cdata(cls, cdata): |
|
49
|
|
|
"""new in libtcod-cffi""" |
|
50
|
|
|
return cls(cdata.r, cdata.g, cdata.b) |
|
51
|
|
|
|
|
52
|
|
|
def __getitem__(self, index): |
|
53
|
|
|
try: |
|
54
|
|
|
return list.__getitem__(self, index) |
|
55
|
|
|
except TypeError: |
|
56
|
|
|
return list.__getitem__(self, 'rgb'.index(index)) |
|
57
|
|
|
|
|
58
|
|
|
def __setitem__(self, index, value): |
|
59
|
|
|
try: |
|
60
|
|
|
list.__setitem__(self, index, value) |
|
61
|
|
|
except TypeError: |
|
62
|
|
|
list.__setitem__(self, 'rgb'.index(index), value) |
|
63
|
|
|
|
|
64
|
|
|
def __eq__(self, other): |
|
65
|
|
|
"""Compare equality between colors. |
|
66
|
|
|
|
|
67
|
|
|
Also compares with standard sequences such as 3-item tuples or lists. |
|
68
|
|
|
""" |
|
69
|
|
|
try: |
|
70
|
|
|
return bool(lib.TCOD_color_equals(self, other)) |
|
71
|
|
|
except TypeError: |
|
72
|
|
|
return False |
|
73
|
|
|
|
|
74
|
|
|
def __add__(self, other): |
|
75
|
|
|
"""Add two colors together.""" |
|
76
|
|
|
return Color._new_from_cdata(lib.TCOD_color_add(self, other)) |
|
77
|
|
|
|
|
78
|
|
|
def __sub__(self, other): |
|
79
|
|
|
"""Subtract one color from another.""" |
|
80
|
|
|
return Color._new_from_cdata(lib.TCOD_color_subtract(self, other)) |
|
81
|
|
|
|
|
82
|
|
|
def __mul__(self, other): |
|
83
|
|
|
"""Multiply with a scaler or another color.""" |
|
84
|
|
|
if isinstance(other, (Color, list, tuple)): |
|
85
|
|
|
return Color._new_from_cdata(lib.TCOD_color_multiply(self, other)) |
|
86
|
|
|
else: |
|
87
|
|
|
return Color._new_from_cdata( |
|
88
|
|
|
lib.TCOD_color_multiply_scalar(self, other)) |
|
89
|
|
|
|
|
90
|
|
|
def __repr__(self): |
|
91
|
|
|
"""Return a printable representation of the current color.""" |
|
92
|
|
|
return "%s(%i,%i,%i)" % (self.__class__.__name__, |
|
93
|
|
|
self.r, self.g, self.b) |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
def _import_colors(lib): |
|
|
|
|
|
|
97
|
|
|
"""Import all Color constants from lib into this module.""" |
|
98
|
|
|
g = globals() |
|
|
|
|
|
|
99
|
|
|
for name in dir(lib): |
|
100
|
|
|
if name[:5] != 'TCOD_': |
|
101
|
|
|
continue |
|
102
|
|
|
value = getattr(lib, name) |
|
103
|
|
|
if not isinstance(value, ffi.CData): |
|
104
|
|
|
continue |
|
105
|
|
|
if ffi.typeof(value) != ffi.typeof('TCOD_color_t'): |
|
106
|
|
|
continue |
|
107
|
|
|
g[name[5:]] = Color._new_from_cdata(value) |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
_import_colors(lib) |
|
111
|
|
|
|