Add a simple script to dynamically cycle colours
This commit is contained in:
parent
7ee61b8ed0
commit
9c9bb872ab
1 changed files with 43 additions and 0 deletions
43
dynamic.py
Normal file
43
dynamic.py
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
#! /bin/python3
|
||||||
|
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
|
from Library.tree import RGBXmasTree
|
||||||
|
|
||||||
|
BRIGHTNESS_MULTIPLIER = 0.15
|
||||||
|
|
||||||
|
class Animation:
|
||||||
|
def __init__(self):
|
||||||
|
self.animation_steps = 50
|
||||||
|
self.progress = [random.randint(0, self.animation_steps) for i in range(25)]
|
||||||
|
self.colours = [(random.random(), random.random(), random.random()) for i in range(25)]
|
||||||
|
self.pixels = [(0, 0, 0) for i in range(25)]
|
||||||
|
|
||||||
|
def next(self):
|
||||||
|
for i in range(len(self.progress)):
|
||||||
|
self.progress[i] = (self.progress[i] + 1) % self.animation_steps
|
||||||
|
if self.progress[i] == 0:
|
||||||
|
self.colours[i] = (random.random(), random.random(), random.random())
|
||||||
|
|
||||||
|
magnitude = math.sin(math.pi * (self.progress[i] / self.animation_steps))
|
||||||
|
magnitude *= BRIGHTNESS_MULTIPLIER
|
||||||
|
colour = self.colours[i]
|
||||||
|
self.pixels[i] = (colour[0] * magnitude, colour[1] * magnitude, colour[2] * magnitude)
|
||||||
|
|
||||||
|
def set_colours(self, tree: RGBXmasTree):
|
||||||
|
for i in range(25):
|
||||||
|
tree[i].color = self.pixels[i]
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
tree = RGBXmasTree()
|
||||||
|
animation = Animation()
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
animation.next()
|
||||||
|
animation.set_colours(tree)
|
||||||
|
time.sleep(0.5)
|
||||||
|
finally:
|
||||||
|
tree.off()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue