My favorites | Sign in
Project Home
Checkout   Browse   Changes    
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Copyright (C) 2008 Samuel Abels, http://debain.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# 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 gtk.gdk

########################
# Explicit converters.
########################
def str2gdk(name):
return gtk.gdk.color_parse(name)

def int2gdk(i):
red = (i >> 24) & 0xff
green = (i >> 16) & 0xff
blue = (i >> 8) & 0xff
return gtk.gdk.Color(red * 256, green * 256, blue * 256)

def rgb2gdk(color):
red = int(color[0] * 65535)
green = int(color[1] * 65535)
blue = int(color[2] * 65535)
return gtk.gdk.Color(red, green, blue)

def rgba2gdk(color):
red = int(color[0] * 65535)
green = int(color[1] * 65535)
blue = int(color[2] * 65535)
value = int(color[3] * 65535) # not supported by gdk.Color
return gtk.gdk.Color(red, green, blue)

def gdk2int(color):
return (color.red / 256 << 24) \
| (color.green / 256 << 16) \
| (color.blue / 256 << 8) \
| 0xff

def gdk2rgb(color):
return (color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0)

def gdk2rgba(color):
return (color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0, 1)

########################
# Automatic converters.
########################
def to_gdk(color):
if isinstance(color, gtk.gdk.Color):
return color
elif type(color) == type(0) or type(color) == type(0l):
return int2gdk(color)
elif type(color) == type(''):
return str2gdk(color)
elif type(color) == type(()) and len(color) == 3:
return rgb2gdk(color)
elif type(color) == type(()) and len(color) == 4:
return rgba2gdk(color)
else:
raise TypeError('%s is not a known color type' % type(color))

def to_int(color):
return gdk2int(to_gdk(color))

def to_rgb(color):
return gdk2rgb(to_gdk(color))

def to_rgba(color):
return gdk2rgba(to_gdk(color))

########################
# Other functions.
########################
# Tango colors:
# http://tango.freedesktop.org/Tango_Icon_Theme_Guidelines
# light mid dark
palette = (('#fce94f', '#edd400', '#c4a000'), # Butter
('#fcaf3e', '#f57900', '#ce5c00'), # Orange
('#e9b96e', '#c17d11', '#8f5902'), # Chocolate
('#8ae234', '#73d216', '#4e9a06'), # Chameleon
('#729fcf', '#3465a4', '#204a87'), # Sky Blue
('#ad7fa8', '#75507b', '#5c35cc'), # Plum
('#ef2929', '#cc0000', '#a40000'), # Scarlet Red
('#eeeeec', '#d3d7cf', '#babdb6'), # Aluminium (bright)
('#888a85', '#555753', '#2e3436')) # Aluminium (dark)
def from_string(string, n_colors = 1):
string += 'b'
tuple = palette[string.__hash__() % len(palette)]
if n_colors == 1:
return to_rgb(tuple[0])
return [to_rgb(color) for color in tuple[:n_colors]]

def bg_color2text_color(color):
if sum(to_rgb(color)) > 1.9:
return 0, 0, 0
return 1, 1, 1

Change log

r45 by knipknap on Feb 19, 2009   Diff
Generate better colors for annotations by
using the tango theme.
Go to: 
Project members, sign in to write a code review

Older revisions

r41 by knipknap on Feb 18, 2009   Diff
Fix: color.from_string() would
sometimes not work.
r37 by knipknap on Feb 17, 2009   Diff
Add preliminary support for automatic
annotation coloring. Add support for
hyperlinks in the textview.
r11 by knipknap on Jul 4, 2008   Diff
Corrected copyright headers, remove a
useless file, cleanups.
All revisions of this file

File info

Size: 3528 bytes, 105 lines
Powered by Google Project Hosting