Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/colors.py
blob: c646baea6e25dd1c90abde31df1f269177ea440a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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-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