[PATCH] extended xocolor for new color selector
Walter Bender
walter at sugarlabs.org
Tue Jul 20 14:58:22 EDT 2010
---
src/sugar/graphics/xocolor.py | 78 +++++++++++++++++++++++++++++++++++++---
1 files changed, 72 insertions(+), 6 deletions(-)
diff --git a/src/sugar/graphics/xocolor.py b/src/sugar/graphics/xocolor.py
index fd329cb..8b5a686 100644
--- a/src/sugar/graphics/xocolor.py
+++ b/src/sugar/graphics/xocolor.py
@@ -1,4 +1,5 @@
# Copyright (C) 2006-2007 Red Hat, Inc.
+# Copyright (C) 2008-2010 Sugar Labs
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -225,14 +226,34 @@ def is_valid(color_string):
return (_parse_string(color_string) != None)
+def get_random_color():
+ color_index = int(random.random() * (len(colors) - 1))
+ return "%s,%s" % (colors[color_index][0], colors[color_index][1])
+
+
+def _next_index(i):
+ next_index = i + 1
+ if next_index == len(colors):
+ next_index = 0
+ return next_index
+
+
+def _prev_index(i):
+ prev_index = i - 1
+ if prev_index < 0:
+ prev_index = len(colors)-1
+ return prev_index
+
+
class XoColor:
def __init__(self, color_string=None):
if color_string == None:
randomize = True
elif not is_valid(color_string):
- logging.debug('Color string is not valid: %s, '
- 'fallback to default', color_string)
+ logging.error(
+ 'Color string is not valid: %s; fallback to default',
+ color_string)
client = gconf.client_get_default()
color_string = client.get_string('/desktop/sugar/user/color')
randomize = False
@@ -240,10 +261,9 @@ class XoColor:
randomize = False
if randomize:
- n = int(random.random() * (len(colors) - 1))
- [self.stroke, self.fill] = colors[n]
- else:
- [self.stroke, self.fill] = _parse_string(color_string)
+ color_string = get_random_color()
+ [self.stroke, self.fill] = _parse_string(color_string)
+ self._current_color_index = self._find_index()
def __cmp__(self, other):
if isinstance(other, XoColor):
@@ -257,9 +277,55 @@ class XoColor:
def get_fill_color(self):
return self.fill
+ def set_color(self, color_string):
+ if color_string == None or not is_valid(color_string):
+ logging.error(
+ 'Color string is not valid: %s; fallback to default',
+ color_string)
+ else:
+ [self.stroke, self.fill] = _parse_string(color_string)
+ self._current_color_index = self._find_index()
+
+ def get_next_color(self):
+ next_index = _next_index(self._current_color_index)
+ return "%s,%s" % (colors[next_index][0], colors[next_index][1])
+
+ def get_prev_color(self):
+ prev_index = _prev_index(self._current_color_index)
+ return "%s,%s" % (colors[prev_index][0], colors[prev_index][1])
+
+ def get_next_stroke_color(self):
+ next_index = _next_index(self._current_color_index)
+ while (colors[next_index][1] != self.fill):
+ next_index = _next_index(next_index)
+ return "%s,%s" % (colors[next_index][0], colors[next_index][1])
+
+ def get_prev_stroke_color(self):
+ prev_index = _prev_index(self._current_color_index)
+ while (colors[prev_index][1] != self.fill):
+ prev_index = _prev_index(prev_index)
+ return "%s,%s" % (colors[prev_index][0], colors[prev_index][1])
+
+ def get_next_fill_color(self):
+ next_index = _next_index(self._current_color_index)
+ while (colors[next_index][0] != self.stroke):
+ next_index = _next_index(next_index)
+ return "%s,%s" % (colors[next_index][0], colors[next_index][1])
+
+ def get_prev_fill_color(self):
+ prev_index = _prev_index(self._current_color_index)
+ while (colors[prev_index][0] != self.stroke):
+ prev_index = _prev_index(prev_index)
+ return "%s,%s" % (colors[prev_index][0], colors[prev_index][1])
+
def to_string(self):
return '%s,%s' % (self.stroke, self.fill)
+ def _find_index(self):
+ for color in range(0, len(colors)):
+ if colors[color] == [self.stroke, self.fill]:
+ return color
+ return 0
if __name__ == "__main__":
import sys
--
1.7.0.4
--
Walter Bender
Sugar Labs
http://www.sugarlabs.org
More information about the Sugar-devel
mailing list