From 7b343a34a438e0d87b1299cdd5c3e094331c5c93 Mon Sep 17 00:00:00 2001
From: Walter Bender <walter@walter-laptop.(none)>
Date: Mon, 30 Nov 2009 09:32:27 -0500
Subject: [PATCH] color selector feature
---
src/sugar/graphics/xocolor.py | 40 ++++++++++++++++++++++++++++++++++++++--
1 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/src/sugar/graphics/xocolor.py b/src/sugar/graphics/xocolor.py
index fd329cb..75b5400 100644
a
|
b
|
|
1 | 1 | # Copyright (C) 2006-2007 Red Hat, Inc. |
| 2 | # Copyright (C) 2008-2009 Sugar Labs |
2 | 3 | # |
3 | 4 | # This library is free software; you can redistribute it and/or |
4 | 5 | # modify it under the terms of the GNU Lesser General Public |
… |
… |
class XoColor: |
240 | 241 | randomize = False |
241 | 242 | |
242 | 243 | if randomize: |
243 | | n = int(random.random() * (len(colors) - 1)) |
244 | | [self.stroke, self.fill] = colors[n] |
| 244 | [self.strole, self.fill] = self.get_random_color() |
245 | 245 | else: |
246 | 246 | [self.stroke, self.fill] = _parse_string(color_string) |
| 247 | # save an index to our color in the list |
| 248 | self.n = self.find_index() |
247 | 249 | |
248 | 250 | def __cmp__(self, other): |
249 | 251 | if isinstance(other, XoColor): |
… |
… |
class XoColor: |
257 | 259 | def get_fill_color(self): |
258 | 260 | return self.fill |
259 | 261 | |
| 262 | def set_color(self, color_string): |
| 263 | if color_string == None or not is_valid(color_string): |
| 264 | logging.debug('Color string is not valid: %s, ' |
| 265 | 'fallback to default', color_string) |
| 266 | [self.stroke,self.fill] = _parse_string(color_string) |
| 267 | self.n = self.find_index() |
| 268 | |
| 269 | def get_random_color(self): |
| 270 | my_n = int(random.random() * (len(colors) - 1)) |
| 271 | [my_stroke, my_fill] = colors[my_n] |
| 272 | return "%s,%s" % (my_stroke, my_fill) |
| 273 | |
| 274 | def get_next_color(self): |
| 275 | my_n = self.n |
| 276 | my_n += 1 |
| 277 | if my_n == len(colors): |
| 278 | my_n = 0 |
| 279 | [my_stroke, my_fill] = colors[my_n] |
| 280 | return "%s,%s" % (my_stroke, my_fill) |
| 281 | |
| 282 | def get_prev_color(self): |
| 283 | my_n = self.n |
| 284 | my_n -= 1 |
| 285 | if my_n < 0: |
| 286 | my_n = len(colors)-1 |
| 287 | [my_stroke, my_fill] = colors[my_n] |
| 288 | return "%s,%s" % (my_stroke, my_fill) |
| 289 | |
260 | 290 | def to_string(self): |
261 | 291 | return '%s,%s' % (self.stroke, self.fill) |
262 | 292 | |
| 293 | def find_index(self): |
| 294 | for c in range(0,len(colors)): |
| 295 | if colors[c] == [self.stroke, self.fill]: |
| 296 | return c |
| 297 | # if the color is not found, then return 0 |
| 298 | return 0 |
263 | 299 | |
264 | 300 | if __name__ == "__main__": |
265 | 301 | import sys |