source: nagslang/mutators.py @ 235:831e4f6b3d18

Last change on this file since 235:831e4f6b3d18 was 56:b9430b4a48da, checked in by Stefano Rivera <stefano@…>, 7 years ago

Now with a werewolf

File size: 1.4 KB
Line 
1'''Mutations to apply to images'''
2
3import pygame
4from pygame.transform import rotate, flip
5
6
7class Mutator(object):
8    def __init__(self, func, *args):
9        self._func = func
10        self._args = tuple(args)
11
12    def __call__(self, image):
13        return self._func(image, *self._args)
14
15    def __hash__(self):
16        return hash((self._func, self._args))
17
18    def __eq__(self, other):
19        if not isinstance(other, Mutator):
20            return NotImplemented
21        return (self._func is other._func) and (self._args == other._args)
22
23    def __repr__(self):
24        return '<%s %r>' % (self.__class__.__name__, self._args)
25
26
27class Colour(Mutator):
28    '''Overlay a colour onto an image'''
29    def __init__(self, colour, blend=pygame.locals.BLEND_RGBA_MULT):
30        super(Colour, self).__init__(Colour.colour, colour, blend)
31
32    @classmethod
33    def colour(self, image, colour, blend):
34        image = image.copy()
35        overlay = pygame.surface.Surface(image.get_size(),
36                                         pygame.locals.SRCALPHA, image)
37        overlay.fill(colour)
38        image.blit(overlay, (0, 0), None, blend)
39        return image
40
41
42# Identity mutator
43NULL = Mutator(lambda x: x)
44
45# Rotation
46R90 = Mutator(rotate, 90)
47R180 = Mutator(rotate, 180)
48R270 = Mutator(rotate, -90)
49
50FLIP_H = Mutator(flip, True, False)
51FLIP_V = Mutator(flip, False, True)
52
53# Colour
54RED = Colour((255, 0, 0))
55GREEN = Colour((0, 255, 0))
56BLUE = Colour((0, 0, 255))
Note: See TracBrowser for help on using the repository browser.