[pitivi: 9/27] Fixed common.unpack_color_32() and added unittests



commit 288f53104a4710dae0a19d96eff700aa22257f5e
Author: Alex BÄ?luÈ? <alexandru balut gmail com>
Date:   Mon Mar 14 10:36:07 2011 +0100

    Fixed common.unpack_color_32() and added unittests

 pitivi/ui/common.py  |   35 ++++++++++++++++++++++-------------
 tests/test_common.py |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 13 deletions(-)
---
diff --git a/pitivi/ui/common.py b/pitivi/ui/common.py
index d86b8f1..a4ec178 100644
--- a/pitivi/ui/common.py
+++ b/pitivi/ui/common.py
@@ -18,30 +18,39 @@ TRACK_SPACING = 8
 SPACING = 6
 PADDING = 6
 
-def pack_color_32(red, green, blue, alpha = 0xFFFF):
-   red = red >> 8
-   green = green >> 8
-   blue = blue >> 8
-   alpha = alpha >> 8
-   return (red << 24 | green << 16 | blue << 8 | alpha)
-
-def pack_color_64(red, green, blue, alpha = 0xFFFF):
-   return (red << 48 | green << 32 | blue << 16 | alpha)
+def pack_color_32(red, green, blue, alpha=0xFFFF):
+    """Packs the specified 16bit color values in a 32bit RGBA value."""
+    red = red >> 8
+    green = green >> 8
+    blue = blue >> 8
+    alpha = alpha >> 8
+    return (red << 24 | green << 16 | blue << 8 | alpha)
+
+def pack_color_64(red, green, blue, alpha=0xFFFF):
+    """Packs the specified 16bit color values in a 64bit RGBA value."""
+    return (red << 48 | green << 32 | blue << 16 | alpha)
 
 def unpack_color(value):
+    """Unpacks the specified RGBA value into four 16bit color values.
+
+    Args:
+      value: A 32bit or 64bit RGBA value.
+    """
     if not (value >> 32):
         return unpack_color_32(value)
     else:
         return unpack_color_64(value)
 
 def unpack_color_32(value):
-    red = (value >> 24); red = red | red << 8
-    green = (value >> 16) & 0xFF; green = green | green << 8
-    blue = (value >> 8) & 0xFF; blue = blue | blue << 8
-    alpha = value & 0xFF; alpha = alpha | alpha << 8
+    """Unpacks the specified 32bit RGBA value into four 16bit color values."""
+    red = (value >> 24) << 8
+    green = ((value >> 16) & 0xFF) << 8
+    blue = ((value >> 8) & 0xFF) << 8
+    alpha = (value & 0xFF) << 8
     return red, green, blue, alpha
 
 def unpack_color_64(value):
+    """Unpacks the specified 64bit RGBA value into four 16bit color values."""
     red = (value >> 48) & 0xFFFF
     green = (value >> 32) & 0xFFFF
     blue = (value >> 16) & 0xFFFF
diff --git a/tests/test_common.py b/tests/test_common.py
new file mode 100644
index 0000000..0706f74
--- /dev/null
+++ b/tests/test_common.py
@@ -0,0 +1,49 @@
+# PiTiVi , Non-linear video editor
+#
+#       tests/test_common.py
+#
+# Copyright (c) 2011 Google <aleb google com>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the
+# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+# Boston, MA 02111-1307, USA.
+
+import common
+from unittest import main
+from pitivi.ui import common as ui_common
+
+class TestColors(common.TestCase):
+
+    def test_pack_color_32(self):
+        self.assertEquals(
+                0x01020408,
+                ui_common.pack_color_32(0x01FF, 0x02FF, 0x04FF, 0x08FF))
+
+    def test_pack_color_64(self):
+        self.assertEquals(
+                0x01FF02FF04FF08FF,
+                ui_common.pack_color_64(0x01FF, 0x02FF, 0x04FF, 0x08FF))
+
+    def test_unpack_color_32(self):
+        self.assertEquals(
+                (0x0100, 0x0200, 0x0400, 0x0800),
+                ui_common.unpack_color_32(0x01020408))
+
+    def test_unpack_color_64(self):
+        self.assertEquals(
+                (0x01FF, 0x02FF, 0x04FF, 0x08FF),
+                ui_common.unpack_color_64(0x01FF02FF04FF08FF))
+
+if __name__ == "__main__":
+    main()



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