Ticket #1592: xocolor.patch

File xocolor.patch, 4.9 KB (added by walter, 9 years ago)

new as of 24-01-10

  • src/sugar/graphics/xocolor.py

    diff --git a/src/sugar/graphics/xocolor.py b/src/sugar/graphics/xocolor.py
    index 75b5400..f516f84 100644
    a b def _parse_string(color_string): 
    221221    else:
    222222        return None
    223223
    224 
    225224def is_valid(color_string):
    226225    return (_parse_string(color_string) != None)
    227226
     227def get_random_color():
     228    color_index = int(random.random() * (len(colors) - 1))
     229    return "%s,%s" % (colors[color_index][0], colors[color_index][1])
     230
     231def _next_index(i):
     232    next_index = i + 1
     233    if next_index == len(colors):
     234        next_index = 0
     235    return next_index
     236
     237def _prev_index(i):
     238    prev_index = i - 1
     239    if prev_index < 0:
     240        prev_index = len(colors)-1
     241    return prev_index
    228242
    229243class XoColor:
    230244
    class XoColor: 
    232246        if color_string == None:
    233247            randomize = True
    234248        elif not is_valid(color_string):
    235             logging.debug('Color string is not valid: %s, '
    236                           'fallback to default', color_string)
     249            logging.error(
     250                'Color string is not valid: %s; fallback to default',
     251                color_string)
    237252            client = gconf.client_get_default()
    238253            color_string = client.get_string('/desktop/sugar/user/color')
    239254            randomize = False
    class XoColor: 
    241256            randomize = False
    242257
    243258        if randomize:
    244             [self.strole, self.fill] = self.get_random_color()
    245         else:
    246             [self.stroke, self.fill] = _parse_string(color_string)
    247         # save an index to our color in the list
    248         self.n = self.find_index()
     259            color_string =  get_random_color()
     260        [self.stroke, self.fill] = _parse_string(color_string)
     261        self._current_color_index = self._find_index()
    249262
    250263    def __cmp__(self, other):
    251264        if isinstance(other, XoColor):
    class XoColor: 
    261274
    262275    def set_color(self, color_string):
    263276        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)
     277            logging.error(
     278                'Color string is not valid: %s; fallback to default',
     279                color_string)
     280        else:
     281            [self.stroke, self.fill] = _parse_string(color_string)
     282        self._current_color_index = self._find_index()
    273283
    274284    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)
     285        next_index = _next_index(self._current_color_index)
     286        return "%s,%s" % (colors[next_index][0], colors[next_index][1])
    281287
    282288    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        prev_index = _prev_index(self._current_color_index)
     290        return "%s,%s" % (colors[prev_index][0], colors[prev_index][1])
     291
     292    def get_next_stroke_color(self):
     293        next_index = _next_index(self._current_color_index)
     294        while (colors[next_index][0] != self.stroke):
     295            next_index = _next_index(next_index)
     296        return "%s,%s" % (colors[next_index][0], colors[next_index][1])
     297
     298    def get_prev_stroke_color(self):
     299        prev_index = _prev_index(self._current_color_index)
     300        while (colors[prev_index][0] != self.stroke):
     301            prev_index = _prev_index(prev_index)
     302        return "%s,%s" % (colors[prev_index][0], colors[prev_index][1])
     303
     304    def get_next_fill_color(self):
     305        next_index = _next_index(self._current_color_index)
     306        while (colors[next_index][1] != self.fill):
     307            next_index = _next_index(next_index)
     308        return "%s,%s" % (colors[next_index][0], colors[next_index][1])
     309
     310    def get_prev_fill_color(self):
     311        prev_index = _prev_index(self._current_color_index)
     312        while (colors[prev_index][1] != self.fill):
     313            prev_index = _prev_index(prev_index)
     314        return "%s,%s" % (colors[prev_index][0], colors[prev_index][1])
    289315
    290316    def to_string(self):
    291317        return '%s,%s' % (self.stroke, self.fill)
    292318
    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
     319    def _find_index(self):
     320        for color in range(0,len(colors)):
     321            if colors[color] == [self.stroke, self.fill]:
     322                return color
    298323        return 0
    299324
    300325if __name__ == "__main__":