1 | # Copyright (C) 2006-2007 Red Hat, Inc. |
---|
2 | # Copyright (C) 2008-2009 Sugar Labs |
---|
3 | # |
---|
4 | # This library is free software; you can redistribute it and/or |
---|
5 | # modify it under the terms of the GNU Lesser General Public |
---|
6 | # License as published by the Free Software Foundation; either |
---|
7 | # version 2 of the License, or (at your option) any later version. |
---|
8 | # |
---|
9 | # This library is distributed in the hope that it will be useful, |
---|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
12 | # Lesser General Public License for more details. |
---|
13 | # |
---|
14 | # You should have received a copy of the GNU Lesser General Public |
---|
15 | # License along with this library; if not, write to the |
---|
16 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
17 | # Boston, MA 02111-1307, USA. |
---|
18 | |
---|
19 | """ |
---|
20 | STABLE. |
---|
21 | """ |
---|
22 | |
---|
23 | import random |
---|
24 | import logging |
---|
25 | |
---|
26 | import gconf |
---|
27 | |
---|
28 | colors = [ |
---|
29 | ['#B20008', '#FF2B34'], \ |
---|
30 | ['#FF2B34', '#B20008'], \ |
---|
31 | ['#E6000A', '#FF2B34'], \ |
---|
32 | ['#FF2B34', '#E6000A'], \ |
---|
33 | ['#FFADCE', '#FF2B34'], \ |
---|
34 | ['#9A5200', '#FF2B34'], \ |
---|
35 | ['#FF2B34', '#9A5200'], \ |
---|
36 | ['#FF8F00', '#FF2B34'], \ |
---|
37 | ['#FF2B34', '#FF8F00'], \ |
---|
38 | ['#FFC169', '#FF2B34'], \ |
---|
39 | ['#807500', '#FF2B34'], \ |
---|
40 | ['#FF2B34', '#807500'], \ |
---|
41 | ['#BE9E00', '#FF2B34'], \ |
---|
42 | ['#FF2B34', '#BE9E00'], \ |
---|
43 | ['#F8E800', '#FF2B34'], \ |
---|
44 | ['#008009', '#FF2B34'], \ |
---|
45 | ['#FF2B34', '#008009'], \ |
---|
46 | ['#00B20D', '#FF2B34'], \ |
---|
47 | ['#FF2B34', '#00B20D'], \ |
---|
48 | ['#8BFF7A', '#FF2B34'], \ |
---|
49 | ['#00588C', '#FF2B34'], \ |
---|
50 | ['#FF2B34', '#00588C'], \ |
---|
51 | ['#005FE4', '#FF2B34'], \ |
---|
52 | ['#FF2B34', '#005FE4'], \ |
---|
53 | ['#BCCDFF', '#FF2B34'], \ |
---|
54 | ['#5E008C', '#FF2B34'], \ |
---|
55 | ['#FF2B34', '#5E008C'], \ |
---|
56 | ['#7F00BF', '#FF2B34'], \ |
---|
57 | ['#FF2B34', '#7F00BF'], \ |
---|
58 | ['#D1A3FF', '#FF2B34'], \ |
---|
59 | ['#9A5200', '#FF8F00'], \ |
---|
60 | ['#FF8F00', '#9A5200'], \ |
---|
61 | ['#C97E00', '#FF8F00'], \ |
---|
62 | ['#FF8F00', '#C97E00'], \ |
---|
63 | ['#FFC169', '#FF8F00'], \ |
---|
64 | ['#807500', '#FF8F00'], \ |
---|
65 | ['#FF8F00', '#807500'], \ |
---|
66 | ['#BE9E00', '#FF8F00'], \ |
---|
67 | ['#FF8F00', '#BE9E00'], \ |
---|
68 | ['#F8E800', '#FF8F00'], \ |
---|
69 | ['#008009', '#FF8F00'], \ |
---|
70 | ['#FF8F00', '#008009'], \ |
---|
71 | ['#00B20D', '#FF8F00'], \ |
---|
72 | ['#FF8F00', '#00B20D'], \ |
---|
73 | ['#8BFF7A', '#FF8F00'], \ |
---|
74 | ['#00588C', '#FF8F00'], \ |
---|
75 | ['#FF8F00', '#00588C'], \ |
---|
76 | ['#005FE4', '#FF8F00'], \ |
---|
77 | ['#FF8F00', '#005FE4'], \ |
---|
78 | ['#BCCDFF', '#FF8F00'], \ |
---|
79 | ['#5E008C', '#FF8F00'], \ |
---|
80 | ['#FF8F00', '#5E008C'], \ |
---|
81 | ['#A700FF', '#FF8F00'], \ |
---|
82 | ['#FF8F00', '#A700FF'], \ |
---|
83 | ['#D1A3FF', '#FF8F00'], \ |
---|
84 | ['#B20008', '#FF8F00'], \ |
---|
85 | ['#FF8F00', '#B20008'], \ |
---|
86 | ['#FF2B34', '#FF8F00'], \ |
---|
87 | ['#FF8F00', '#FF2B34'], \ |
---|
88 | ['#FFADCE', '#FF8F00'], \ |
---|
89 | ['#807500', '#F8E800'], \ |
---|
90 | ['#F8E800', '#807500'], \ |
---|
91 | ['#BE9E00', '#F8E800'], \ |
---|
92 | ['#F8E800', '#BE9E00'], \ |
---|
93 | ['#FFFA00', '#EDDE00'], \ |
---|
94 | ['#008009', '#F8E800'], \ |
---|
95 | ['#F8E800', '#008009'], \ |
---|
96 | ['#00EA11', '#F8E800'], \ |
---|
97 | ['#F8E800', '#00EA11'], \ |
---|
98 | ['#8BFF7A', '#F8E800'], \ |
---|
99 | ['#00588C', '#F8E800'], \ |
---|
100 | ['#F8E800', '#00588C'], \ |
---|
101 | ['#00A0FF', '#F8E800'], \ |
---|
102 | ['#F8E800', '#00A0FF'], \ |
---|
103 | ['#BCCEFF', '#F8E800'], \ |
---|
104 | ['#5E008C', '#F8E800'], \ |
---|
105 | ['#F8E800', '#5E008C'], \ |
---|
106 | ['#AC32FF', '#F8E800'], \ |
---|
107 | ['#F8E800', '#AC32FF'], \ |
---|
108 | ['#D1A3FF', '#F8E800'], \ |
---|
109 | ['#B20008', '#F8E800'], \ |
---|
110 | ['#F8E800', '#B20008'], \ |
---|
111 | ['#FF2B34', '#F8E800'], \ |
---|
112 | ['#F8E800', '#FF2B34'], \ |
---|
113 | ['#FFADCE', '#F8E800'], \ |
---|
114 | ['#9A5200', '#F8E800'], \ |
---|
115 | ['#F8E800', '#9A5200'], \ |
---|
116 | ['#FF8F00', '#F8E800'], \ |
---|
117 | ['#F8E800', '#FF8F00'], \ |
---|
118 | ['#FFC169', '#F8E800'], \ |
---|
119 | ['#008009', '#00EA11'], \ |
---|
120 | ['#00EA11', '#008009'], \ |
---|
121 | ['#00B20D', '#00EA11'], \ |
---|
122 | ['#00EA11', '#00B20D'], \ |
---|
123 | ['#8BFF7A', '#00EA11'], \ |
---|
124 | ['#00588C', '#00EA11'], \ |
---|
125 | ['#00EA11', '#00588C'], \ |
---|
126 | ['#005FE4', '#00EA11'], \ |
---|
127 | ['#00EA11', '#005FE4'], \ |
---|
128 | ['#BCCDFF', '#00EA11'], \ |
---|
129 | ['#5E008C', '#00EA11'], \ |
---|
130 | ['#00EA11', '#5E008C'], \ |
---|
131 | ['#7F00BF', '#00EA11'], \ |
---|
132 | ['#00EA11', '#7F00BF'], \ |
---|
133 | ['#D1A3FF', '#00EA11'], \ |
---|
134 | ['#B20008', '#00EA11'], \ |
---|
135 | ['#00EA11', '#B20008'], \ |
---|
136 | ['#FF2B34', '#00EA11'], \ |
---|
137 | ['#00EA11', '#FF2B34'], \ |
---|
138 | ['#FFADCE', '#00EA11'], \ |
---|
139 | ['#9A5200', '#00EA11'], \ |
---|
140 | ['#00EA11', '#9A5200'], \ |
---|
141 | ['#FF8F00', '#00EA11'], \ |
---|
142 | ['#00EA11', '#FF8F00'], \ |
---|
143 | ['#FFC169', '#00EA11'], \ |
---|
144 | ['#807500', '#00EA11'], \ |
---|
145 | ['#00EA11', '#807500'], \ |
---|
146 | ['#BE9E00', '#00EA11'], \ |
---|
147 | ['#00EA11', '#BE9E00'], \ |
---|
148 | ['#F8E800', '#00EA11'], \ |
---|
149 | ['#00588C', '#00A0FF'], \ |
---|
150 | ['#00A0FF', '#00588C'], \ |
---|
151 | ['#005FE4', '#00A0FF'], \ |
---|
152 | ['#00A0FF', '#005FE4'], \ |
---|
153 | ['#BCCDFF', '#00A0FF'], \ |
---|
154 | ['#5E008C', '#00A0FF'], \ |
---|
155 | ['#00A0FF', '#5E008C'], \ |
---|
156 | ['#9900E6', '#00A0FF'], \ |
---|
157 | ['#00A0FF', '#9900E6'], \ |
---|
158 | ['#D1A3FF', '#00A0FF'], \ |
---|
159 | ['#B20008', '#00A0FF'], \ |
---|
160 | ['#00A0FF', '#B20008'], \ |
---|
161 | ['#FF2B34', '#00A0FF'], \ |
---|
162 | ['#00A0FF', '#FF2B34'], \ |
---|
163 | ['#FFADCE', '#00A0FF'], \ |
---|
164 | ['#9A5200', '#00A0FF'], \ |
---|
165 | ['#00A0FF', '#9A5200'], \ |
---|
166 | ['#FF8F00', '#00A0FF'], \ |
---|
167 | ['#00A0FF', '#FF8F00'], \ |
---|
168 | ['#FFC169', '#00A0FF'], \ |
---|
169 | ['#807500', '#00A0FF'], \ |
---|
170 | ['#00A0FF', '#807500'], \ |
---|
171 | ['#BE9E00', '#00A0FF'], \ |
---|
172 | ['#00A0FF', '#BE9E00'], \ |
---|
173 | ['#F8E800', '#00A0FF'], \ |
---|
174 | ['#008009', '#00A0FF'], \ |
---|
175 | ['#00A0FF', '#008009'], \ |
---|
176 | ['#00B20D', '#00A0FF'], \ |
---|
177 | ['#00A0FF', '#00B20D'], \ |
---|
178 | ['#8BFF7A', '#00A0FF'], \ |
---|
179 | ['#5E008C', '#AC32FF'], \ |
---|
180 | ['#AC32FF', '#5E008C'], \ |
---|
181 | ['#7F00BF', '#AC32FF'], \ |
---|
182 | ['#AC32FF', '#7F00BF'], \ |
---|
183 | ['#D1A3FF', '#AC32FF'], \ |
---|
184 | ['#B20008', '#AC32FF'], \ |
---|
185 | ['#AC32FF', '#B20008'], \ |
---|
186 | ['#FF2B34', '#AC32FF'], \ |
---|
187 | ['#AC32FF', '#FF2B34'], \ |
---|
188 | ['#FFADCE', '#AC32FF'], \ |
---|
189 | ['#9A5200', '#AC32FF'], \ |
---|
190 | ['#AC32FF', '#9A5200'], \ |
---|
191 | ['#FF8F00', '#AC32FF'], \ |
---|
192 | ['#AC32FF', '#FF8F00'], \ |
---|
193 | ['#FFC169', '#AC32FF'], \ |
---|
194 | ['#807500', '#AC32FF'], \ |
---|
195 | ['#AC32FF', '#807500'], \ |
---|
196 | ['#BE9E00', '#AC32FF'], \ |
---|
197 | ['#AC32FF', '#BE9E00'], \ |
---|
198 | ['#F8E800', '#AC32FF'], \ |
---|
199 | ['#008009', '#AC32FF'], \ |
---|
200 | ['#AC32FF', '#008009'], \ |
---|
201 | ['#00B20D', '#AC32FF'], \ |
---|
202 | ['#AC32FF', '#00B20D'], \ |
---|
203 | ['#8BFF7A', '#AC32FF'], \ |
---|
204 | ['#00588C', '#AC32FF'], \ |
---|
205 | ['#AC32FF', '#00588C'], \ |
---|
206 | ['#005FE4', '#AC32FF'], \ |
---|
207 | ['#AC32FF', '#005FE4'], \ |
---|
208 | ['#BCCDFF', '#AC32FF'], \ |
---|
209 | ] |
---|
210 | |
---|
211 | |
---|
212 | def _parse_string(color_string): |
---|
213 | if color_string == 'white': |
---|
214 | return ['#ffffff', '#414141'] |
---|
215 | elif color_string == 'insensitive': |
---|
216 | return ['#ffffff', '#e2e2e2'] |
---|
217 | |
---|
218 | splitted = color_string.split(',') |
---|
219 | if len(splitted) == 2: |
---|
220 | return [splitted[0], splitted[1]] |
---|
221 | else: |
---|
222 | return None |
---|
223 | |
---|
224 | def is_valid(color_string): |
---|
225 | return (_parse_string(color_string) != None) |
---|
226 | |
---|
227 | def get_random_color(): |
---|
228 | color_index = int(random.random() * (len(colors) - 1)) |
---|
229 | return "%s,%s" % (colors[color_index][0], colors[color_index][1]) |
---|
230 | |
---|
231 | def _next_index(i): |
---|
232 | next_index = i + 1 |
---|
233 | if next_index == len(colors): |
---|
234 | next_index = 0 |
---|
235 | return next_index |
---|
236 | |
---|
237 | def _prev_index(i): |
---|
238 | prev_index = i - 1 |
---|
239 | if prev_index < 0: |
---|
240 | prev_index = len(colors)-1 |
---|
241 | return prev_index |
---|
242 | |
---|
243 | class XoColor: |
---|
244 | |
---|
245 | def __init__(self, color_string=None): |
---|
246 | if color_string == None: |
---|
247 | randomize = True |
---|
248 | elif not is_valid(color_string): |
---|
249 | logging.error( |
---|
250 | 'Color string is not valid: %s; fallback to default', |
---|
251 | color_string) |
---|
252 | client = gconf.client_get_default() |
---|
253 | color_string = client.get_string('/desktop/sugar/user/color') |
---|
254 | randomize = False |
---|
255 | else: |
---|
256 | randomize = False |
---|
257 | |
---|
258 | if randomize: |
---|
259 | color_string = get_random_color() |
---|
260 | [self.stroke, self.fill] = _parse_string(color_string) |
---|
261 | self._current_color_index = self._find_index() |
---|
262 | |
---|
263 | def __cmp__(self, other): |
---|
264 | if isinstance(other, XoColor): |
---|
265 | if self.stroke == other.stroke and self.fill == other.fill: |
---|
266 | return 0 |
---|
267 | return -1 |
---|
268 | |
---|
269 | def get_stroke_color(self): |
---|
270 | return self.stroke |
---|
271 | |
---|
272 | def get_fill_color(self): |
---|
273 | return self.fill |
---|
274 | |
---|
275 | def set_color(self, color_string): |
---|
276 | if color_string == None or not is_valid(color_string): |
---|
277 | logging.error( |
---|
278 | 'Color string is not valid: %s; fallback to default', |
---|
279 | color_string) |
---|
280 | else: |
---|
281 | [self.stroke, self.fill] = _parse_string(color_string) |
---|
282 | self._current_color_index = self._find_index() |
---|
283 | |
---|
284 | def get_next_color(self): |
---|
285 | next_index = _next_index(self._current_color_index) |
---|
286 | return "%s,%s" % (colors[next_index][0], colors[next_index][1]) |
---|
287 | |
---|
288 | def get_prev_color(self): |
---|
289 | prev_index = _prev_index(self._current_color_index) |
---|
290 | return "%s,%s" % (colors[prev_index][0], colors[prev_index][1]) |
---|
291 | |
---|
292 | def get_next_stroke_color(self): |
---|
293 | next_index = _next_index(self._current_color_index) |
---|
294 | while (colors[next_index][1] != self.fill): |
---|
295 | next_index = _next_index(next_index) |
---|
296 | return "%s,%s" % (colors[next_index][0], colors[next_index][1]) |
---|
297 | |
---|
298 | def get_prev_stroke_color(self): |
---|
299 | prev_index = _prev_index(self._current_color_index) |
---|
300 | while (colors[prev_index][1] != self.fill): |
---|
301 | prev_index = _prev_index(prev_index) |
---|
302 | return "%s,%s" % (colors[prev_index][0], colors[prev_index][1]) |
---|
303 | |
---|
304 | def get_next_fill_color(self): |
---|
305 | next_index = _next_index(self._current_color_index) |
---|
306 | while (colors[next_index][0] != self.stroke): |
---|
307 | next_index = _next_index(next_index) |
---|
308 | return "%s,%s" % (colors[next_index][0], colors[next_index][1]) |
---|
309 | |
---|
310 | def get_prev_fill_color(self): |
---|
311 | prev_index = _prev_index(self._current_color_index) |
---|
312 | while (colors[prev_index][0] != self.stroke): |
---|
313 | prev_index = _prev_index(prev_index) |
---|
314 | return "%s,%s" % (colors[prev_index][0], colors[prev_index][1]) |
---|
315 | |
---|
316 | def to_string(self): |
---|
317 | return '%s,%s' % (self.stroke, self.fill) |
---|
318 | |
---|
319 | def _find_index(self): |
---|
320 | for color in range(0,len(colors)): |
---|
321 | if colors[color] == [self.stroke, self.fill]: |
---|
322 | return color |
---|
323 | return 0 |
---|
324 | |
---|
325 | if __name__ == "__main__": |
---|
326 | import sys |
---|
327 | import re |
---|
328 | |
---|
329 | f = open(sys.argv[1], 'r') |
---|
330 | |
---|
331 | print 'colors = [' |
---|
332 | |
---|
333 | for line in f.readlines(): |
---|
334 | match = re.match(r'fill: ([A-Z0-9]*) stroke: ([A-Z0-9]*)', line) |
---|
335 | print "['#%s', '#%s'], \\" % (match.group(2), match.group(1)) |
---|
336 | |
---|
337 | print ']' |
---|
338 | |
---|
339 | f.close() |
---|