diff --git a/generators/unrath.py b/generators/unrath.py new file mode 100755 index 0000000..9f38dfb --- /dev/null +++ b/generators/unrath.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python3 + +import sys +import random +import argparse +import blup.frame +import blup.output +import blup.animation +import blup.writebml +import colorsys + + +WIDTH = 22 +HEIGHT = 16 +DEPTH = 256 + + +def convert_color(c): + return list(map(lambda x: int(round(x*(DEPTH-1))), c)) + + +def get_random_color(): + if random.random() < 0.5: + s = 1 + v = random.random() + else: + s = random.random() + v = 1 + return colorsys.hsv_to_rgb(random.random(), s, v) + + +def cap(x): + return min(1, max(0, x)) + + +def cyc(x): + while x < 0: + x += 1 + while x > 1: + x -= 1 + return x + + +class Unrath: + def __init__(self): + self.shape = set([(0, 0)]) + shapesize = random.randint(2, 5) + while len(self.shape) < shapesize: + p = random.sample(self.shape, 1)[0] + self.shape.add((p[0]+random.randint(0, 1), p[1]+random.randint(0, 1))) + self.shapewidth = max(map(lambda p: p[0], self.shape)) + shapeheight = max(map(lambda p: p[1], self.shape)) + + y = random.randint(0, HEIGHT-shapeheight) + if random.random() > 0.5: + d = 1 + x = -self.shapewidth + else: + d = -1 + x = WIDTH + self.shapewidth + self.pos = (x, y) + self.finished = False + self.speed = d * random.randint(2, WIDTH/2) + self.color = get_random_color() + + def update(self, dt): + if (self.pos[0] + self.shapewidth < 0 and self.speed < 0) or \ + (self.pos[0] > WIDTH and self.speed > 0): + self.finished = True + else: + self.pos = (self.pos[0] + dt*self.speed, self.pos[1]) + + def draw(self, frame): + for p in self.shape: + try: + frame.setPixel( + int(self.pos[0] + p[0]), + int(self.pos[1] + p[1]), + convert_color(self.color) + ) + except ValueError: + pass + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Generate Unrath animations') + parser.add_argument('-d', '--delay', type=int, default=50) + parser.add_argument('-t', '--time', type=int, default=15) + parser.add_argument('-n', '--num-unraethe', type=int, default=5) + parser.add_argument('output_file') + args = parser.parse_args() + + dim = blup.frame.FrameDimension(WIDTH, HEIGHT, DEPTH, 3) + anim = blup.animation.Animation(dim) + anim.tags['description'] = ' '.join(sys.argv) + + t = 0 + unraethe = [] + while t < args.time: + t += args.delay / 1000 + frame = blup.animation.AnimationFrame(dim, args.delay) + + unraethe = list(filter(lambda u: not u.finished, unraethe)) + if len(unraethe) < args.num_unraethe: + unraethe.append(Unrath()) + + for u in unraethe: + u.update(args.delay / 1000) + u.draw(frame) + + anim.addFrame(frame) + + blup.writebml.writeBml(anim, args.output_file)