diff options
author | Daniel Francis <francis@sugarlabs.org> | 2012-10-23 12:57:06 (GMT) |
---|---|---|
committer | Daniel Francis <francis@sugarlabs.org> | 2012-10-23 12:57:06 (GMT) |
commit | 9c7c915efa9d1815ea2e47f29ff549781d50c083 (patch) | |
tree | 12af6185de444135dc87573012f33afb6c8af7c9 /colors.py |
Initial commit
Moved from git.sl.org/sweetener/sweetener
Signed-off-by: Daniel Francis <francis@sugarlabs.org>
Diffstat (limited to 'colors.py')
-rw-r--r-- | colors.py | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/colors.py b/colors.py new file mode 100644 index 0000000..cff7ca1 --- /dev/null +++ b/colors.py @@ -0,0 +1,88 @@ +"""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 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 -- gtk.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 |