root/trunk/dabo/dColors.py

Revision 4389, 8.1 kB (checked in by ed, 4 months ago)

Forgot to include these in last night's changes. dColors adds randomColorName(), which returns a random color name instead of a random color tuple. dEvents has been updated to use the dSlidePanelControl naming.

  • Property svn:eol-style set to native
Line 
1 # -*- coding: utf-8 -*-
2 import re
3 import random
4
5 class HexError(Exception): pass
6 class InvalidCharError(HexError): pass
7 class TypeError(HexError): pass
8
9 class ColorTupleError(Exception): pass
10 class RgbValueError(ColorTupleError): pass
11 class LengthError(ColorTupleError): pass
12 class IntegerTypeError(ColorTupleError): pass
13
14 colorDict = {"aliceblue" : (240, 248, 255),
15         "antiquewhite" : (250, 235, 215),
16         "aqua" : (0, 255, 255),
17         "aquamarine" : (127, 255, 212),
18         "azure" : (240, 255, 255),
19         "beige" : (245, 245, 220),
20         "bisque" : (255, 228, 196),
21         "black" : (0, 0, 0),
22         "blanchedalmond" : (255, 235, 205),
23         "blue" : (0, 0, 255),
24         "blueviolet" : (138, 43, 226),
25         "brown" : (165, 42, 42),
26         "burlywood" : (222, 184, 135),
27         "cadetblue" : (95, 158, 160),
28         "chartreuse" : (127, 255, 0),
29         "chocolate" : (210, 105, 30),
30         "coral" : (255, 127, 80),
31         "cornflowerblue" : (100, 149, 237),
32         "cornsilk" : (255, 248, 220),
33         "crimson" : (220, 20, 60),
34         "cyan" : (0, 255, 255),
35         "darkblue" : (0, 0, 139),
36         "darkcyan" : (0, 139, 139),
37         "darkgoldenrod" : (184, 134, 11),
38         "darkgray" : (169, 169, 169),
39         "darkgrey" : (169, 169, 169),
40         "darkgreen" : (0, 100, 0),
41         "darkkhaki" : (189, 183, 107),
42         "darkmagenta" : (139, 0, 139),
43         "darkolivegreen" : (85, 107, 47),
44         "darkorange" : (255, 140, 0),
45         "darkorchid" : (153, 50, 204),
46         "darkred" : (139, 0, 0),
47         "darksalmon" : (233, 150, 122),
48         "darkseagreen" : (143, 188, 143),
49         "darkslateblue" : (72, 61, 139),
50         "darkslategray" : (47, 79, 79),
51         "darkslategrey" : (47, 79, 79),
52         "darkturquoise" : (0, 206, 209),
53         "darkviolet" : (148, 0, 211),
54         "deeppink" : (255, 20, 147),
55         "deepskyblue" : (0, 191, 255),
56         "dimgray" : (105, 105, 105),
57         "dimgrey" : (105, 105, 105),
58         "dodgerblue" : (30, 144, 255),
59         "feldspar" : (209, 146, 117),
60         "firebrick" : (178, 34, 34),
61         "floralwhite" : (255, 250, 240),
62         "forestgreen" : (34, 139, 34),
63         "fuchsia" : (255, 0, 255),
64         "gainsboro" : (220, 220, 220),
65         "ghostwhite" : (248, 248, 255),
66         "gold" : (255, 215, 0),
67         "goldenrod" : (218, 165, 32),
68         "gray" : (128, 128, 128),
69         "grey" : (128, 128, 128),
70         "green" : (0, 128, 0),
71         "greenyellow" : (173, 255, 47),
72         "honeydew" : (240, 255, 240),
73         "hotpink" : (255, 105, 180),
74         "indianred" : (205, 92, 92),
75         "indigo" : (75, 0, 130),
76         "ivory" : (255, 255, 240),
77         "khaki" : (240, 230, 140),
78         "lavender" : (230, 230, 250),
79         "lavenderblush" : (255, 240, 245),
80         "lawngreen" : (124, 252, 0),
81         "lemonchiffon" : (255, 250, 205),
82         "lightblue" : (173, 216, 230),
83         "lightcoral" : (240, 128, 128),
84         "lightcyan" : (224, 255, 255),
85         "lightgoldenrodyellow" : (250, 250, 210),
86         "lightgray" : (211, 211, 211),
87         "lightgrey" : (211, 211, 211),
88         "lightgreen" : (144, 238, 144),
89         "lightpink" : (255, 182, 193),
90         "lightsalmon" : (255, 160, 122),
91         "lightseagreen" : (32, 178, 170),
92         "lightskyblue" : (135, 206, 250),
93         "lightslateblue" : (132, 112, 255),
94         "lightslategray" : (119, 136, 153),
95         "lightslategrey" : (119, 136, 153),
96         "lightsteelblue" : (176, 196, 222),
97         "lightyellow" : (255, 255, 224),
98         "lime" : (0, 255, 0),
99         "limegreen" : (50, 205, 50),
100         "linen" : (250, 240, 230),
101         "magenta" : (255, 0, 255),
102         "maroon" : (128, 0, 0),
103         "mediumaquamarine" : (102, 205, 170),
104         "mediumblue" : (0, 0, 205),
105         "mediumorchid" : (186, 85, 211),
106         "mediumpurple" : (147, 112, 216),
107         "mediumseagreen" : (60, 179, 113),
108         "mediumslateblue" : (123, 104, 238),
109         "mediumspringgreen" : (0, 250, 154),
110         "mediumturquoise" : (72, 209, 204),
111         "mediumvioletred" : (199, 21, 133),
112         "midnightblue" : (25, 25, 112),
113         "mintcream" : (245, 255, 250),
114         "mistyrose" : (255, 228, 225),
115         "moccasin" : (255, 228, 181),
116         "navajowhite" : (255, 222, 173),
117         "navy" : (0, 0, 128),
118         "oldlace" : (253, 245, 230),
119         "olive" : (128, 128, 0),
120         "olivedrab" : (107, 142, 35),
121         "orange" : (255, 165, 0),
122         "orangered" : (255, 69, 0),
123         "orchid" : (218, 112, 214),
124         "palegoldenrod" : (238, 232, 170),
125         "palegreen" : (152, 251, 152),
126         "paleturquoise" : (175, 238, 238),
127         "palevioletred" : (216, 112, 147),
128         "papayawhip" : (255, 239, 213),
129         "peachpuff" : (255, 218, 185),
130         "peru" : (205, 133, 63),
131         "pink" : (255, 192, 203),
132         "plum" : (221, 160, 221),
133         "powderblue" : (176, 224, 230),
134         "purple" : (128, 0, 128),
135         "red" : (255, 0, 0),
136         "rosybrown" : (188, 143, 143),
137         "royalblue" : (65, 105, 225),
138         "saddlebrown" : (139, 69, 19),
139         "salmon" : (250, 128, 114),
140         "sandybrown" : (244, 164, 96),
141         "seagreen" : (46, 139, 87),
142         "seashell" : (255, 245, 238),
143         "sienna" : (160, 82, 45),
144         "silver" : (192, 192, 192),
145         "skyblue" : (135, 206, 235),
146         "slateblue" : (106, 90, 205),
147         "slategray" : (112, 128, 144),
148         "slategrey" : (112, 128, 144),
149         "snow" : (255, 250, 250),
150         "springgreen" : (0, 255, 127),
151         "steelblue" : (70, 130, 180),
152         "tan" : (210, 180, 140),
153         "teal" : (0, 128, 128),
154         "thistle" : (216, 191, 216),
155         "tomato" : (255, 99, 71),
156         "turquoise" : (64, 224, 208),
157         "violet" : (238, 130, 238),
158         "violetred" : (208, 32, 144),
159         "wheat" : (245, 222, 179),
160         "white" : (255, 255, 255),
161         "whitesmoke" : (245, 245, 245),
162         "yellow" : (255, 255, 0),
163         "yellowgreen" : (154, 205, 50)
164         }
165
166 colors = colorDict.keys()
167
168
169 def hexToDec(hx):
170     if not isinstance(hx, basestring):
171         raise TypeError, "Input must be a string"
172     # Define a dict of char-value pairs
173     hex = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8,
174             "9": 9, "A": 10, "B": 11, "C": 12, "D": 13, "E": 14, "F": 15}
175     # Reverse it, and force upper case
176     rev = hx[::-1].upper()
177     ret = 0
178     pos = 1
179     for c in rev:
180         if hex.get(c) == None:
181             raise InvalidCharError, "%s is an invalid hex character" % (c, )
182         ret += (hex[c] * pos)
183         pos = pos * 16
184     return ret
185
186
187 def tupleToHex(t, includeHash=True):
188     """Convert a color tuple into an HTML hex format."""
189     if not len(t) == 3:
190         raise LengthError, "Color tuple needs to contain 3 elements"
191     for rgb in t:
192         if not isinstance(rgb, int):
193             raise IntegerTypeError, "Tuple elements should be all integers."
194         if not 0 <= rgb <= 255:
195             raise RgbValueError, "Rgb Value must be in the range 0-255"
196     rx, gx, bx = hex(t[0]), hex(t[1]), hex(t[2])
197     # Each is in the format '0x00'.
198     r = rx[2:].upper()
199     g = gx[2:].upper()
200     b= bx[2:].upper()
201     if len(r) == 1: r = '0' + r
202     if len(g) == 1: g = '0' + g
203     if len(b) == 1: b = '0' + b
204     ret = ""
205     if includeHash:
206         ret = "#"
207     ret += r + g + b
208     return ret
209
210
211 def colorTupleFromHex(hx):
212     # Strip the pound sign, if any
213     hx = hx.replace("#", "")
214     hx = hx.lstrip('0')
215     if len(hx) < 6: hx = '0'*(6-len(hx)) + hx
216     red = hexToDec(hx[:2])
217     green = hexToDec(hx[2:4])
218     blue = hexToDec(hx[4:])
219     return (red, green, blue)
220
221
222 def colorTupleFromName(color):
223     """Given a color name, such as "Blue" or "Aquamarine", return a color tuple.
224     
225     This is used internally in the ForeColor and BackColor property setters. The
226     color name is not case-sensitive. If the color name doesn't exist, an exception
227     is raised.
228     """
229     ret = None
230     try:
231         ret = colorDict[color.lower().strip()]
232     except KeyError:
233         try:
234             ret = colorTupleFromHex(color)
235         except InvalidCharError:
236             ret = colorTupleFromString(color)
237     return ret
238    
239        
240 def colorTupleFromString(color):
241     ret = None
242     colorTuplePat = "\((\d+), *(\d+), *(\d+)\)"
243     mtch = re.match(colorTuplePat, color)
244     if mtch:
245         grps = mtch.groups()
246         ret = (int(grps[0]), int(grps[1]), int(grps[2]))
247         for val in ret:
248             if not 0 <= val <= 255:
249                 raise KeyError, "Color tuple integer must range from 0-255"
250     else:
251         raise KeyError, "Color '%s' is not defined." % color
252     return ret
253
254
255 def randomColor():
256     """Returns a random color tuple"""
257     return colorDict[random.choice(colorDict.keys())]
258
259
260 def randomColorName():
261     """Returns a random color name"""
262     return random.choice(colorDict.keys())
263
264
265 def colorNameFromTuple(colorTuple, firstOnly=False):
266     """Returns a list of color names, if any, whose RGB tuple matches
267     the specified tuple. If 'firstOnly' is True, then a single color name
268     will be returned as a string, not a list; the string will be empty
269     if there is no match.
270     """
271     ret = [nm for nm, tup in colorDict.items()
272             if tup == colorTuple]
273     if firstOnly:
274         try:
275             ret = ret[0]
276         except IndexError:
277             ret = ""
278     return ret
Note: See TracBrowser for help on using the browser.