Rotation mutators
authorStefano Rivera <stefano@rivera.za.net>
Sun, 11 May 2014 16:41:18 +0000 (18:41 +0200)
committerStefano Rivera <stefano@rivera.za.net>
Sun, 11 May 2014 16:41:18 +0000 (18:41 +0200)
naja/resources/mutators.py [new file with mode: 0644]

diff --git a/naja/resources/mutators.py b/naja/resources/mutators.py
new file mode 100644 (file)
index 0000000..2a730de
--- /dev/null
@@ -0,0 +1,39 @@
+'''Mutations to apply to images'''
+
+from pygame.transform import rotate, flip
+
+
+class Mutator(object):
+    def __init__(self, func, *args):
+        self._func = func
+        self._args = tuple(args)
+
+    def __call__(self, image):
+        return self._func(image, *self._args)
+
+    def __hash__(self):
+        return hash((self._func, self._args))
+
+    def __eq__(self, other):
+        if not isinstance(other, Mutator):
+            return NotImplemented
+        return (self._func == other._func) and (self._args == other._args)
+
+    def __repr__(self):
+        return '<%s %r>' % (self.__class__.__name__, self._args)
+
+
+def rotator(angle):
+    return Mutator(rotate, angle)
+
+
+# Identity mutator
+NULL = Mutator(lambda x: x)
+
+# Rotation
+R90 = rotator(90)
+R180 = rotator(180)
+R270 = rotator(-90)
+
+FLIP_H = Mutator(flip, True, False)
+FLIP_V = Mutator(flip, False, True)