1 """ Image transformations for use with the image loader. """
5 import pygame.locals as pgl
7 from .constants import COLOURS
10 class Transform(object):
14 def __init__(self, **kw):
16 setattr(self, k, kw.pop(k))
18 self._repr = "%s: <%s>" % (
19 self.__class__.__name__,
20 ", ".join("%s=%s" % (k, getattr(self, k)) for k in self.ARGS))
27 return hash(self._repr)
29 def __eq__(self, other):
30 if not isinstance(other, Transform):
32 return self._repr == other._repr
34 def apply(self, surface):
35 raise NotImplementedError("Transforms should implement .apply")
38 class NullTransform(Transform):
41 def apply(self, surface):
45 class Overlay(Transform):
46 """ Apply a colour overlay. """
50 def apply(self, surface):
51 over = pygame.surface.Surface(surface.get_size())
52 over = over.convert_alpha()
53 over.fill(self.colour)
54 surface.blit(over, (0, 0), None)
58 class Multiply(Transform):
59 """ Apply a colour by multiplying. """
63 def apply(self, surface):
64 mult = pygame.surface.Surface(surface.get_size())
65 mult = mult.convert_alpha()
66 mult.fill(self.colour)
67 surface.blit(mult, (0, 0), None, pgl.BLEND_RGBA_MULT)
71 class Alpha(Transform):
72 """ Make translucent. """
76 def apply(self, surface):
77 surface.fill((255, 255, 255, self.alpha), None, pgl.BLEND_RGBA_MULT)
81 class ColourWedges(Transform):
82 """ Apply colours as wedges. """
86 def apply(self, surface):
87 size = surface.get_width()
88 assert size in (16, 32, 48, 64)
89 fitting_colours = [COLOURS[c] for c in self.colours]
90 ncolour = len(fitting_colours)
92 print "Multicoloured light should not have more than 3 colours"
96 multiply = Multiply(colour=fitting_colours[0])
97 return multiply.apply(surface)
99 colour_mult_image = pygame.surface.Surface((size, size))
100 from .loader import loader
101 for i in range(ncolour):
102 sector = loader.load_image(
103 str(size), "light_mask_%d_%d.png" % (ncolour, i + 1),
104 transform=Multiply(colour=fitting_colours[i]))
105 colour_mult_image.blit(sector, (0, 0), None, 0)
107 surface.blit(colour_mult_image, (0, 0), None, pgl.BLEND_RGBA_MULT)