[PATCH] added get_next and get_prev fill and stroke colors

Walter Bender walter at sugarlabs.org
Thu Aug 5 07:34:03 EDT 2010


---
 src/sugar/graphics/xocolor.py |  109 ++++++++++++++++++++++++++++++++++++++---
 1 files changed, 102 insertions(+), 7 deletions(-)

diff --git a/src/sugar/graphics/xocolor.py b/src/sugar/graphics/xocolor.py
index fd329cb..9d1d80a 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
@@ -24,6 +25,9 @@ import logging

 import gconf

+_STROKE_COLOR = 0
+_FILL_COLOR = 1
+
 colors = [
 ['#B20008', '#FF2B34'], \
 ['#FF2B34', '#B20008'], \
@@ -216,7 +220,7 @@ def _parse_string(color_string):

     splitted = color_string.split(',')
     if len(splitted) == 2:
-        return [splitted[0], splitted[1]]
+        return [splitted[_STROKE_COLOR], splitted[_FILL_COLOR]]
     else:
         return None

@@ -225,14 +229,99 @@ def is_valid(color_string):
     return (_parse_string(color_string) != None)


+def get_random_color():
+    """ Return a random color from the default colors-pair list. """
+    color_index = int(random.random() * (len(colors) - 1))
+    return "%s,%s" % (colors[color_index][_STROKE_COLOR],
+                      colors[color_index][_FILL_COLOR])
+
+
+def get_next_stroke_color(color):
+    """ Return the next color pair in the list that shares the same fill
+        as color. """
+    current_index = _get_index(color)
+    if current_index == -1:
+        return "%s,%s" % (color.stroke, color.fill)
+    next_index = _next_index(current_index)
+    while(colors[next_index][_FILL_COLOR] != \
+              colors[current_index][_FILL_COLOR]):
+        next_index = _next_index(next_index)
+    return "%s,%s" % (colors[next_index][_STROKE_COLOR],
+                      colors[next_index][_FILL_COLOR])
+
+
+def get_prev_stroke_color(color):
+    """ Return the prev color pair in the list that shares the same fill
+        as color. """
+    current_index = _get_index(color)
+    if current_index == -1:
+        return "%s,%s" % (color.stroke, color.fill)
+    prev_index = _prev_index(current_index)
+    while (colors[prev_index][_FILL_COLOR] != \
+               colors[current_index][_FILL_COLOR]):
+        prev_index = _prev_index(prev_index)
+    return "%s,%s" % (colors[prev_index][_STROKE_COLOR],
+                      colors[prev_index][_FILL_COLOR])
+
+
+def get_next_fill_color(color):
+    """ Return the next color pair in the list that shares the same stroke
+        as color. """
+    current_index = _get_index(color)
+    if current_index == -1:
+        return "%s,%s" % (color.stroke, color.fill)
+    next_index = _next_index(current_index)
+    while (colors[next_index][_STROKE_COLOR] != \
+               colors[current_index][_STROKE_COLOR]):
+        next_index = _next_index(next_index)
+    return "%s,%s" % (colors[next_index][_STROKE_COLOR],
+                      colors[next_index][_FILL_COLOR])
+
+
+def get_prev_fill_color(color):
+    """ Return the prev color pair in the list that shares the same stroke
+        as color. """
+    current_index = _get_index(color)
+    if current_index == -1:
+        return "%s,%s" % (color.stroke, color.fill)
+    prev_index = _prev_index(current_index)
+    while (colors[prev_index][_STROKE_COLOR] != \
+               colors[current_index][_STROKE_COLOR]):
+        prev_index = _prev_index(prev_index)
+    return "%s,%s" % (colors[prev_index][_STROKE_COLOR],
+                      colors[prev_index][_FILL_COLOR])
+
+
+def _next_index(current_index):
+    next_index = current_index + 1
+    if next_index == len(colors):
+        next_index = 0
+    return next_index
+
+
+def _prev_index(current_index):
+    prev_index = current_index - 1
+    if prev_index < 0:
+        prev_index = len(colors)-1
+    return prev_index
+
+
+def _get_index(color):
+    for index in range(0, len(colors)):
+        if colors[index] == [color.stroke, color.fill]:
+            return index
+    return -1
+
+
 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 +329,8 @@ 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)

     def __cmp__(self, other):
         if isinstance(other, XoColor):
@@ -257,6 +344,14 @@ 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)
+
     def to_string(self):
         return '%s,%s' % (self.stroke, self.fill)

-- 
1.7.0.4

-walter

-- 
Walter Bender
Sugar Labs
http://www.sugarlabs.org


More information about the Sugar-devel mailing list