Difference between revisions of "Features/Enhanced color selector/Patch-xocolor"

From Sugar Labs
Jump to navigation Jump to search
 
Line 1: Line 1:
 
<pre>
 
<pre>
--- src/sugar/graphics/xocolor.py 2009-11-19 14:54:22.000000000 -0500
+
diff --git a/src/sugar/graphics/xocolor.py b/src/sugar/graphics/xocolor.py
+++ src/sugar/graphics/xocolor.py 2009-11-19 15:00:58.000000000 -0500
+
index 75b5400..57b5712 100644
@@ -1,4 +1,5 @@
+
--- a/src/sugar/graphics/xocolor.py
# Copyright (C) 2006-2007 Red Hat, Inc.
+
+++ b/src/sugar/graphics/xocolor.py
+# Copyright (C) 2008-2009 Sugar Labs
+
@@ -221,10 +221,12 @@ def _parse_string(color_string):
#
+
    else:
# This library is free software; you can redistribute it and/or
+
        return None
# modify it under the terms of the GNU Lesser General Public
+
@@ -240,10 +241,11 @@ class XoColor:
+
-
 +
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])
 +
 +
class XoColor:
 +
 +
@@ -232,8 +234,9 @@ class XoColor:
 +
        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
 +
@@ -241,11 +244,9 @@ class XoColor:
 
             randomize = False
 
             randomize = False
 
   
 
   
 
         if randomize:
 
         if randomize:
-            n = int(random.random() * (len(colors) - 1))
+
-            [self.strole, self.fill] = self.get_random_color()
-           [self.stroke, self.fill] = colors[n]
+
-        else:
+           [self.strole, self.fill] = self.get_random_color()
+
-            [self.stroke, self.fill] = _parse_string(color_string)
        else:
+
-       # save an index to our color in the list
            [self.stroke, self.fill] = _parse_string(color_string)
+
-       self.n = self.find_index()
+       # save an index to our color in the list
+
+            color_string =  get_random_color()
+       self.n = self.find_index()
+
+        [self.stroke, self.fill] = _parse_string(color_string)
 +
+        self._current_color_index = self._find_index()
 
   
 
   
 
     def __cmp__(self, other):
 
     def __cmp__(self, other):
 
         if isinstance(other, XoColor):
 
         if isinstance(other, XoColor):
@@ -257,9 +259,43 @@ class XoColor:
+
@@ -261,40 +262,32 @@ 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.debug('Color string is not valid: %s, '
 +
-                          'fallback to default', color_string)
 +
-        [self.stroke,self.fill] = _parse_string(color_string)
 +
-        self.n = self.find_index()
 +
-
 +
-    def get_random_color(self):
 +
-        my_n = int(random.random() * (len(colors) - 1))
 +
-        [my_stroke, my_fill] = colors[my_n]
 +
-        return "%s,%s" % (my_stroke, my_fill)
 +
+            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):
 +
-        my_n = self.n
 +
-        my_n += 1
 +
-        if my_n == len(colors):
 +
-            my_n = 0
 +
-        [my_stroke, my_fill] = colors[my_n]
 +
-        return "%s,%s" % (my_stroke, my_fill)
 +
+        next_index = self._current_color_index + 1
 +
+        if next_index == len(colors):
 +
+            next_index = 0
 +
+        return "%s,%s" % (colors[next_index][0], colors[next_index][1])
 +
 +
    def get_prev_color(self):
 +
-        my_n = self.n
 +
-        my_n -= 1
 +
-        if my_n < 0:
 +
-            my_n = len(colors)-1
 +
-        [my_stroke, my_fill] = colors[my_n]
 +
-        return "%s,%s" % (my_stroke, my_fill)
 +
+        prev_index = self._current_color_index - 1
 +
+        if prev_index < 0:
 +
+            prev_index = len(colors)-1
 +
+        return "%s,%s" % (colors[prev_index][0], colors[prev_index][1])
 
   
 
   
+    def set_color(self, color_string):
 
+        if color_string == None or not is_valid(color_string):
 
