"""Rendering classes and helper functions.""" from __future__ import division import color import random import constants from constants import * import math from show import Show class Sun(object): """Object displayed on the bridge, probably a fish.""" def __init__(self): """Create fish with default parameters.""" self.center_x = 140 self.center_y = -5 self.width = 30 self.height = 5 self.wait = 0 def update(self): """Swim gently to the other side of the bridge.""" self.center_y += 0.005 if self.center_y >= 1: self.center_y = 1 self.wait += 1 if self.wait >= 200: self.wait = 0 self.center_y = -5 def color_at(self, x, y): ball_width = 15 if abs(x - self.center_x) < ball_width: return color.RGB(1, 1, 1) distance = abs(self.center_x - x) start_hue = 58 / 360 end_hue = 41 / 360 start_sat = 0 end_sat = 0.92 sat = scale_with_clamp(distance, ball_width, 60, start_sat, end_sat) hue = scale_with_clamp(distance, ball_width, 100, start_hue, end_hue) return color.HSV(hue, sat, 1).to_RGB() def scale_with_clamp(inp, min_inp, max_inp, min_out, max_out): return (clamp(min_inp, inp, max_inp) - min_inp) / (max_inp - min_inp) * (max_out - min_out) + min_out class Light(object): """Represent a bridge light and its state.""" def __init__(self, x, y, light): """Store all facts about a light.""" self.light = light self.color = color.black self.transitioning_to = self.color self.step = 0 self.x = x self.y = y def update(self, sun): """Change color depending on where the fish is.""" self.light.setRGBRaw(*sun.color_at(self.x, self.y).components) class SunriseShow(Show): def init(self): self.lights = [Light(x, y, self.bridge.get_light(x, y)) for y in xrange(self.bridge.HEIGHT) for x in xrange(self.bridge.WIDTH)] self.sun = Sun() def update(self): self.sun.update() for light in self.lights: light.update(self.sun) def compute_coverage(fish, x, y): """Calculate the coverage of a fish over a box.""" middle_of_fish = fish.x + fish.width / 2 middle_of_panel = x + 0.5 diff = abs(middle_of_fish - middle_of_panel) / (fish.width / 2) scale = 1 - diff min_x = min(max(x, fish.x), x + 1) max_x = max(min(x+1, fish.x + fish.width), x) min_y = min(max(y, fish.y), y + 1) max_y = max(min(y+1, fish.y + fish.height), y) coverage = (max_x - min_x) * (max_y - min_y) return coverage * scale def pick_HSV(): """Return a random color in an appropriate interval.""" hue = random.random() * (constants.MAX_HUE - constants.MIN_HUE) + constants.MIN_HUE saturation = random.random() * (MAX_SATURATION - MIN_SATURATION) + MIN_SATURATION value = random.random() * (MAX_VALUE - MIN_VALUE) + MIN_VALUE return color.HSV(hue, saturation, value) def transition(start, end, step): """Return a vector equal to (end - start) * step + start.""" if start == end: return start diff_vector = end - start step_vector = diff_vector / abs(diff_vector) * step result = step_vector + start return result def random_change(): """Return a random amount that a light should change each frame.""" min_time = int(constants.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))