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 """ Color specifications using CSS2 (Cascading Style Sheet) syntax."""
27
29 """ Color specifications using CSS2 (Cascading Style Sheet) syntax.
30
31 http://www.w3.org/TR/REC-CSS2/syndata.html#color-units
32
33 Usage:
34
35 red = Color(255,0,0)
36 red = Color(1., 0., 0.)
37 red = Color.by_name("red")
38 red = Color.from_rgb(1.,0.,0.)
39 red = Color.from_rgb(255,0,0)
40 red = Color.from_hsl(0.,1., 0.5)
41
42 red = Color.from_string("red")
43 red = Color.from_string("RED")
44 red = Color.from_string("#F00")
45 red = Color.from_string("#FF0000")
46 red = Color.from_string("rgb(255, 0, 0)")
47 red = Color.from_string("rgb(100%, 0%, 0%)")
48 red = Color.from_string("hsl(0, 100%, 50%)")
49
50 """
52
53 if ( type(red) is not type(green) ) or (type(red) is not type(blue)):
54 raise TypeError("Mixed floats and integers?")
55
56 if type(red) is type(1) : red = float(red)/255.
57 if type(green) is type(1) : green = float(green)/255.
58 if type(blue) is type(1) : blue = float(blue)/255.
59
60 self.red = max(0., min(float(red), 1.0))
61 self.green = max(0., min(float(green), 1.0))
62 self.blue = max(0., min(float(blue), 1.0))
63
64
66 "Return a list of standard color names."
67 return _std_colors.keys()
68 names = staticmethod(names)
69
70
73 from_rgb = classmethod(from_rgb)
74
75
76 - def from_hsl(cls, hue_angle, saturation, lightness ):
77 def hue_to_rgb( v1, v2, vH) :
78 if vH < 0.0 : vH += 1.0
79 if vH > 1.0 : vH -= 1.0
80 if vH*6.0 < 1.0 : return (v1 + (v2 - v1) * 6.0 * vH)
81 if vH*2.0 < 1.0 : return v2
82 if vH*3.0 < 2.0 : return (v1 + (v2 - v1) * ((2.0/3.0) - vH) * 6.0)
83 return v1
84
85 hue = (((hue_angle % 360.) + 360.) % 360.)/360.
86
87 if not (saturation >= 0.0 and saturation <=1.0) :
88 raise ValueError("Out-of-range saturation %f"% saturation)
89 if not (lightness >= 0.0 and lightness <=1.0) :
90 raise ValueError("Out-of-range lightness %f"% lightness)
91
92 if saturation == 0 :
93
94 return cls.from_rgb( lightness, lightness, lightness)
95
96 if lightness < 0.5 :
97 v2 = lightness * (1.0+ saturation)
98 else :
99 v2 = (lightness + saturation) - (saturation* lightness)
100
101 v1 = 2.0 * lightness - v2
102 r = hue_to_rgb( v1, v2, hue + (1./3.) )
103 g = hue_to_rgb( v1, v2, hue )
104 b = hue_to_rgb( v1, v2, hue - (1./3.) )
105
106 return cls(r,g,b)
107 from_hsl = classmethod(from_hsl)
108
109
110
112 s = string.strip().lower().replace(' ', '')
113
114 try:
115 return _std_colors[s]
116 except KeyError:
117 raise ValueError("Unknown color name: %s"% s)
118 by_name = staticmethod(by_name)
119
120
122 def to_frac(string) :
123
124 if string[-1]=='%':
125 return float(string[0:-1])/100.
126 else:
127 return float(string)/255.
128
129 s = string.strip().lower().replace(' ', '').replace('_', '')
130
131 if s in _std_colors :
132 return _std_colors[s]
133
134 if s[0] == "#" :
135 if len(s) == 4 :
136 r = int(s[1]+s[1],16)
137 g = int(s[2]+s[2],16)
138 b = int(s[3]+s[3],16)
139 return cls(r,g,b)
140 elif len(s) ==7 :
141 r = int(s[1:3],16)
142 g = int(s[3:5],16)
143 b = int(s[5:7],16)
144 return cls(r,g,b)
145 else :
146 raise ValueError("Cannot parse string: %s" % s)
147
148 if s[0:4] == 'rgb(' and s[-1] == ')' :
149 rgb = s[4:-1].split(",")
150 if len(rgb) != 3 :
151 raise ValueError("Cannot parse string a: %s" % s)
152 return cls( to_frac(rgb[0]), to_frac(rgb[1]), to_frac(rgb[2]))
153
154 if s[0:4] == 'hsl(' and s[-1] == ')' :
155 hsl = s[4:-1].split(",")
156 if len(hsl) != 3 :
157 raise ValueError("Cannot parse string a: %s" % s)
158 return cls.from_hsl( int(hsl[0]), to_frac(hsl[1]), to_frac(hsl[2]))
159
160 raise ValueError("Cannot parse string: %s" % s)
161 from_string = classmethod(from_string)
162
164 req = int(0.5+255.*self.red) == int(0.5+255.*other.red)
165 beq = int(0.5+255.*self.blue) == int(0.5+255.*other.blue)
166 geq = int(0.5+255.*self.green) == int(0.5+255.*other.green)
167
168 return req and beq and geq
169
171 return "Color(%f,%f,%f)" % (self.red, self.green, self.blue)
172
173
174 _std_colors = dict(
175 aliceblue = Color(240,248,255),
176 antiquewhite = Color(250,235,215),
177 aqua = Color(0,255,255),
178 aquamarine = Color(127,255,212),
179 azure = Color(240,255,255),
180 beige = Color(245,245,220),
181 bisque = Color(255,228,196),
182 black = Color(0,0,0),
183 blanchedalmond = Color(255,235,205),
184 blue = Color(0,0,255),
185 blueviolet = Color(138,43,226),
186 brown = Color(165,42,42),
187 burlywood = Color(222,184,135),
188 cadetblue = Color(95,158,160),
189 chartreuse = Color(127,255,0),
190 chocolate = Color(210,105,30),
191 coral = Color(255,127,80),
192 cornflowerblue = Color(100,149,237),
193 cornsilk = Color(255,248,220),
194 crimson = Color(220,20,60),
195 cyan = Color(0,255,255),
196 darkblue = Color(0,0,139),
197 darkcyan = Color(0,139,139),
198 darkgoldenrod = Color(184,134,11),
199 darkgray = Color(169,169,169),
200 darkgreen = Color(0,100,0),
201 darkgrey = Color(169,169,169),
202 darkkhaki = Color(189,183,107),
203 darkmagenta = Color(139,0,139),
204 darkolivegreen = Color(85,107,47),
205 darkorange = Color(255,140,0),
206 darkorchid = Color(153,50,204),
207 darkred = Color(139,0,0),
208 darksalmon = Color(233,150,122),
209 darkseagreen = Color(143,188,143),
210 darkslateblue = Color(72,61,139),
211 darkslategray = Color(47,79,79),
212 darkslategrey = Color(47,79,79),
213 darkturquoise = Color(0,206,209),
214 darkviolet = Color(148,0,211),
215 deeppink = Color(255,20,147),
216 deepskyblue = Color(0,191,255),
217 dimgray = Color(105,105,105),
218 dimgrey = Color(105,105,105),
219 dodgerblue = Color(30,144,255),
220 firebrick = Color(178,34,34),
221 floralwhite = Color(255,250,240),
222 forestgreen = Color(34,139,34),
223 fuchsia = Color(255,0,255),
224 gainsboro = Color(220,220,220),
225 ghostwhite = Color(248,248,255),
226 gold = Color(255,215,0),
227 goldenrod = Color(218,165,32),
228 gray = Color(128,128,128),
229 green = Color(0,128,0),
230 greenyellow = Color(173,255,47),
231 grey = Color(128,128,128),
232 honeydew = Color(240,255,240),
233 hotpink = Color(255,105,180),
234 indianred = Color(205,92,92),
235 indigo = Color(75,0,130),
236 ivory = Color(255,255,240),
237 khaki = Color(240,230,140),
238 lavender = Color(230,230,250),
239 lavenderblush = Color(255,240,245),
240 lawngreen = Color(124,252,0),
241 lemonchiffon = Color(255,250,205),
242 lightblue = Color(173,216,230),
243 lightcoral = Color(240,128,128),
244 lightcyan = Color(224,255,255),
245 lightgoldenrodyellow = Color(250,250,210),
246 lightgray = Color(211,211,211),
247 lightgreen = Color(144,238,144),
248 lightgrey = Color(211,211,211),
249 lightpink = Color(255,182,193),
250 lightsalmon = Color(255,160,122),
251 lightseagreen = Color(32,178,170),
252 lightskyblue = Color(135,206,250),
253 lightslategray = Color(119,136,153),
254 lightslategrey = Color(119,136,153),
255 lightsteelblue = Color(176,196,222),
256 lightyellow = Color(255,255,224),
257 lime = Color(0,255,0),
258 limegreen = Color(50,205,50),
259 linen = Color(250,240,230),
260 magenta = Color(255,0,255),
261 maroon = Color(128,0,0),
262 mediumaquamarine = Color(102,205,170),
263 mediumblue = Color(0,0,205),
264 mediumorchid = Color(186,85,211),
265 mediumpurple = Color(147,112,219),
266 mediumseagreen = Color(60,179,113),
267 mediumslateblue = Color(123,104,238),
268 mediumspringgreen = Color(0,250,154),
269 mediumturquoise = Color(72,209,204),
270 mediumvioletred = Color(199,21,133),
271 midnightblue = Color(25,25,112),
272 mintcream = Color(245,255,250),
273 mistyrose = Color(255,228,225),
274 moccasin = Color(255,228,181),
275 navajowhite = Color(255,222,173),
276 navy = Color(0,0,128),
277 oldlace = Color(253,245,230),
278 olive = Color(128,128,0),
279 olivedrab = Color(107,142,35),
280 orange = Color(255,165,0),
281 orangered = Color(255,69,0),
282 orchid = Color(218,112,214),
283 palegoldenrod = Color(238,232,170),
284 palegreen = Color(152,251,152),
285 paleturquoise = Color(175,238,238),
286 palevioletred = Color(219,112,147),
287 papayawhip = Color(255,239,213),
288 peachpuff = Color(255,218,185),
289 peru = Color(205,133,63),
290 pink = Color(255,192,203),
291 plum = Color(221,160,221),
292 powderblue = Color(176,224,230),
293 purple = Color(128,0,128),
294 red = Color(255,0,0),
295 rosybrown = Color(188,143,143),
296 royalblue = Color(65,105,225),
297 saddlebrown = Color(139,69,19),
298 salmon = Color(250,128,114),
299 sandybrown = Color(244,164,96),
300 seagreen = Color(46,139,87),
301 seashell = Color(255,245,238),
302 sienna = Color(160,82,45),
303 silver = Color(192,192,192),
304 skyblue = Color(135,206,235),
305 slateblue = Color(106,90,205),
306 slategray = Color(112,128,144),
307 slategrey = Color(112,128,144),
308 snow = Color(255,250,250),
309 springgreen = Color(0,255,127),
310 steelblue = Color(70,130,180),
311 tan = Color(210,180,140),
312 teal = Color(0,128,128),
313 thistle = Color(216,191,216),
314 tomato = Color(255,99,71),
315 turquoise = Color(64,224,208),
316 violet = Color(238,130,238),
317 wheat = Color(245,222,179),
318 white = Color(255,255,255),
319 whitesmoke = Color(245,245,245),
320 yellow = Color(255,255,0),
321 yellowgreen = Color(154,205,50)
322 )
323