+            logging.debug('Color string is not valid: %s, '
 
+                          'fallback to default', color_string)
 
+        [self.stroke,self.fill] = _parse_string(color_string)
 
+        self.n = self.find_index()
 
+
 
+    def get_random_color(self):
 
+        my_n = int(random.random() * (len(colors) - 1))
 
+        [my_stroke, my_fill] = colors[my_n]
 
+        return "%s,%s" % (my_stroke, my_fill)
 
+
 
+    def get_next_color(self):
 
+        my_n = self.n
 
+        my_n += 1
 
+        if my_n == len(colors):
 
+            my_n = 0
 
+        [my_stroke, my_fill] = colors[my_n]
 
+        return "%s,%s" % (my_stroke, my_fill)
 
+
 
+    def get_prev_color(self):
 
+        my_n = self.n
 
+        my_n -= 1
 
+        if my_n < 0:
 
+            my_n = len(colors)-1
 
+        [my_stroke, my_fill] = colors[my_n]
 
+        return "%s,%s" % (my_stroke, my_fill)
 
+
 
 
     def to_string(self):
 
     def to_string(self):
 
         return '%s,%s' % (self.stroke, self.fill)
 
         return '%s,%s' % (self.stroke, self.fill)
 
   
 
   
+   def find_index(self):
+
-   def find_index(self):
+       for c in range(0,len(colors)):
+
-       for c in range(0,len(colors)):
+           if colors[c] == [self.stroke, self.fill]:
+
-           if colors[c] == [self.stroke, self.fill]:
+               return c
+
-               return c
+       # if the color is not found, then return 0
+
-       # if the color is not found, then return 0
+        return 0
+
+    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__":
 
  if __name__ == "__main__":
    import sys
 
 
</pre>
 
</pre>

Latest revision as of 17:46, 21 December 2009

diff --git a/src/sugar/graphics/xocolor.py b/src/sugar/graphics/xocolor.py
index 75b5400..57b5712 100644
--- a/src/sugar/graphics/xocolor.py
+++ b/src/sugar/graphics/xocolor.py
@@ -221,10 +221,12 @@ def _parse_string(color_string):
     else:
         return None
 
-
 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])
 
 class XoColor:
 
@@ -232,8 +234,9 @@ class XoColor:
         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
@@ -241,11 +244,9 @@ class XoColor:
             randomize = False
 
         if randomize:
-            [self.strole, self.fill] = self.get_random_color()
-        else:
-            [self.stroke, self.fill] = _parse_string(color_string)
-        # save an index to our color in the list
-        self.n = self.find_index()
+            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):
@@ -261,40 +262,32 @@ class XoColor:
 
     def set_color(self, color_string):
         if color_string == None or not is_valid(color_string):
-            logging.debug('Color string is not valid: %s, '
-                          'fallback to default', color_string)
-        [self.stroke,self.fill] = _parse_string(color_string)
-        self.n = self.find_index()
-
-    def get_random_color(self):
-        my_n = int(random.random() * (len(colors) - 1))
-        [my_stroke, my_fill] = colors[my_n]
-        return "%s,%s" % (my_stroke, my_fill)
+            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):
-        my_n = self.n
-        my_n += 1
-        if my_n == len(colors):
-            my_n = 0
-        [my_stroke, my_fill] = colors[my_n]
-        return "%s,%s" % (my_stroke, my_fill)
+        next_index = self._current_color_index + 1
+        if next_index == len(colors):
+            next_index = 0
+        return "%s,%s" % (colors[next_index][0], colors[next_index][1])
 
     def get_prev_color(self):
-        my_n = self.n
-        my_n -= 1
-        if my_n < 0:
-            my_n = len(colors)-1
-        [my_stroke, my_fill] = colors[my_n]
-        return "%s,%s" % (my_stroke, my_fill)
+        prev_index = self._current_color_index - 1
+        if prev_index < 0:
+            prev_index = len(colors)-1
+        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 c in range(0,len(colors)):
-            if colors[c] == [self.stroke, self.fill]:
-                return c
-        # if the color is not found, then return 0
+    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__":