1 | '''Mutations to apply to images'''
|
---|
2 |
|
---|
3 | import pygame
|
---|
4 | from pygame.transform import rotate, flip
|
---|
5 |
|
---|
6 |
|
---|
7 | class 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 |
|
---|
27 | class 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
|
---|
43 | NULL = Mutator(lambda x: x)
|
---|
44 |
|
---|
45 | # Rotation
|
---|
46 | R90 = Mutator(rotate, 90)
|
---|
47 | R180 = Mutator(rotate, 180)
|
---|
48 | R270 = Mutator(rotate, -90)
|
---|
49 |
|
---|
50 | FLIP_H = Mutator(flip, True, False)
|
---|
51 | FLIP_V = Mutator(flip, False, True)
|
---|
52 |
|
---|
53 | # Colour
|
---|
54 | RED = Colour((255, 0, 0))
|
---|
55 | GREEN = Colour((0, 255, 0))
|
---|
56 | BLUE = Colour((0, 0, 255))
|
---|