Ticket #1592: 0001-color-selector-feature.patch

File 0001-color-selector-feature.patch, 2.6 KB (added by walter, 11 years ago)
  • src/sugar/graphics/xocolor.py

    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  
    11# Copyright (C) 2006-2007 Red Hat, Inc.
     2# Copyright (C) 2008-2009 Sugar Labs
    23#
    34# This library is free software; you can redistribute it and/or
    45# modify it under the terms of the GNU Lesser General Public
    class XoColor: 
    240241            randomize = False
    241242
    242243        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()
    245245        else:
    246246            [self.stroke, self.fill] = _parse_string(color_string)
     247        # save an index to our color in the list
     248        self.n = self.find_index()
    247249
    248250    def __cmp__(self, other):
    249251        if isinstance(other, XoColor):
    class XoColor: 
    257259    def get_fill_color(self):
    258260        return self.fill
    259261
     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
    260290    def to_string(self):
    261291        return '%s,%s' % (self.stroke, self.fill)
    262292
     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
    263299
    264300if __name__ == "__main__":
    265301    import sys