#!/usr/bin/env python3

import numpy as np
import os
import sys
import time
import colorsys

Nx = int(sys.argv[1])
Ny = int(sys.argv[2])
iterations = 2

class Slider:
    def __init__(self, lo, hi, step, pos=0):
        self.lo = lo
        self.hi = hi
        self.stepSize = step
        self.pos = pos
        self.dir = 1
    
    def step(self):
        if self.pos >= self.hi:
            self.dir = -1
        elif self.pos <= self.lo:
            self.dir = 1
        
        self.pos += self.dir * self.stepSize
        return self.pos

x, y = np.meshgrid(
    np.linspace(-2, 2, Nx),
    np.linspace(-2, 2, Ny))

img = np.zeros( [Ny, Nx, 3] )

Sa = Slider(-3, 3, 0.01, np.random.uniform(-1, 1))
Sb = Slider(-3, 3, 0.01, np.random.uniform(-1, 1))
Sc = Slider(0, 1, 0.005, np.random.uniform(-1, 1))

Scolor = Slider(0, 1, 0.001/iterations, 0)

step = 0
while True:
    a = Sa.step()
    b = Sb.step()
    c = Sc.step()

    Sa.pos += b*0.001
    Sb.pos += c*0.001
    Sc.pos += a*0.001

    curve = (np.clip(np.sin(c) + np.sin(np.sin(a*x) + np.cos(b*y)) - np.cos(np.sin(b*x*y) + np.cos(b*x)), 0, 1))

    cr, cg, cb = colorsys.hsv_to_rgb(Scolor.step(), 1, 1)

    img[:,:,0] = curve*cr*255
    img[:,:,1] = curve*cg*255
    img[:,:,2] = curve*cb*255

    step += 1
    if step % iterations == 0:
        out = img.reshape((Nx*Ny*3,)).astype(np.uint8)
        os.write(1, out.tobytes())
        time.sleep(0.01)



