1
2
3
4
5
6
7
8
9
10
11 """ Colors module
12
13 Provides:
14
15 o ColorTranslator - class to convert tuples of integers and floats into
16 colors.Color objects
17
18 For drawing capabilities, this module uses reportlab to define colors:
19
20 http://www.reportlab.com
21 """
22
23
24 from reportlab.lib import colors
25
27 """ Class providing methods for translating representations of color into
28 """
30 """ __init__(self, filename)
31
32 o filename Location of a file containing colorscheme
33 information
34
35 Optional parameters set the color scheme
36 """
37 self._artemis_colorscheme = {0: (colors.Color(1, 1, 1,), "pathogenicity, adaptation, chaperones"),
38 1: (colors.Color(0.39, 0.39, 0.39), "energy metabolism"),
39 2: (colors.Color(1, 0, 0), "information transfer"),
40 3: (colors.Color(0, 1, 0), "surface"),
41 4: (colors.Color(0, 0, 1), "stable RNA"),
42 5: (colors.Color(0, 1, 1), "degradation of large molecules"),
43 6: (colors.Color(1, 0, 1), "degradation of small molecules"),
44 7: (colors.Color(1, 1, 0), "central/intermediary/miscellaneous metabolism"),
45 8: (colors.Color(0.60, 0.98, 0.60), "unknown"),
46 9: (colors.Color(0.53, 0.81, 0.98), "regulators"),
47 10: (colors.Color(1, 0.65, 0), "conserved hypotheticals"),
48 11: (colors.Color(0.78, 0.59, 0.39), "pseudogenes and partial genes"),
49 12: (colors.Color(1, 0.78, 0.78), "phage/IS elements"),
50 13: (colors.Color(0.70, 0.70, 0.70), "some miscellaneous information"),
51 14: (colors.Color(0, 0, 0), ""),
52 15: (colors.Color(1, 0.25, 0.25), "secondary metabolism"),
53 16: (colors.Color(1, 0.5, 0.5), ""),
54 17: (colors.Color(1, 0.75, 0.75), "")
55 }
56 self._colorscheme = {}
57 if filename is not None:
58 self.read_colorscheme(filename)
59 else:
60 self._colorscheme = self._artemis_colorscheme
61
62
63 - def translate(self, color=None, colour=None):
64 """ translate(self, color)
65
66 o color Color defined as an int, a tuple of three ints 0->255
67 or a tuple of three floats 0 -> 1, or a string giving
68 one of the named colors defined by ReportLab.
69
70 (This argument is overridden by a backwards compatible
71 argument with UK spelling, colour).
72
73 Returns a colors.Color object, determined semi-intelligently
74 depending on the input values
75 """
76
77 if colour is not None:
78 color = colour
79
80 if color is None:
81 raise ValueError, "Passed color (or colour) must be a valid color type"
82 if type(color) == type(1):
83 color = self.scheme_color(color)
84 elif isinstance(color, basestring):
85
86 color = colors.toColor(color)
87 elif type(color) == type((1., 2., 3.)) and type(color[0]) == type(1.):
88 color = self.float1_color(color)
89 elif type(color) == type((1, 2, 3)) and type(color[0]) == type(1):
90 color = self.int255_color(color)
91 return color
92
93
95 """ read_colorscheme(self, filename)
96
97 o filename The location of a file defining colors in tab-separated
98 format plaintext as:
99 INT \t RED \t GREEN \t BLUE \t Comment
100 Where RED, GREEN and BLUE are intensities in the range
101 0 -> 255
102 e.g.
103 2 \t 255 \t 0 \t 0 \t Red: Information transfer
104
105 Reads information from a file containing color information and
106 stores it internally
107 """
108 lines = open(filename, 'r').readlines()
109 for line in lines:
110 data = line.strip().split('\t')
111 try:
112 label = int(data[0])
113 red, green, blue = int(data[1]), int(data[2]), int(data[3])
114 if len(data) > 4:
115 comment = data[4]
116 else:
117 comment = ""
118 self._colorscheme[label] = (self.int255_color((red, green, blue)),
119 comment)
120 except:
121 raise IOError, "Expected INT \t INT \t INT \t INT \t string input"
122
124 """ get_artemis_colorscheme(self)
125
126 Return the Artemis color scheme as a dictionary
127 """
128 return self._artemis_colorscheme
129
130
132 """ artemis_color(self, value)
133
134 o value An int representing a functional class in the Artemis
135 color scheme (see www.sanger.ac.uk for a description)
136
137 Takes an int representing a functional class in the Artemis color
138 scheme, and returns the appropriate colors.Color object
139 """
140 value = int(value)
141 if value in self._artemis_colorscheme:
142 return self._artemis_colorscheme[value][0]
143 else:
144 raise ValueError, "Artemis color out of range: %d" % value
145
146
148 """ get_colorscheme(self)
149
150 Return the user-defined color scheme as a dictionary
151 """
152 return self._colorscheme
153
154
156 """ scheme_color(self, value)
157
158 o value An int representing a single color in the user-defined
159 color scheme
160
161 Takes an int representing a user-defined color and returns the
162 appropriate colors.Color object
163 """
164 if value in self._colorscheme:
165 return self._colorscheme[value][0]
166 else:
167 raise ValueError, "Scheme color out of range: %d" % value
168
169
171 """ int255_color(self, values)
172
173 o values A tuple of (red, green, blue) intensities as
174 integers in the range 0->255
175
176 Takes a tuple of (red, green, blue) intensity values in the range
177 0 -> 255 and returns an appropriate colors.Color object
178 """
179 red, green, blue = values
180 factor = 1/255.
181 red, green, blue = red * factor, green * factor, blue * factor
182 return colors.Color(red, green, blue)
183
184
186 """ float1_color(self, values)
187
188 o values A tuple of (red, green, blue) intensities as floats
189 in the range 0 -> 1
190
191 Takes a tuple of (red, green, blue) intensity values in the range
192 0 -> 1 and returns an appropriate colors.Color object
193 """
194 red, green, blue = values
195 return colors.Color(red, green, blue)
196
197
198
199
200
201
202 if __name__ == '__main__':
203
204
205 gdct = ColorTranslator()
206 print gdct.float1_color((0.5, 0.5, 0.5))
207 print gdct.int255_color((1, 75, 240))
208 print gdct.artemis_color(7)
209 print gdct.scheme_color(2)
210
211 print gdct.translate((0.5, 0.5, 0.5))
212 print gdct.translate((1, 75, 240))
213 print gdct.translate(7)
214 print gdct.translate(2)
215