1 #!/usr/bin/env python
2 """
3 simplestyle.py
4 Two simple functions for working with inline css
5 and some color handling on top.
7 Copyright (C) 2005 Aaron Spike, aaron@ekips.org
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 """
24 svgcolors={
25 'aliceblue':'#f0f8ff',
26 'antiquewhite':'#faebd7',
27 'aqua':'#00ffff',
28 'aquamarine':'#7fffd4',
29 'azure':'#f0ffff',
30 'beige':'#f5f5dc',
31 'bisque':'#ffe4c4',
32 'black':'#000000',
33 'blanchedalmond':'#ffebcd',
34 'blue':'#0000ff',
35 'blueviolet':'#8a2be2',
36 'brown':'#a52a2a',
37 'burlywood':'#deb887',
38 'cadetblue':'#5f9ea0',
39 'chartreuse':'#7fff00',
40 'chocolate':'#d2691e',
41 'coral':'#ff7f50',
42 'cornflowerblue':'#6495ed',
43 'cornsilk':'#fff8dc',
44 'crimson':'#dc143c',
45 'cyan':'#00ffff',
46 'darkblue':'#00008b',
47 'darkcyan':'#008b8b',
48 'darkgoldenrod':'#b8860b',
49 'darkgray':'#a9a9a9',
50 'darkgreen':'#006400',
51 'darkgrey':'#a9a9a9',
52 'darkkhaki':'#bdb76b',
53 'darkmagenta':'#8b008b',
54 'darkolivegreen':'#556b2f',
55 'darkorange':'#ff8c00',
56 'darkorchid':'#9932cc',
57 'darkred':'#8b0000',
58 'darksalmon':'#e9967a',
59 'darkseagreen':'#8fbc8f',
60 'darkslateblue':'#483d8b',
61 'darkslategray':'#2f4f4f',
62 'darkslategrey':'#2f4f4f',
63 'darkturquoise':'#00ced1',
64 'darkviolet':'#9400d3',
65 'deeppink':'#ff1493',
66 'deepskyblue':'#00bfff',
67 'dimgray':'#696969',
68 'dimgrey':'#696969',
69 'dodgerblue':'#1e90ff',
70 'firebrick':'#b22222',
71 'floralwhite':'#fffaf0',
72 'forestgreen':'#228b22',
73 'fuchsia':'#ff00ff',
74 'gainsboro':'#dcdcdc',
75 'ghostwhite':'#f8f8ff',
76 'gold':'#ffd700',
77 'goldenrod':'#daa520',
78 'gray':'#808080',
79 'grey':'#808080',
80 'green':'#008000',
81 'greenyellow':'#adff2f',
82 'honeydew':'#f0fff0',
83 'hotpink':'#ff69b4',
84 'indianred':'#cd5c5c',
85 'indigo':'#4b0082',
86 'ivory':'#fffff0',
87 'khaki':'#f0e68c',
88 'lavender':'#e6e6fa',
89 'lavenderblush':'#fff0f5',
90 'lawngreen':'#7cfc00',
91 'lemonchiffon':'#fffacd',
92 'lightblue':'#add8e6',
93 'lightcoral':'#f08080',
94 'lightcyan':'#e0ffff',
95 'lightgoldenrodyellow':'#fafad2',
96 'lightgray':'#d3d3d3',
97 'lightgreen':'#90ee90',
98 'lightgrey':'#d3d3d3',
99 'lightpink':'#ffb6c1',
100 'lightsalmon':'#ffa07a',
101 'lightseagreen':'#20b2aa',
102 'lightskyblue':'#87cefa',
103 'lightslategray':'#778899',
104 'lightslategrey':'#778899',
105 'lightsteelblue':'#b0c4de',
106 'lightyellow':'#ffffe0',
107 'lime':'#00ff00',
108 'limegreen':'#32cd32',
109 'linen':'#faf0e6',
110 'magenta':'#ff00ff',
111 'maroon':'#800000',
112 'mediumaquamarine':'#66cdaa',
113 'mediumblue':'#0000cd',
114 'mediumorchid':'#ba55d3',
115 'mediumpurple':'#9370db',
116 'mediumseagreen':'#3cb371',
117 'mediumslateblue':'#7b68ee',
118 'mediumspringgreen':'#00fa9a',
119 'mediumturquoise':'#48d1cc',
120 'mediumvioletred':'#c71585',
121 'midnightblue':'#191970',
122 'mintcream':'#f5fffa',
123 'mistyrose':'#ffe4e1',
124 'moccasin':'#ffe4b5',
125 'navajowhite':'#ffdead',
126 'navy':'#000080',
127 'oldlace':'#fdf5e6',
128 'olive':'#808000',
129 'olivedrab':'#6b8e23',
130 'orange':'#ffa500',
131 'orangered':'#ff4500',
132 'orchid':'#da70d6',
133 'palegoldenrod':'#eee8aa',
134 'palegreen':'#98fb98',
135 'paleturquoise':'#afeeee',
136 'palevioletred':'#db7093',
137 'papayawhip':'#ffefd5',
138 'peachpuff':'#ffdab9',
139 'peru':'#cd853f',
140 'pink':'#ffc0cb',
141 'plum':'#dda0dd',
142 'powderblue':'#b0e0e6',
143 'purple':'#800080',
144 'red':'#ff0000',
145 'rosybrown':'#bc8f8f',
146 'royalblue':'#4169e1',
147 'saddlebrown':'#8b4513',
148 'salmon':'#fa8072',
149 'sandybrown':'#f4a460',
150 'seagreen':'#2e8b57',
151 'seashell':'#fff5ee',
152 'sienna':'#a0522d',
153 'silver':'#c0c0c0',
154 'skyblue':'#87ceeb',
155 'slateblue':'#6a5acd',
156 'slategray':'#708090',
157 'slategrey':'#708090',
158 'snow':'#fffafa',
159 'springgreen':'#00ff7f',
160 'steelblue':'#4682b4',
161 'tan':'#d2b48c',
162 'teal':'#008080',
163 'thistle':'#d8bfd8',
164 'tomato':'#ff6347',
165 'turquoise':'#40e0d0',
166 'violet':'#ee82ee',
167 'wheat':'#f5deb3',
168 'white':'#ffffff',
169 'whitesmoke':'#f5f5f5',
170 'yellow':'#ffff00',
171 'yellowgreen':'#9acd32'
172 }
173 def parseStyle(s):
174 """Create a dictionary from the value of an inline style attribute"""
175 return dict([i.split(":") for i in s.split(";") if len(i)])
176 def formatStyle(a):
177 """Format an inline style attribute from a dictionary"""
178 return ";".join([":".join(i) for i in a.iteritems()])
179 def isColor(c):
180 """Determine if its a color we can use. If not, leave it unchanged."""
181 if c.startswith('#') and (len(c)==4 or len(c)==7):
182 return True
183 if c in svgcolors.keys():
184 return True
185 #might be "none" or some undefined color constant or rgb()
186 #however, rgb() shouldnt occur at this point
187 return False
188 def parseColor(c):
189 """Creates a rgb int array"""
190 if c in svgcolors.keys():
191 c=svgcolors[c]
192 if c.startswith('#') and len(c)==4:
193 c='#'+c[1:2]+c[1:2]+c[2:3]+c[2:3]+c[3:]+c[3:]
194 elif c.startswith('rgb('):
195 # remove the rgb(...) stuff
196 tmp = c.strip()[4:-1]
197 numbers = [number.strip() for number in tmp.split(',')]
198 converted_numbers = []
199 if len(numbers) == 3:
200 for num in numbers:
201 if num.endswith(r'%'):
202 converted_numbers.append( int(int(num[0:-1])*255/100))
203 else:
204 converted_numbers.append(int(num))
205 return tuple(converted_numbers)
206 else:
207 return (0,0,0)
209 r=int(c[1:3],16)
210 g=int(c[3:5],16)
211 b=int(c[5:],16)
212 return (r,g,b)
213 def formatColoria(a):
214 """int array to #rrggbb"""
215 return '#%02x%02x%02x' % (a[0],a[1],a[2])
216 def formatColorfa(a):
217 """float array to #rrggbb"""
218 return '#%02x%02x%02x' % (int(round(a[0]*255)),int(round(a[1]*255)),int(round(a[2]*255)))
219 def formatColor3i(r,g,b):
220 """3 ints to #rrggbb"""
221 return '#%02x%02x%02x' % (r,g,b)
222 def formatColor3f(r,g,b):
223 """3 floats to #rrggbb"""
224 return '#%02x%02x%02x' % (int(round(r*255)),int(round(g*255)),int(round(b*255)))
227 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99