Ticket #1592: 0001-extended-xocolor-for-new-color-selector.patch

File 0001-extended-xocolor-for-new-color-selector.patch, 4.5 KB (added by walter, 11 years ago)

and the latest version of xocolor.py for review

  • src/sugar/graphics/xocolor.py

    From ced1b1cde0117441665b5eae7ac7c20f7e39a743 Mon Sep 17 00:00:00 2001
    From: Walter Bender <walter@sugarlabs.org>
    Date: Tue, 20 Jul 2010 14:58:22 -0400
    Subject: [PATCH] extended xocolor for new color selector
    
    ---
     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 b  
    11# Copyright (C) 2006-2007 Red Hat, Inc.
     2# Copyright (C) 2008-2010 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
    def is_valid(color_string): 
    225226    return (_parse_string(color_string) != None)
    226227
    227228
     229def get_random_color():
     230    color_index = int(random.random() * (len(colors) - 1))
     231    return "%s,%s" % (colors[color_index][0], colors[color_index][1])
     232
     233
     234def _next_index(i):
     235    next_index = i + 1
     236    if next_index == len(colors):
     237        next_index = 0
     238    return next_index
     239
     240
     241def _prev_index(i):
     242    prev_index = i - 1
     243    if prev_index < 0:
     244        prev_index = len(colors)-1
     245    return prev_index
     246
     247
    228248class XoColor:
    229249
    230250    def __init__(self, color_string=None):
    231251        if color_string == None:
    232252            randomize = True
    233253        elif not is_valid(color_string):
    234             logging.debug('Color string is not valid: %s, '
    235                           'fallback to default', color_string)
     254            logging.error(
     255                'Color string is not valid: %s; fallback to default',
     256                color_string)
    236257            client = gconf.client_get_default()
    237258            color_string = client.get_string('/desktop/sugar/user/color')
    238259            randomize = False
    class XoColor: 
    240261            randomize = False
    241262
    242263        if randomize:
    243             n = int(random.random() * (len(colors) - 1))
    244             [self.stroke, self.fill] = colors[n]
    245         else:
    246             [self.stroke, self.fill] = _parse_string(color_string)
     264            color_string = get_random_color()
     265        [self.stroke, self.fill] = _parse_string(color_string)
     266        self._current_color_index = self._find_index()
    247267
    248268    def __cmp__(self, other):
    249269        if isinstance(other, XoColor):
    class XoColor: 
    257277    def get_fill_color(self):
    258278        return self.fill
    259279
     280    def set_color(self, color_string):
     281        if color_string == None or not is_valid(color_string):
     282            logging.error(
     283                'Color string is not valid: %s; fallback to default',
     284                color_string)
     285        else:
     286            [self.stroke, self.fill] = _parse_string(color_string)
     287        self._current_color_index = self._find_index()
     288
     289    def get_next_color(self):
     290        next_index = _next_index(self._current_color_index)
     291        return "%s,%s" % (colors[next_index][0], colors[next_index][1])
     292
     293    def get_prev_color(self):
     294        prev_index = _prev_index(self._current_color_index)
     295        return "%s,%s" % (colors[prev_index][0], colors[prev_index][1])
     296
     297    def get_next_stroke_color(self):
     298        next_index = _next_index(self._current_color_index)
     299        while (colors[next_index][1] != self.fill):
     300            next_index = _next_index(next_index)
     301        return "%s,%s" % (colors[next_index][0], colors[next_index][1])
     302
     303    def get_prev_stroke_color(self):
     304        prev_index = _prev_index(self._current_color_index)
     305        while (colors[prev_index][1] != self.fill):
     306            prev_index = _prev_index(prev_index)
     307        return "%s,%s" % (colors[prev_index][0], colors[prev_index][1])
     308
     309    def get_next_fill_color(self):
     310        next_index = _next_index(self._current_color_index)
     311        while (colors[next_index][0] != self.stroke):
     312            next_index = _next_index(next_index)
     313        return "%s,%s" % (colors[next_index][0], colors[next_index][1])
     314
     315    def get_prev_fill_color(self):
     316        prev_index = _prev_index(self._current_color_index)
     317        while (colors[prev_index][0] != self.stroke):
     318            prev_index = _prev_index(prev_index)
     319        return "%s,%s" % (colors[prev_index][0], colors[prev_index][1])
     320
    260321    def to_string(self):
    261322        return '%s,%s' % (self.stroke, self.fill)
    262323
     324    def _find_index(self):
     325        for color in range(0, len(colors)):
     326            if colors[color] == [self.stroke, self.fill]:
     327                return color
     328        return 0
    263329
    264330if __name__ == "__main__":
    265331    import sys