From ca580095a6bd245e0c3ce5d653c9e968e570f242 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sun, 11 May 2014 18:41:18 +0200 Subject: [PATCH] Rotation mutators --- naja/resources/mutators.py | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 naja/resources/mutators.py diff --git a/naja/resources/mutators.py b/naja/resources/mutators.py new file mode 100644 index 0000000..2a730de --- /dev/null +++ b/naja/resources/mutators.py @@ -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) -- 2.34.1