[billreminder] put all the color parsing in single place. now we also accept html-style colors



commit b4e0c2a11751caac1fa7bfede09123897cc975dc
Author: Toms Bauģis <toms baugis gmail com>
Date:   Sun Nov 29 14:50:02 2009 +0000

    put all the color parsing in single place. now we also accept html-style colors

 src/gui/widgets/graphics.py |   34 ++++++++++++++++------------------
 1 files changed, 16 insertions(+), 18 deletions(-)
---
diff --git a/src/gui/widgets/graphics.py b/src/gui/widgets/graphics.py
index 2bdebb9..1a138bd 100644
--- a/src/gui/widgets/graphics.py
+++ b/src/gui/widgets/graphics.py
@@ -12,18 +12,24 @@ class Colors(object):
     almost_white = (250, 250, 250)
 
     @staticmethod
-    def normalize_rgb(color):
-        # turns your average rgb into values with components in range 0..1
-        # if none of the componets are over 1 - will return what it got
-        if color[0] > 1 or color[1] > 0 or color[2] > 0:
-            color = [c / 255.0 for c in color]
+    def color(color):
+        #parse color into rgb values
+        if isinstance(color, str) or isinstance(color, unicode):
+            color = gtk.gdk.Color(color)
+
+        if isinstance(color, gtk.gdk.Color):
+            color = [color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0]
+        else:
+            # otherwise we assume we have color components in 0..255 range
+            if color[0] > 1 or color[1] > 1 or color[2] > 1:
+                print color[0], color[1], color[2]
+                color = [c / 255.0 for c in color]
+
         return color
     
     @staticmethod
     def rgb(color):
-        #return color that has each component in 0..255 range
-        return [c*255 for c in Colors.normalize_rgb(color)]
-        
+        return [c * 255 for c in Colors.color(color)]
 
 class Area(gtk.DrawingArea):
     """Abstraction on top of DrawingArea to work specifically with cairo"""
@@ -97,15 +103,7 @@ class Area(gtk.DrawingArea):
 
     """ drawing on canvas bits """
     def __rectangle(self, x, y, w, h, color, opacity = 0):
-        if color[0] > 1: color = [c / 256.0 for c in color]
-
-        if opacity:
-            self.context.set_source_rgba(color[0], color[1], color[2], opacity)
-        elif len(color) == 3:
-            self.context.set_source_rgb(*color)
-        else:
-            self.context.set_source_rgba(*color)
-            
+        self.set_color(color, opacity)
         self.context.rectangle(x, y, w, h)
     
     def fill_area(self, x, y, w, h, color, opacity = 0):
@@ -128,7 +126,7 @@ class Area(gtk.DrawingArea):
         return self.layout.get_pixel_size()
         
     def set_color(self, color, opacity = None):
-        color = Colors.normalize_rgb(color)
+        color = Colors.color(color) #parse whatever we have there into a normalized triplet
 
         if opacity:
             self.context.set_source_rgba(color[0], color[1], color[2], opacity)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]