Browse code

Move lookup_color into ui.colours

Now all dependencies on GUI library are in xdot.ui

Peter Hill authored on 02/07/2016 15:34:17 • Jose Fonseca committed on 10/07/2016 08:40:15
Showing 2 changed files

... ...
@@ -16,15 +16,9 @@
16 16
 import colorsys
17 17
 import sys
18 18
 
19
-import gi
20
-gi.require_version('Gtk', '3.0')
21
-gi.require_version('PangoCairo', '1.0')
22
-
23
-from gi.repository import Gdk
24
-
25 19
 from .lexer import ParseError, DotLexer
26 20
 
27
-from ..ui.colours import brewer_colors
21
+from ..ui.colours import lookup_color
28 22
 from ..ui.pen import Pen
29 23
 from ..ui import elements
30 24
 
... ...
@@ -166,36 +160,7 @@ class XDotAttrParser:
166 160
             sys.stderr.write('warning: color gradients not supported yet\n')
167 161
             return None
168 162
         else:
169
-            return self.lookup_color(c)
170
-
171
-    def lookup_color(self, c):
172
-        try:
173
-            color = Gdk.color_parse(c)
174
-        except ValueError:
175
-            pass
176
-        else:
177
-            s = 1.0/65535.0
178
-            r = color.red*s
179
-            g = color.green*s
180
-            b = color.blue*s
181
-            a = 1.0
182
-            return r, g, b, a
183
-
184
-        try:
185
-            dummy, scheme, index = c.split('/')
186
-            r, g, b = brewer_colors[scheme][int(index)]
187
-        except (ValueError, KeyError):
188
-            pass
189
-        else:
190
-            s = 1.0/255.0
191
-            r = r*s
192
-            g = g*s
193
-            b = b*s
194
-            a = 1.0
195
-            return r, g, b, a
196
-
197
-        sys.stderr.write("warning: unknown color '%s'\n" % c)
198
-        return None
163
+            return lookup_color(c)
199 164
 
200 165
     def parse(self):
201 166
         s = self
... ...
@@ -303,3 +303,46 @@ brewer_colors = {
303 303
     'ylorrd7': [(255, 255, 178), (254, 217, 118), (254, 178, 76), (253, 141, 60), (252, 78, 42), (227, 26, 28), (177, 0, 38)],
304 304
     'ylorrd8': [(255, 255, 204), (255, 237, 160), (254, 217, 118), (254, 178, 76), (253, 141, 60), (252, 78, 42), (227, 26, 28), (177, 0, 38)],
305 305
 }
306
+
307
+
308
+def lookup_color(c):
309
+    """Return RGBA values of colour c
310
+
311
+    c should be either an X11 colour or a brewer colour set and index
312
+    e.g. "navajowhite", "greens3/2"
313
+
314
+    """
315
+    import sys
316
+    import gi
317
+    gi.require_version('Gtk', '3.0')
318
+    gi.require_version('PangoCairo', '1.0')
319
+
320
+    from gi.repository import Gdk
321
+
322
+    try:
323
+        color = Gdk.color_parse(c)
324
+    except ValueError:
325
+        pass
326
+    else:
327
+        s = 1.0/65535.0
328
+        r = color.red*s
329
+        g = color.green*s
330
+        b = color.blue*s
331
+        a = 1.0
332
+        return r, g, b, a
333
+
334
+    try:
335
+        dummy, scheme, index = c.split('/')
336
+        r, g, b = brewer_colors[scheme][int(index)]
337
+    except (ValueError, KeyError):
338
+        pass
339
+    else:
340
+        s = 1.0/255.0
341
+        r = r*s
342
+        g = g*s
343
+        b = b*s
344
+        a = 1.0
345
+        return r, g, b, a
346
+
347
+    sys.stderr.write("warning: unknown color '%s'\n" % c)
348
+    return None