Ticket #1592: 0001-added-get_next-and-get_prev-fill-and-stroke-colors.patch

File 0001-added-get_next-and-get_prev-fill-and-stroke-colors.patch, 5.7 KB (added by walter, 14 years ago)

cleaned up version as per feedback

  • src/sugar/graphics/xocolor.py

    From 44564b8d20094d61f06b99f25726bdf5cd0829ff Mon Sep 17 00:00:00 2001
    From: Walter Bender <walter@sugarlabs.org>
    Date: Thu, 5 Aug 2010 07:34:03 -0400
    Subject: [PATCH] added get_next and get_prev fill and stroke colors
    
    ---
     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 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
    import logging 
    2425
    2526import gconf
    2627
     28_STROKE_COLOR = 0
     29_FILL_COLOR = 1
     30
    2731colors = [
    2832['#B20008', '#FF2B34'], \
    2933['#FF2B34', '#B20008'], \
    def _parse_string(color_string): 
    216220
    217221    splitted = color_string.split(',')
    218222    if len(splitted) == 2:
    219         return [splitted[0], splitted[1]]
     223        return [splitted[_STROKE_COLOR], splitted[_FILL_COLOR]]
    220224    else:
    221225        return None
    222226
    def is_valid(color_string): 
    225229    return (_parse_string(color_string) != None)
    226230
    227231
     232def get_random_color():
     233    """ Return a random color from the default colors-pair list. """
     234    color_index = int(random.random() * (len(colors) - 1))
     235    return "%s,%s" % (colors[color_index][_STROKE_COLOR],
     236                      colors[color_index][_FILL_COLOR])
     237
     238
     239def get_next_stroke_color(color):
     240    """ Return the next color pair in the list that shares the same fill
     241        as color. """
     242    current_index = _get_index(color)
     243    if current_index == -1:
     244        return "%s,%s" % (color.stroke, color.fill)
     245    next_index = _next_index(current_index)
     246    while(colors[next_index][_FILL_COLOR] != \
     247              colors[current_index][_FILL_COLOR]):
     248        next_index = _next_index(next_index)
     249    return "%s,%s" % (colors[next_index][_STROKE_COLOR],
     250                      colors[next_index][_FILL_COLOR])
     251
     252
     253def get_prev_stroke_color(color):
     254    """ Return the prev color pair in the list that shares the same fill
     255        as color. """
     256    current_index = _get_index(color)
     257    if current_index == -1:
     258        return "%s,%s" % (color.stroke, color.fill)
     259    prev_index = _prev_index(current_index)
     260    while (colors[prev_index][_FILL_COLOR] != \
     261               colors[current_index][_FILL_COLOR]):
     262        prev_index = _prev_index(prev_index)
     263    return "%s,%s" % (colors[prev_index][_STROKE_COLOR],
     264                      colors[prev_index][_FILL_COLOR])
     265
     266
     267def get_next_fill_color(color):
     268    """ Return the next color pair in the list that shares the same stroke
     269        as color. """
     270    current_index = _get_index(color)
     271    if current_index == -1:
     272        return "%s,%s" % (color.stroke, color.fill)
     273    next_index = _next_index(current_index)
     274    while (colors[next_index][_STROKE_COLOR] != \
     275               colors[current_index][_STROKE_COLOR]):
     276        next_index = _next_index(next_index)
     277    return "%s,%s" % (colors[next_index][_STROKE_COLOR],
     278                      colors[next_index][_FILL_COLOR])
     279
     280
     281def get_prev_fill_color(color):
     282    """ Return the prev color pair in the list that shares the same stroke
     283        as color. """
     284    current_index = _get_index(color)
     285    if current_index == -1:
     286        return "%s,%s" % (color.stroke, color.fill)
     287    prev_index = _prev_index(current_index)
     288    while (colors[prev_index][_STROKE_COLOR] != \
     289               colors[current_index][_STROKE_COLOR]):
     290        prev_index = _prev_index(prev_index)
     291    return "%s,%s" % (colors[prev_index][_STROKE_COLOR],
     292                      colors[prev_index][_FILL_COLOR])
     293
     294
     295def _next_index(current_index):
     296    next_index = current_index + 1
     297    if next_index == len(colors):
     298        next_index = 0
     299    return next_index
     300
     301
     302def _prev_index(current_index):
     303    prev_index = current_index - 1
     304    if prev_index < 0:
     305        prev_index = len(colors)-1
     306    return prev_index
     307
     308
     309def _get_index(color):
     310    for index in range(0, len(colors)):
     311        if colors[index] == [color.stroke, color.fill]:
     312            return index
     313    return -1
     314
     315
    228316class XoColor:
    229317
    230318    def __init__(self, color_string=None):
    231319        if color_string == None:
    232320            randomize = True
    233321        elif not is_valid(color_string):
    234             logging.debug('Color string is not valid: %s, '
    235                           'fallback to default', color_string)
     322            logging.error(
     323                'Color string is not valid: %s; fallback to default',
     324                color_string)
    236325            client = gconf.client_get_default()
    237326            color_string = client.get_string('/desktop/sugar/user/color')
    238327            randomize = False
    class XoColor: 
    240329            randomize = False
    241330
    242331        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)
     332            color_string = get_random_color()
     333        [self.stroke, self.fill] = _parse_string(color_string)
    247334
    248335    def __cmp__(self, other):
    249336        if isinstance(other, XoColor):
    class XoColor: 
    257344    def get_fill_color(self):
    258345        return self.fill
    259346
     347    def set_color(self, color_string):
     348        if color_string == None or not is_valid(color_string):
     349            logging.error(
     350                'Color string is not valid: %s; fallback to default',
     351                color_string)
     352        else:
     353            [self.stroke, self.fill] = _parse_string(color_string)
     354
    260355    def to_string(self):
    261356        return '%s,%s' % (self.stroke, self.fill)
    262357