From 8e169a82bd6779561eb8d6b030a86c6a34f4e619 Mon Sep 17 00:00:00 2001 From: Markus Schmidl Date: Tue, 21 Feb 2017 20:42:07 +0100 Subject: [PATCH] added class for fading between colors --- animations.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 animations.py diff --git a/animations.py b/animations.py new file mode 100644 index 0000000..eaaccef --- /dev/null +++ b/animations.py @@ -0,0 +1,91 @@ +from beleuchtung import Beleuchtung +import random +import time + +class NoSegmentExists(Exception): + pass + +class ColorsMustBeMultipleOfThreeAndMustExist(Exception): + pass + +class Fade(): + def __init__(self, start, end, step_num): + self.fade_step = (end - start) / step_num + self.current = start + + def next(self): + self.current += self.fade_step + return round(self.current) + +class SolidColorFadingBetweenFenster(Beleuchtung): + def __init__(self, segments, colors, steps, addr): + if len(segments) == 0: + raise NoSegmentExists + if len(colors) % 3 != 0 or len(colors) == 0: + raise ColorsMustBeMultipleOfThreeAndMustExist + Beleuchtung.__init__(self, segments, addr) + self.colors = colors + self.max_step = steps + self.data = self.generate_new_colors() + self.fader = [] + + def __del__(self): + for i in self.fader: + del i + + def to_hex(self, obj, offset): + return "%02x%02x%02x" % (obj[offset+1], obj[offset], obj[offset+2]) + + def __iter__(self): + self.step = 0 + self.fade_to = self.generate_new_colors() + self.fader = [Fade(self.data[i], self.fade_to[i], self.max_step) for i in range(len(self.data))] + j = 0 + for i in range(self.get_num_of_segments()): + print("%d\t#%s\t#%s" % (i, self.to_hex(self.data, j), self.to_hex(self.fade_to, j))) + j += self.segments[i] * 3 + return self + + def __next__(self): + self.step += 1 + if self.step > self.max_step: + for i in self.fader: + del i + raise StopIteration + self.data = [] + for i in self.fader: + self.data.append(i.next()) + return self.send + + def get_num_of_colors(self): + return len(self.colors) / 3 + + def get_rand_color(self): + i = random.randint(0, self.get_num_of_colors() - 1) * 3 + return self.colors[i:i+3] + + def generate_new_colors(self): + colors = [] + for i in range(self.get_num_of_segments()): + colors += self.get_rand_color() * self.segments[i] + return colors + +if __name__ == "__main__": + ambiant_colors = [ + 0,0xff,0xfb, + 0xff,0,0xff, + 0,0xcd,0xeb, + 0xfe,0xa0,0xd0, + 0xce,0xfa,0x2d, + 0x90,0x3c,0xee + ] + + x = SolidColorFadingBetweenFenster([57, 59], ambiant_colors, 1500, [("127.0.0.1", 49152),("172.23.92.15", 49152)]) + + try: + while True: + for i in x: + i() + time.sleep(1.0/50) + except KeyboardInterrupt: + del x