[hamster-applet] corrected color parsing into gdk.Color and added utility function Colors.contrast (is_light -> darke



commit 862b75e7560e6ffa2160154c0e74ee1865c972b8
Author: Toms Bauģis <toms baugis gmail com>
Date:   Mon Jul 12 13:26:14 2010 +0100

    corrected color parsing into gdk.Color and added utility function Colors.contrast (is_light -> darker, else -> lighter)

 src/hamster/graphics.py |   12 +++++++++++-
 1 files changed, 11 insertions(+), 1 deletions(-)
---
diff --git a/src/hamster/graphics.py b/src/hamster/graphics.py
index 15d6960..bf232ae 100644
--- a/src/hamster/graphics.py
+++ b/src/hamster/graphics.py
@@ -57,7 +57,7 @@ class Colors(object):
 
     def gdk(self, color):
         c = self.parse(color)
-        return gtk.gdk.Color(c[0] * 65535.0, c[1] * 65535.0, c[2] * 65535.0)
+        return gtk.gdk.Color(int(c[0] * 65535.0), int(c[1] * 65535.0), int(c[2] * 65535.0))
 
     def is_light(self, color):
         # tells you if color is dark or light, so you can up or down the
@@ -68,6 +68,16 @@ class Colors(object):
         # returns color darker by step (where step is in range 0..255)
         hls = colorsys.rgb_to_hls(*self.rgb(color))
         return colorsys.hls_to_rgb(hls[0], hls[1] - step, hls[2])
+
+    def contrast(self, color, step):
+        """if color is dark, will return a lighter one, otherwise darker"""
+        hls = colorsys.rgb_to_hls(*self.rgb(color))
+        if self.is_light(color):
+            return colorsys.hls_to_rgb(hls[0], hls[1] - step, hls[2])
+        else:
+            return colorsys.hls_to_rgb(hls[0], hls[1] + step, hls[2])
+        # returns color darker by step (where step is in range 0..255)
+
 Colors = Colors() # this is a static class, so an instance will do
 
 class Graphics(object):



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