Features/Enhanced color selector/Patch-xocolor

From Sugar Labs
Jump to navigation Jump to search
--- src/sugar/graphics/xocolor.py	2009-11-19 14:54:22.000000000 -0500
+++ src/sugar/graphics/xocolor.py	2009-11-19 15:00:58.000000000 -0500
@@ -1,4 +1,5 @@
 # Copyright (C) 2006-2007 Red Hat, Inc.
+# Copyright (C) 2008-2009 Sugar Labs
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -240,10 +241,11 @@ class XoColor:
             randomize = False
 
         if randomize:
-            n = int(random.random() * (len(colors) - 1))
-            [self.stroke, self.fill] = colors[n]
+            [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()
 
     def __cmp__(self, other):
         if isinstance(other, XoColor):
@@ -257,9 +259,43 @@ 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)
+
+    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):
         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
+        return 0
 
 if __name__ == "__main__":
     import sys