forked from libtcod/python-tcod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor.py
More file actions
93 lines (73 loc) · 2.48 KB
/
color.py
File metadata and controls
93 lines (73 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
"""
from __future__ import absolute_import
from tcod.libtcod import ffi, lib
class Color(list):
"""
Args:
r (int): Red value, from 0 to 255.
g (int): Green value, from 0 to 255.
b (int): Blue value, from 0 to 255.
"""
def __init__(self, r=0, g=0, b=0):
self[:] = (r & 0xff, g & 0xff, b & 0xff)
@property
def r(self):
"""int: Red value, always normalised to 0-255."""
return self[0]
@r.setter
def r(self, value):
self[0] = value & 0xff
@property
def g(self):
"""int: Green value, always normalised to 0-255."""
return self[1]
@g.setter
def g(self, value):
self[1] = value & 0xff
@property
def b(self):
"""int: Blue value, always normalised to 0-255."""
return self[2]
@b.setter
def b(self, value):
self[2] = value & 0xff
@classmethod
def _new_from_cdata(cls, cdata):
"""new in libtcod-cffi"""
return cls(cdata.r, cdata.g, cdata.b)
def __getitem__(self, index):
try:
return list.__getitem__(self, index)
except TypeError:
return list.__getitem__(self, 'rgb'.index(index))
def __setitem__(self, index, value):
try:
list.__setitem__(self, index, value)
except TypeError:
list.__setitem__(self, 'rgb'.index(index), value)
def __eq__(self, other):
"""Compare equality between colors.
Also compares with standard sequences such as 3-item tuples or lists.
"""
try:
return bool(lib.TCOD_color_equals(self, other))
except TypeError:
return False
def __add__(self, other):
"""Add two colors together."""
return Color._new_from_cdata(lib.TCOD_color_add(self, other))
def __sub__(self, other):
"""Subtract one color from another."""
return Color._new_from_cdata(lib.TCOD_color_subtract(self, other))
def __mul__(self, other):
"""Multiply with a scaler or another color."""
if isinstance(other, (Color, list, tuple)):
return Color._new_from_cdata(lib.TCOD_color_multiply(self, other))
else:
return Color._new_from_cdata(
lib.TCOD_color_multiply_scalar(self, other))
def __repr__(self):
"""Return a printable representation of the current color."""
return "%s(%i,%i,%i)" % (self.__class__.__name__,
self.r, self.g, self.b)