-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolor.py
179 lines (133 loc) · 5.49 KB
/
color.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
class Color(object):
"""
A class that defines a specific color.
A color is specified in RGBA space, and internally stored as a 4-list of
integers in the range 0-255. If an A value is not given, then it defaults
to opaque.
Colors can be specified by the user in the following manners:
================= =============
Format Description
================= =============
(r, g, b) Each value can be either an integer in the range 0-255, or a float in the range 0-1.
(r, g, b, a) Each value can be either an integer in the range 0-255, or a float in the range 0-1.
'#rrggbb' Each letter is a single hex value.
'#rrggbbaa' Each letter is a single hex value.
name A string representing a color name. Valid names are given below.
Color() A current Color object.
================= =============
=========== ===
Name
=========== ===
red
green
blue
black
white
=========== ===
If an invalid value is given, then the color defaults to black (0, 0, 0, 255).
"""
_namedColors = {'red': (255, 0, 0, 255),
'green': (0, 255, 0, 255),
'blue': (0, 0, 255, 255),
'black': (0, 0, 0, 255),
'white': (255, 255, 255, 255),
}
def __init__(self, color=None):
self._color = [0, 0, 0, 255]
self.setColor(color)
def color(self):
"""
Return the RGBA version of the color, as a 4-list of ints.
"""
return self.rgba()
def rgb(self):
"""
Return the RGB version of the color, as a 3-list of ints.
"""
return self._color[0:3]
def rgba(self):
"""
Return the RGBA version of the color, as a 4-list of ints.
"""
return self._color
def setColor(self, color):
"""
Generic method to set the color. Accepts the color in any format.
Return True if the color is set, False otherwise.
"""
# Check which format is being used, and call the appropriate method
if isinstance(color, Color):
return self.setColor(color.rgba())
elif isinstance(color, tuple) or isinstance(color, list):
if len(color) == 3:
return self.setRgb(color)
elif len(color) == 4:
return self.setRgba(color)
elif isinstance(color, str):
if color[0] == '#':
if len(color) == 7:
return self.setRgbHex(color)
elif len(color) == 9:
return self.setRgbaHex(color)
else:
return self.setName(color)
return False
def setRgb(self, color):
if not (isinstance(color, tuple) or isinstance(color, list)) or len(color) != 3:
return False
# Check if all values are ints
if all([isinstance(x, int) for x in color]):
# Check if all values are between 0 and 255
if all([(x >= 0 and x <= 255) for x in color]):
self._color[0:3] = color
return True
# Check if all values are floats
if all([isinstance(x, float) for x in color]):
# Check if all values are between 0 and 255
if all([(x >= 0.0 and x <= 1.0) for x in color]):
self._color[0:3] = [int(round(x * 255.)) for x in color]
return True
return False
def setRgba(self, color):
if not (isinstance(color, tuple) or isinstance(color, list)) or len(color) != 4:
return False
# Check if all values are ints
if all([isinstance(x, int) for x in color]):
# Check if all values are between 0 and 255
if all([(x >= 0 and x <= 255) for x in color]):
self._color[0:4] = color
return True
# Check if all values are floats
if all([isinstance(x, float) for x in color]):
# Check if all values are between 0 and 255
if all([(x >= 0.0 and x <= 1.0) for x in color]):
self._color[0:4] = [int(round(x * 255.)) for x in color]
return True
return False
def setRgbHex(self, color):
if not isinstance(color, str) or color[0] != '#' or len(color) != 7:
return False
hexValues = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')
if any([x not in hexValues for x in color[1:]]):
return False
self._color[0] = int(color[1:3], 16)
self._color[1] = int(color[3:5], 16)
self._color[2] = int(color[5:7], 16)
return True
def setRgbaHex(self, color):
if not isinstance(color, str) or color[0] != '#' or len(color) != 9:
return False
hexValues = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')
if any([x not in hexValues for x in color[1:]]):
return False
self._color[0] = int(color[1:3], 16)
self._color[1] = int(color[3:5], 16)
self._color[2] = int(color[5:7], 16)
self._color[3] = int(color[7:9], 16)
return True
def setName(self, color):
if not isinstance(color, str):
return False
if color in self._namedColors.keys():
return self.setColor(self._namedColors[color])
return False