From: Simon Cross Date: Fri, 9 Sep 2016 19:09:20 +0000 (+0200) Subject: Add support for transforming images when loading them (pt II). X-Git-Tag: tabakrolletjie-v1.0.0~161 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=86f39a5a1b6e4a433cbbf61b0088239779a21321;p=tabakrolletjie.git Add support for transforming images when loading them (pt II). --- diff --git a/tabakrolletjie/transforms.py b/tabakrolletjie/transforms.py new file mode 100644 index 0000000..70ff036 --- /dev/null +++ b/tabakrolletjie/transforms.py @@ -0,0 +1,40 @@ +""" Image transformations for use with the image loader. """ + +import pygame.surface + + +class Transform(object): + + ARGS = [] + + def __init__(self, **kw): + for k in self.ARGS: + setattr(self, k, kw.pop(k)) + assert not kw + self.hash = "%s: <%s>" % ( + self.__class__.__name__, + ", ".join("%s=%s" % (k, getattr(self, k)) for k in self.ARGS)) + return + + def apply(self, surface): + raise NotImplementedError("Transforms should implement .apply") + + +class NullTransform(Transform): + """ Do nothing. """ + + def apply(self, surface): + return surface + + +class Overlay(Transform): + """ Apply a colour overlay. """ + + ARGS = ["colour"] + + def apply(self, surface): + over = pygame.surface.Surface(surface.get_size()) + over = over.convert_alpha() + over.fill(self.colour) + surface.blit(over, (0, 0), None) + return surface