Commit 9a1a0774 authored by Simon Feltman's avatar Simon Feltman Committed by Martin Pitt
Browse files

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: default avatarMartin Pitt <martinpitt@gnome.org>
parent d9608c33
......@@ -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')
......
......@@ -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
......@@ -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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment