diff options
| -rwxr-xr-x[-rw-r--r--] | bridge.py | 6 | ||||
| -rw-r--r-- | color.py | 3 | ||||
| -rw-r--r-- | fish_render.py | 28 |
3 files changed, 27 insertions, 10 deletions
diff --git a/bridge.py b/bridge.py index 7271db8..87b339c 100644..100755 --- a/bridge.py +++ b/bridge.py @@ -17,8 +17,10 @@ def get_all_lights(rig): """Return a list of all panels.""" return [ [ - rig.select('$side={y}[$sequence={x}]'.format({'y': y, 'x': x})) - for y in ('top', 'bot') + fish_render.Light( + x, int(y == 'top'), + rig.select('$side={y}[$sequence={x}]'.format(y=y, x=x)) + ) for y in ('top', 'bot') ] for x in xrange(1, 200) ] @@ -37,6 +37,9 @@ class Vector(object): """Return self * 1 / scalar.""" return self * (1 / scalar) + def __eq__(self, other): + return self.components == other.components + class HSV(Vector): """HSV 3-vector.""" diff --git a/fish_render.py b/fish_render.py index becd8c1..6d24167 100644 --- a/fish_render.py +++ b/fish_render.py @@ -13,6 +13,7 @@ class Fish(object): self.height = height self.color = color self.speed = step + self.step = step def update(self): """Swim gently to the other side of the bridge.""" @@ -21,12 +22,12 @@ class Fish(object): class Light(object): """Represent a bridge light and its state.""" - def __init__(self, x, y, light, start_color, step): + def __init__(self, x, y, light): """Store all facts about a light.""" self.light = light - self.color = start_color - self.transitioning_to = start_color - self.step = step + self.color = color.black + self.transitioning_to = self.color + self.step = 0 self.x = x self.y = y @@ -40,7 +41,9 @@ class Light(object): ) coverage = compute_coverage(fish, self.x, self.y) new_color = coverage * fish.color + (1 - coverage) * new_background - self.light.setRGBRaw(*new_color.components) + self.light.setRGBRaw( + *map(lambda x: clamp(0, x, 1), new_color.components) + ) class Bridge(object): @@ -55,8 +58,8 @@ class Bridge(object): """Compute fish coverages and push lights to bridge.""" for x, light_col in enumerate(self.lights): for y, light in enumerate(light_col): - fish.update() - light.update(fish) + self.fish.update() + light.update(self.fish) self.rig.updateOnce() @@ -80,7 +83,11 @@ def pick_HSV(): def transition(start, end, step): """Return a vector equal to (end - start) * step + start.""" - return (end - start) / abs(end - start) * step + start + if start == end: + return start + diff_vector = end - start + step_vector = diff_vector / abs(diff_vector) * step + return step_vector + start def random_change(): @@ -88,3 +95,8 @@ def random_change(): min_time = int(MIN_TIME * FRAME_RATE) max_time = int(MAX_TIME * FRAME_RATE) return 1 / random.randint(min_time, max_time) + + +def clamp(low, x, high): + """Return v such that low <= v <= high and |x - v| is minimal.""" + return max(low, min(x, high)) |
