"""Color handling. Utilities for handle color data and conversions. """ # XOColor from Sugar Toolkit: # Copyright (C) 2006-2007 Red Hat, Inc. # # Adaptation and more: # Copyright (C) 2012-2013 Daniel Francis # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. import logging logger = logging.getLogger('colors') # Part from Sugar Toolkit with very few adaptations. def _parse_string(color_string): if not isinstance(color_string, (str, unicode)): logging.error('Invalid color string: %r', color_string) return None if color_string == 'white': return ['#ffffff', '#414141'] elif color_string == 'insensitive': return ['#ffffff', '#e2e2e2'] splitted = color_string.split(',') if len(splitted) == 2: return [splitted[0], splitted[1]] else: return None def is_valid(color_string): return (_parse_string(color_string) != None) class XoColor: def __init__(self, color_string): if not is_valid(color_string): logger.debug('Color string is not valid: %s, ' 'fallback to default', color_string) raise Exception [self.stroke, self.fill] = _parse_string(color_string) def __cmp__(self, other): if isinstance(other, XoColor): if self.stroke == other.stroke and self.fill == other.fill: return 0 return -1 def get_stroke_color(self): return self.stroke def get_fill_color(self): return self.fill def to_string(self): return '%s,%s' % (self.stroke, self.fill) # Added in Sweetener def color2string(color): """Converts a GdkColor to a RGB string color -- Gdk.Color """ color_string = ["#"] color_string.append("%02x" % (color.red / 256)) color_string.append("%02x" % (color.green / 256)) color_string.append("%02x" % (color.blue / 256)) string = "".join(color_string) #logger.debug(str(color) + ' ' + string) return string