File: color.py

package info (click to toggle)
python-plwm 2.6a%2B20080530-2
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 1,048 kB
  • ctags: 1,781
  • sloc: python: 7,574; ansic: 1,075; xml: 109; makefile: 83
file content (76 lines) | stat: -rw-r--r-- 2,675 bytes parent folder | download | duplicates (3)
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
#
# color.py -- Handle colors in a more symbolic way
#
#    Copyright (C) 1999-2001  Peter Liljenberg <petli@ctrl-c.liu.se>
#
#    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 2 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


import types

class ColorError(Exception): pass


# Screen mixin class
class Color:
    def __screen_client_init__(self):
        self.color_map = self.info.default_colormap
        self.color_alloced = {}

    def get_color(self, color, default = None):
        """Return the pixel value corresponding to COLOR.
        COLOR can be a string or tuple of (R, G, B).

        If the color can't be found and DEFAULT is provided, try to
        return that color instead.
        """

        try:
            return self.color_alloced[color]
        except KeyError:
            pass

        if type(color) is types.StringType:
            col = self.color_map.alloc_named_color(color)
        elif type(color) is types.TupleType and len(color) == 3:
            col = self.color_map.alloc_color(color[0], color[1], color[2])
        else:
            raise TypeError("string or 3-tuple expected")

        # If color allocation fails and there is a default color, simply
        # recurse to try to allocate it
        if col is None:
            if default:
                return self.get_color(default)
            else:
                raise ColorError(color)
        else:
            self.color_alloced[color] = col.pixel

        return self.color_alloced[color]

    def get_color_res(self, res_name, res_class, default = None):
        """Return the pixel value for the color defined in
        the X resource RES_NAME/RES_CLASS.

        If DEFAULT is provided, that name will be used if no matching
        X resource is found.  If omitted, ColorError will be raised.
        """
        col = self.wm.rdb_get(res_name, res_class, default)
        if col is None:
            raise ColorError('No color resource defined for %s/%s' % (res_name, res_class))

        return self.get_color(col, default)