diff options
| author | Raymond Hogenson <rayhogenson@openmailbox.org> | 2016-10-15 10:06:20 -0400 |
|---|---|---|
| committer | Raymond Hogenson <rayhogenson@openmailbox.org> | 2016-10-15 10:06:20 -0400 |
| commit | c4999bbbfdbca65986e876ac78863041e85cfbe4 (patch) | |
| tree | b3f300c16fb369061ea7139c3e3684e7eac8a924 /sunrise.py | |
| parent | Merge branch 'master' of github.com:rayhogenson/bridge (diff) | |
| parent | Add img-disable css and usage (diff) | |
| download | bridge-c4999bbbfdbca65986e876ac78863041e85cfbe4.tar.zst | |
Merge branch 'master' of github.com:rayhogenson/bridge
Diffstat (limited to 'sunrise.py')
| -rw-r--r-- | sunrise.py | 138 |
1 files changed, 92 insertions, 46 deletions
@@ -9,38 +9,46 @@ from show import Show class Sun(object): """Object displayed on the bridge, probably a fish.""" - def __init__(self, x, y, width, height, c, step): + def __init__(self): """Create fish with default parameters.""" - self.x = x - self.y = y - self.x = 75 - self.y = -2 - self.width = width - self.width = 75 - self.height = height + self.x = 25 + self.y = 0 - 1 + self.width = 150 self.height = 2 - self.color = c - self.color = color.RGB(1, 0.5, 0) - self.speed = step - self.step = step - self.wait = 0 + self.base_color = color.HSV(27/360, 1, 0.5) + self.end_color = color.HSV(53/360, 1, 1) + self.color = self.base_color.to_RGB() def update(self): """Swim gently to the other side of the bridge.""" - #self.x += self.step - #if self.x > 200: - # self.x = 0 - #self.y = 0.25 + 0.25 * math.sin(math.pi * self.x / 10) - #theta = random.random() * math.pi / 2 - math.pi / 4 - #self.y += 0.01 * math.sin(theta) - #print(self.y) self.y += 0.005 - if self.y >= 1: - self.y = 1 - self.wait += 1 - if self.wait >= 200: - self.wait = 0 - self.y = -2 + if self.y + self.height >= 3: + self.y = 3 - self.height + elif self.y + self.height > 1: + self.width += 0.2 + self.x -= 0.1 + color_range = (self.end_color - self.base_color).unit() + self.color = (self.y + self.height - 1) / 3 * color_range + self.base_color + self.color = self.color.to_RGB() + + #print(self.y) + + #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): @@ -53,18 +61,36 @@ class Light(object): self.step = 0 self.x = x self.y = y + self.fade_step = 0 - def update(self, fish): + def update(self, sky_brightness, sun): """Change color depending on where the fish is.""" - if self.y == 1: - new_color = color.RGB(0.5, 0.5, 0.5) + if self.y == 0: + if abs(self.transitioning_to - self.color) <= self.step: + self.transitioning_to = pick_ocean_HSV(sky_brightness).to_RGB() + self.step = (random_change() + * abs(self.color - self.transitioning_to)) + self.color = transition( + self.color, self.transitioning_to, self.step + ) + self.light.setRGBRaw( + *map(lambda x: clamp(0, x, 1), self.color.components) + ) else: - new_color = color.RGB(0.5, 0.5, 0) - coverage = compute_coverage(fish, self.x, self.y) - ccolor = new_color * (1 - coverage) + fish.color * coverage - self.light.setRGBRaw( - *map(lambda x: clamp(0, x, 1), ccolor.components) - ) + sky_color = color.HSV(250/360, 0.25, sky_brightness).to_RGB() + coverage = compute_coverage(sun, self.x, self.y) + self.color = coverage * sun.color + (1 - coverage) * sky_color + self.light.setRGBRaw( + *map(lambda x: clamp(0, x, 1), self.color.components) + ) + + def fade_to_black(self, steps): + hue = self.color.to_HSV().h() + sat = self.color.to_HSV().s() + value = self.color.to_HSV().v() + new_color = color.HSV(hue, sat, value - value / steps * self.fade_step).to_RGB() + self.fade_step += 1 + self.light.setRGBRaw(*map(lambda x: clamp(0, x, 1), new_color.components)) class SunriseShow(Show): @@ -72,13 +98,30 @@ class SunriseShow(Show): 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(0, 0, 23, 1.5, color.black, 1) + self.sun = Sun() + self.sky_brightness = 0 + self.tick_count = 0 def update(self): - self.sun.update() - for light in self.lights: - light.update(self.sun) + if self.tick_count > 1000: + fade_time = 3 # sec + if self.tick_count > 1000 + fade_time * FRAME_RATE: + self.stop() + return + for light in self.lights: + light.fade_to_black(fade_time * FRAME_RATE) + else: + if self.sky_brightness >= 1: + self.sun.update() + for light in self.lights: + light.update(self.sky_brightness, self.sun) + + if self.sky_brightness < 1: + self.sky_brightness += 0.05 / FRAME_RATE + + self.tick_count += 1 + print self.tick_count def compute_coverage(fish, x, y): @@ -95,12 +138,15 @@ def compute_coverage(fish, x, y): return coverage * scale -def pick_HSV(): +def pick_ocean_HSV(sky_brightness): """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) + min_hue = 236 / 360 + max_hue = 250 / 360 + min_sat = 0.25 + max_sat = 0.6 + hue = random.random() * (max_hue - min_hue) + min_hue + saturation = random.random() * (max_sat - min_sat) + min_sat + return color.HSV(hue, saturation, sky_brightness) def transition(start, end, step): @@ -115,8 +161,8 @@ def transition(start, end, step): 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) + min_time = int(2 * FRAME_RATE) + max_time = int(6 * FRAME_RATE) return 1 / random.randint(min_time, max_time) |
