From: Stefano Rivera Date: Sun, 11 May 2014 16:41:18 +0000 (+0200) Subject: Rotation mutators X-Git-Tag: 0.1~411^2 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=ca580095a6bd245e0c3ce5d653c9e968e570f242;hp=ea0fea7855b70665b1b155f1d20495e88096b73a;p=naja.git Rotation mutators --- 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)