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, 13 years ago) |
---|
-
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 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 … … def is_valid(color_string): 225 226 return (_parse_string(color_string) != None) 226 227 227 228 229 def 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 234 def _next_index(i): 235 next_index = i + 1 236 if next_index == len(colors): 237 next_index = 0 238 return next_index 239 240 241 def _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 228 248 class XoColor: 229 249 230 250 def __init__(self, color_string=None): 231 251 if color_string == None: 232 252 randomize = True 233 253 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) 236 257 client = gconf.client_get_default() 237 258 color_string = client.get_string('/desktop/sugar/user/color') 238 259 randomize = False … … class XoColor: 240 261 randomize = False 241 262 242 263 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() 247 267 248 268 def __cmp__(self, other): 249 269 if isinstance(other, XoColor): … … class XoColor: 257 277 def get_fill_color(self): 258 278 return self.fill 259 279 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 260 321 def to_string(self): 261 322 return '%s,%s' % (self.stroke, self.fill) 262 323 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 263 329 264 330 if __name__ == "__main__": 265 331 import sys