distraction/main.py

73 lines
1.5 KiB
Python
Raw Normal View History

2020-02-12 12:01:12 +01:00
# import shutil # shutil.get_terminal_size(fallback=(0, 0))
import os
import math
from pixel import Pixel, Screen
WIDTH, HEIGHT = 100, 30
STD_HANDLE = 1 # Would want this to be 0, but it can cause issues
def shad_background(pixel: Pixel, x: int, y: int, time: float):
uv = (x / WIDTH, y / HEIGHT)
h = math.sin(uv[0] * math.pi + uv[1] * 10.0 + time + math.cos(uv[0] * uv[1] + uv[1] * 7.1823) * math.sin(uv[0] * 14.2))
pixel.background_color.set(h, h, h)
def shad_wave(pixel: Pixel, x: int, y: int, time: float): # https://www.shadertoy.com/view/Wd23W3
uv = (x / WIDTH, y / HEIGHT)
def hsv2rgb(h, s, v) -> tuple:
r,g,b = 0,0,0
i = int(h * 6)
f = h * 6 - i
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
o = i % 6
if o == 0:
r,g,b = v,t,p
elif o == 1:
r,g,b = q,v,p
elif o == 2:
r,g,b = p,v,t
elif o == 3:
r,g,b = p,q,v
elif o == 4:
r,g,b = t,p,v
else:
r,g,b = v,p,q
return r, g, b
def step(edge: float, value: float) -> float:
return 1.0 if value > edge else 0.0
def strip(h: float, thickness: float, y: float) -> float:
return step(y + h - thickness, step(1., y + h + thickness))
col = [0.0, 0.0, 0.0]
f = 0.0
while f < math.pi * 2:
a = strip(.5 + math.cos(uv[0] * 2. + time * 3.) * .15,
.005,
uv[1] + math.cos(uv[0] * 3. + time * 2. + f) * .25)
_col = hsv2rgb(f / math.pi / 2., 1., 1.)
for i in range(3):
col[i] += a * _col[i]
f += math.pi / 4.
pixel.text_color.set(*col)
screen = Screen(WIDTH, HEIGHT, STD_HANDLE, [
shad_background
#shad_wave
])
screen.mainloop()