Mercurial > nagslang
annotate nagslang/mutators.py @ 652:4a334fb55348
Merge
author | David Sharpe <decoydavid@gmail.com> |
---|---|
date | Sun, 08 Sep 2013 02:10:30 +0200 |
parents | 0185d2b508c8 |
children |
rev | line source |
---|---|
24
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
1 '''Mutations to apply to images''' |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
2 |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
3 import pygame |
364 | 4 from pygame.transform import rotate, flip, scale |
24
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
5 |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
6 |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
7 class Mutator(object): |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
8 def __init__(self, func, *args): |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
9 self._func = func |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
10 self._args = tuple(args) |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
11 |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
12 def __call__(self, image): |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
13 return self._func(image, *self._args) |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
14 |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
15 def __hash__(self): |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
16 return hash((self._func, self._args)) |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
17 |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
18 def __eq__(self, other): |
31
c62ed518e5c8
Fix repor and add type-check to __eq__.
Simon Cross <hodgestar@gmail.com>
parents:
24
diff
changeset
|
19 if not isinstance(other, Mutator): |
c62ed518e5c8
Fix repor and add type-check to __eq__.
Simon Cross <hodgestar@gmail.com>
parents:
24
diff
changeset
|
20 return NotImplemented |
604
0185d2b508c8
Don't leak mutators (leading to leaked images)
Stefano Rivera <stefano@rivera.za.net>
parents:
555
diff
changeset
|
21 return (self._func == other._func) and (self._args == other._args) |
24
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
22 |
31
c62ed518e5c8
Fix repor and add type-check to __eq__.
Simon Cross <hodgestar@gmail.com>
parents:
24
diff
changeset
|
23 def __repr__(self): |
24
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
24 return '<%s %r>' % (self.__class__.__name__, self._args) |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
25 |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
26 |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
27 class Colour(Mutator): |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
28 '''Overlay a colour onto an image''' |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
29 def __init__(self, colour, blend=pygame.locals.BLEND_RGBA_MULT): |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
30 super(Colour, self).__init__(Colour.colour, colour, blend) |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
31 |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
32 @classmethod |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
33 def colour(self, image, colour, blend): |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
34 image = image.copy() |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
35 overlay = pygame.surface.Surface(image.get_size(), |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
36 pygame.locals.SRCALPHA, image) |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
37 overlay.fill(colour) |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
38 image.blit(overlay, (0, 0), None, blend) |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
39 return image |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
40 |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
41 |
364 | 42 class ImageOverlay(Mutator): |
43 '''Overlay another image onto an image''' | |
44 def __init__(self, image, offset=(0, 0), halfsize=True, blend=0): | |
45 super(ImageOverlay, self).__init__( | |
46 ImageOverlay.overlay, image, offset, halfsize, blend) | |
47 | |
48 @classmethod | |
49 def overlay(self, image, overlay, offset, halfsize, blend): | |
50 image = image.copy() | |
51 if halfsize: | |
52 new_size = (overlay.get_width() / 2, overlay.get_height() / 2) | |
53 overlay = scale(overlay, new_size) | |
54 offset_x = image.get_width() / 2 - overlay.get_width() / 2 + offset[0] | |
55 offset_y = image.get_width() / 2 - overlay.get_width() / 2 + offset[1] | |
56 image.blit(overlay, (offset_x, offset_y), None, blend) | |
57 return image | |
58 | |
59 | |
555 | 60 class ImageCentre(Mutator): |
61 def __init__(self, size): | |
62 super(ImageCentre, self).__init__(ImageCentre.centre, size) | |
63 | |
64 @classmethod | |
65 def centre(cls, image, size): | |
66 if image.get_size() == size: | |
67 return image | |
68 surf = pygame.surface.Surface(size, pygame.locals.SRCALPHA, image) | |
69 surf.blit(image, ((size[0] - image.get_width()) / 2, | |
70 (size[1] - image.get_height()) / 2)) | |
71 return surf | |
72 | |
73 | |
519 | 74 def rotator(angle): |
75 return Mutator(rotate, angle) | |
76 | |
77 | |
555 | 78 def scaler(size): |
79 return Mutator(scale, size) | |
80 | |
81 | |
24
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
82 # Identity mutator |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
83 NULL = Mutator(lambda x: x) |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
84 |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
85 # Rotation |
519 | 86 R90 = rotator(90) |
87 R180 = rotator(180) | |
88 R270 = rotator(-90) | |
24
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
89 |
56 | 90 FLIP_H = Mutator(flip, True, False) |
91 FLIP_V = Mutator(flip, False, True) | |
92 | |
24
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
93 # Colour |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
94 RED = Colour((255, 0, 0)) |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
95 GREEN = Colour((0, 255, 0)) |
50babb330261
Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
96 BLUE = Colour((0, 0, 255)) |