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, 13 years ago) |
---|
-
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 1 1 # Copyright (C) 2006-2007 Red Hat, Inc. 2 # Copyright (C) 2008-2010 Sugar Labs 2 3 # 3 4 # This library is free software; you can redistribute it and/or 4 5 # modify it under the terms of the GNU Lesser General Public … … import logging 24 25 25 26 import gconf 26 27 28 _STROKE_COLOR = 0 29 _FILL_COLOR = 1 30 27 31 colors = [ 28 32 ['#B20008', '#FF2B34'], \ 29 33 ['#FF2B34', '#B20008'], \ … … def _parse_string(color_string): 216 220 217 221 splitted = color_string.split(',') 218 222 if len(splitted) == 2: 219 return [splitted[ 0], splitted[1]]223 return [splitted[_STROKE_COLOR], splitted[_FILL_COLOR]] 220 224 else: 221 225 return None 222 226 … … def is_valid(color_string): 225 229 return (_parse_string(color_string) != None) 226 230 227 231 232 def 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 239 def 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 253 def 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 267 def 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 281 def 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 295 def _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 302 def _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 309 def _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 228 316 class XoColor: 229 317 230 318 def __init__(self, color_string=None): 231 319 if color_string == None: 232 320 randomize = True 233 321 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) 236 325 client = gconf.client_get_default() 237 326 color_string = client.get_string('/desktop/sugar/user/color') 238 327 randomize = False … … class XoColor: 240 329 randomize = False 241 330 242 331 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) 247 334 248 335 def __cmp__(self, other): 249 336 if isinstance(other, XoColor): … … class XoColor: 257 344 def get_fill_color(self): 258 345 return self.fill 259 346 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 260 355 def to_string(self): 261 356 return '%s,%s' % (self.stroke, self.fill) 262 357