bugfix with calendar
[openerp-client.git] / bin / SpiffGtkWidgets / color.py
blob190a670391ac725ee04b3797ba0076ed2a2d0f3f
1 # Copyright (C) 2008 Samuel Abels, http://debain.org
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License version 2, as
5 # published by the Free Software Foundation.
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 import gtk.gdk
17 ########################
18 # Explicit converters.
19 ########################
20 def str2gdk(name):
21 return gtk.gdk.color_parse(name)
23 def int2gdk(i):
24 red = (i >> 24) & 0xff
25 green = (i >> 16) & 0xff
26 blue = (i >> 8) & 0xff
27 return gtk.gdk.Color(red * 256, green * 256, blue * 256)
29 def rgb2gdk(color):
30 red = color[0] * 65535.0
31 green = color[1] * 65535.0
32 blue = color[2] * 65535.0
33 return gtk.gdk.Color(red, green, blue)
35 def rgba2gdk(color):
36 red = color[0] * 65535.0
37 green = color[1] * 65535.0
38 blue = color[2] * 65535.0
39 value = color[3] * 65535.0 # not supported by gdk.Color
40 return gtk.gdk.Color(red, green, blue)
42 def gdk2int(color):
43 return (color.red / 256 << 24) \
44 | (color.green / 256 << 16) \
45 | (color.blue / 256 << 8) \
46 | 0xff
48 def gdk2rgb(color):
49 return (color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0)
51 def gdk2rgba(color):
52 return (color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0, 1)
54 ########################
55 # Automatic converters.
56 ########################
57 def to_gdk(color):
58 if isinstance(color, gtk.gdk.Color):
59 return color
60 elif type(color) == type(0) or type(color) == type(0l):
61 return int2gdk(color)
62 elif type(color) == type(''):
63 return str2gdk(color)
64 elif type(color) == type(()) and len(color) == 3:
65 return rgb2gdk(color)
66 elif type(color) == type(()) and len(color) == 4:
67 return rgba2gdk(color)
68 else:
69 raise TypeError('%s is not a known color type' % type(color))
71 def to_int(color):
72 return gdk2int(to_gdk(color))
74 def to_rgb(color):
75 return gdk2rgb(to_gdk(color))
77 def to_rgba(color):
78 return gdk2rgba(to_gdk(color))