From 86761b1f1075953a08944d178b05c0b3c259d243 Mon Sep 17 00:00:00 2001 From: Aaron Perley Date: Thu, 13 Oct 2016 23:26:30 -0400 Subject: Modify sunrise show --- sunrise.py | 126 +++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 89 insertions(+), 37 deletions(-) (limited to 'sunrise.py') diff --git a/sunrise.py b/sunrise.py index 59818d5..4956342 100644 --- a/sunrise.py +++ b/sunrise.py @@ -11,34 +11,40 @@ 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 + self.x = 25 + self.y = 0 - 1 + self.width = 150 + self.height = 2 + 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.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() + self.y += 0.005 + 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): @@ -55,10 +61,36 @@ class Light(object): self.step = 0 self.x = x self.y = y + self.fade_step = 0 - def update(self, sun): + def update(self, sky_brightness, sun): """Change color depending on where the fish is.""" - self.light.setRGBRaw(*sun.color_at(self.x, self.y).components) + 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: + 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): @@ -67,12 +99,29 @@ class SunriseShow(Show): for y in xrange(self.bridge.HEIGHT) for x in xrange(self.bridge.WIDTH)] 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): @@ -89,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): @@ -109,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) -- cgit v1.3.1