Code

Extensions. Compressed+media export improvements (see Bug #386664, Gather Resources...
[inkscape.git] / share / extensions / simplestyle.py
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'
174 def parseStyle(s):
175     """Create a dictionary from the value of an inline style attribute"""
176     if s is None:
177       return {}
178     else:
179       return dict([i.split(":") for i in s.split(";") if len(i)])
180 def formatStyle(a):
181     """Format an inline style attribute from a dictionary"""
182     return ";".join([att+":"+str(val) for att,val in a.iteritems()])
183 def isColor(c):
184     """Determine if its a color we can use. If not, leave it unchanged."""
185     if c.startswith('#') and (len(c)==4 or len(c)==7):
186         return True
187     if c.lower() in svgcolors.keys():
188         return True
189     #might be "none" or some undefined color constant or rgb()
190     #however, rgb() shouldnt occur at this point
191     return False
193 def parseColor(c):
194     """Creates a rgb int array"""
195     tmp = svgcolors.get(c.lower())
196     if tmp is not None:
197         c = tmp
198     elif c.startswith('#') and len(c)==4:
199         c='#'+c[1:2]+c[1:2]+c[2:3]+c[2:3]+c[3:]+c[3:]
200     elif c.startswith('rgb('):
201         # remove the rgb(...) stuff
202         tmp = c.strip()[4:-1]
203         numbers = [number.strip() for number in tmp.split(',')]
204         converted_numbers = []
205         if len(numbers) == 3:
206             for num in numbers:
207                 if num.endswith(r'%'):
208                     converted_numbers.append( int(int(num[0:-1])*255/100))
209                 else:
210                     converted_numbers.append(int(num))
211             return tuple(converted_numbers)
212         else:    
213             return (0,0,0)
214         
215     r=int(c[1:3],16)
216     g=int(c[3:5],16)
217     b=int(c[5:],16)
218     return (r,g,b)
220 def formatColoria(a):
221     """int array to #rrggbb"""
222     return '#%02x%02x%02x' % (a[0],a[1],a[2])
223 def formatColorfa(a):
224     """float array to #rrggbb"""
225     return '#%02x%02x%02x' % (int(round(a[0]*255)),int(round(a[1]*255)),int(round(a[2]*255)))
226 def formatColor3i(r,g,b):
227     """3 ints to #rrggbb"""
228     return '#%02x%02x%02x' % (r,g,b)
229 def formatColor3f(r,g,b):
230     """3 floats to #rrggbb"""
231     return '#%02x%02x%02x' % (int(round(r*255)),int(round(g*255)),int(round(b*255)))
234 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99