root/tags/0.4.1/src/color.py

Revision 96, 2.8 kB (checked in by lgs, 4 years ago)

Kill the whitespace

Line 
1# Copyright (c) 2007-2008 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
2#
3# This file is part of PyCha.
4#
5# PyCha is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# PyCha is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public License
16# along with PyCha.  If not, see <http://www.gnu.org/licenses/>.
17
18DEFAULT_COLOR = '#3c581a'
19
20def clamp(minValue, maxValue, value):
21    """Make sure value is between minValue and maxValue"""
22    if value < minValue:
23        return minValue
24    if value > maxValue:
25        return maxValue
26    return value
27
28def hex2rgb(hexstring, digits=2):
29    """Converts a hexstring color to a rgb tuple.
30
31    Example: #ff0000 -> (1.0, 0.0, 0.0)
32
33    digits is an integer number telling how many characters should be
34    interpreted for each component in the hexstring.
35    """
36    if isinstance(hexstring, (tuple, list)):
37        return hexstring
38
39    top = float(int(digits * 'f', 16))
40    r = int(hexstring[1:digits+1], 16)
41    g = int(hexstring[digits+1:digits*2+1], 16)
42    b = int(hexstring[digits*2+1:digits*3+1], 16)
43    return r / top, g / top, b / top
44
45def lighten(r, g, b, amount):
46    """Return a lighter version of the color (r, g, b)"""
47    return (clamp(0.0, 1.0, r + amount),
48            clamp(0.0, 1.0, g + amount),
49            clamp(0.0, 1.0, b + amount))
50
51def generateColorscheme(masterColor, keys):
52    """Generates a dictionary where the keys match the keys argument and
53    the values are colors derivated from the masterColor.
54
55    Each color is a lighter version of masterColor. This difference is
56    computed based on the number of keys.
57
58    The masterColor is given in a hex string format.
59    """
60    r, g, b = hex2rgb(masterColor)
61    light = 1.0 / (len(keys)*2)
62    return dict([(key, lighten(r, g, b, light * i))
63                 for i, key in enumerate(keys)])
64
65def defaultColorscheme(keys):
66    """Return the default color scheme (derived from a dark green)"""
67    return generateColorscheme(DEFAULT_COLOR, keys)
68
69def getColorscheme(color, keys):
70    """Get a color scheme from the six predefined ones or makes another
71    one if the color is not found
72    """
73    return generateColorscheme(colorSchemes.get(color, color), keys)
74
75# default colors for color schemes
76colorSchemes = dict(
77    red='#6d1d1d',
78    green=DEFAULT_COLOR,
79    blue='#224565',
80    grey='#444444',
81    black='#000000',
82    darkcyan='#305755'
83    )
Note: See TracBrowser for help on using the browser.