X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=tabakrolletjie%2Ftransforms.py;h=50cb1d1de42287b977ba84e1d5a4b8420956b62f;hb=e544ee168243127f9d6b6ef4379775dacc3e1079;hp=70ff036160618b51c2c5cd3651310172a23f6a36;hpb=618aba4ccd2f421ad022c31849d87dd7ef00121c;p=tabakrolletjie.git diff --git a/tabakrolletjie/transforms.py b/tabakrolletjie/transforms.py index 70ff036..50cb1d1 100644 --- a/tabakrolletjie/transforms.py +++ b/tabakrolletjie/transforms.py @@ -2,6 +2,8 @@ import pygame.surface +import pygame.locals as pgl + class Transform(object): @@ -11,11 +13,22 @@ class Transform(object): for k in self.ARGS: setattr(self, k, kw.pop(k)) assert not kw - self.hash = "%s: <%s>" % ( + self._repr = "%s: <%s>" % ( self.__class__.__name__, ", ".join("%s=%s" % (k, getattr(self, k)) for k in self.ARGS)) return + def __repr__(self): + return self._repr + + def __hash__(self): + return hash(self._repr) + + def __eq__(self, other): + if not isinstance(other, Transform): + return NotImplemented + return self._repr == other._repr + def apply(self, surface): raise NotImplementedError("Transforms should implement .apply") @@ -38,3 +51,36 @@ class Overlay(Transform): over.fill(self.colour) surface.blit(over, (0, 0), None) return surface + + +class Multiply(Transform): + """ Apply a colour by multiplying. """ + + ARGS = ["colour"] + + def apply(self, surface): + mult = pygame.surface.Surface(surface.get_size()) + mult = mult.convert_alpha() + mult.fill(self.colour) + surface.blit(mult, (0, 0), None, pgl.BLEND_RGBA_MULT) + return surface + + +class MultiplyImage(Transform): + """ Apply a colour by multiplying. """ + + ARGS = ["image"] + + def apply(self, surface): + surface.blit(self.image, (0, 0), None, pgl.BLEND_RGBA_MULT) + return surface + + +class Alpha(Transform): + """ Make translucent. """ + + ARGS = ["alpha"] + + def apply(self, surface): + surface.fill((255, 255, 255, self.alpha), None, pgl.BLEND_RGBA_MULT) + return surface