[pygobject] Gdk Color override should support red/green/blue_float properties



commit 9a1a07742ec0b1821d469603f9996a2b7d832f40
Author: Simon Feltman <s feltman gmail com>
Date:   Sun May 6 18:10:39 2012 -0700

    Gdk Color override should support red/green/blue_float properties
    
    Added red_float, green_float, and blue_float properties to Color.
    Also added Color.from_floats, RGBA.to_color, and RGBA.from_color.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=675579
    
    Signed-off-by: Martin Pitt <martinpitt gnome org>

 gi/overrides/Gdk.py     |   44 ++++++++++++++++++++++++++++++++++++++++++++
 tests/Makefile.am       |    2 +-
 tests/test_overrides.py |   17 +++++++++++++++++
 3 files changed, 62 insertions(+), 1 deletions(-)
---
diff --git a/gi/overrides/Gdk.py b/gi/overrides/Gdk.py
index 604ac37..66d47ef 100644
--- a/gi/overrides/Gdk.py
+++ b/gi/overrides/Gdk.py
@@ -30,6 +30,7 @@ __all__ = []
 
 
 class Color(Gdk.Color):
+    MAX_VALUE = 65535
 
     def __init__(self, red, green, blue):
         Gdk.Color.__init__(self)
@@ -46,6 +47,28 @@ class Color(Gdk.Color):
     def __repr__(self):
         return '<Gdk.Color(red=%d, green=%d, blue=%d)>' % (self.red, self.green, self.blue)
 
+    red_float = property(fget=lambda self: self.red / float(self.MAX_VALUE),
+                         fset=lambda self, v: setattr(self, 'red', int(v * self.MAX_VALUE)))
+
+    green_float = property(fget=lambda self: self.green / float(self.MAX_VALUE),
+                           fset=lambda self, v: setattr(self, 'green', int(v * self.MAX_VALUE)))
+
+    blue_float = property(fget=lambda self: self.blue / float(self.MAX_VALUE),
+                          fset=lambda self, v: setattr(self, 'blue', int(v * self.MAX_VALUE)))
+
+    def to_floats(self):
+        """Return (red_float, green_float, blue_float) triple."""
+
+        return (self.red_float, self.green_float, self.blue_float)
+
+    @staticmethod
+    def from_floats(red, green, blue):
+        """Return a new Color object from red/green/blue values from 0.0 to 1.0."""
+
+        return Color(int(red * Color.MAX_VALUE),
+                     int(green * Color.MAX_VALUE),
+                     int(blue * Color.MAX_VALUE))
+
 Color = override(Color)
 __all__.append('Color')
 
@@ -67,6 +90,27 @@ if Gdk._version == '3.0':
         def __repr__(self):
             return '<Gdk.Color(red=%f, green=%f, blue=%f, alpha=%f)>' % (self.red, self.green, self.blue, self.alpha)
 
+        def __iter__(self):
+            """Iterator which allows easy conversion to tuple and list types."""
+
+            yield self.red
+            yield self.green
+            yield self.blue
+            yield self.alpha
+
+        def to_color(self):
+            """Converts this RGBA into a Color instance which excludes alpha."""
+
+            return Color(int(self.red * Color.MAX_VALUE),
+                         int(self.green * Color.MAX_VALUE),
+                         int(self.blue * Color.MAX_VALUE))
+
+        @classmethod
+        def from_color(cls, color):
+            """Returns a new RGBA instance given a Color instance."""
+
+            return cls(color.red_float, color.green_float, color.blue_float)
+
     RGBA = override(RGBA)
     __all__.append('RGBA')
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a54bed8..7b2390a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -118,6 +118,6 @@ check.nemiver:
 	EXEC_NAME="nemiver" $(MAKE) check
 
 check.valgrind:
-	EXEC_NAME="valgrind --suppressions=python.supp" G_SLICE=always-malloc G_DEBUG=gc-friendly $(MAKE) check
+	EXEC_NAME="valgrind --leak-check=full --show-possibly-lost=no --suppressions=python.supp" G_SLICE=always-malloc G_DEBUG=gc-friendly $(MAKE) check
 
 -include $(top_srcdir)/git.mk
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index 4e3f7d2..35b7dfe 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -501,6 +501,19 @@ class TestGdk(unittest.TestCase):
         self.assertEqual(color, Gdk.Color(100, 200, 300))
         self.assertNotEqual(color, Gdk.Color(1, 2, 3))
 
+    def test_color_floats(self):
+        self.assertEqual(Gdk.Color(13107, 21845, 65535),
+                         Gdk.Color.from_floats(0.2, 1.0 / 3.0, 1.0))
+
+        self.assertEqual(Gdk.Color(13107, 21845, 65535).to_floats(),
+                         (0.2, 1.0 / 3.0, 1.0))
+
+        self.assertEqual(Gdk.RGBA(0.2, 1.0 / 3.0, 1.0, 0.5).to_color(),
+                         Gdk.Color.from_floats(0.2, 1.0 / 3.0, 1.0))
+
+        self.assertEqual(Gdk.RGBA.from_color(Gdk.Color(13107, 21845, 65535)),
+                         Gdk.RGBA(0.2, 1.0 / 3.0, 1.0, 1.0))
+
     def test_rgba(self):
         self.assertEqual(Gdk.RGBA, overrides.Gdk.RGBA)
         rgba = Gdk.RGBA(0.1, 0.2, 0.3, 0.4)
@@ -513,6 +526,10 @@ class TestGdk(unittest.TestCase):
         rgba.green = 0.9
         self.assertEqual(rgba.green, 0.9)
 
+        # Iterator/tuple convsersion
+        self.assertEqual(tuple(Gdk.RGBA(0.1, 0.2, 0.3, 0.4)),
+                         (0.1, 0.2, 0.3, 0.4))
+
     def test_event(self):
         event = Gdk.Event.new(Gdk.EventType.CONFIGURE)
         self.assertEqual(event.type, Gdk.EventType.CONFIGURE)



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