summaryrefslogtreecommitdiffstats
path: root/sunrise.py
diff options
context:
space:
mode:
authorAaron Perley <aaron.perley@gmail.com>2016-10-13 23:26:30 -0400
committerAaron Perley <aaron.perley@gmail.com>2016-10-13 23:26:30 -0400
commit86761b1f1075953a08944d178b05c0b3c259d243 (patch)
tree97a72c0b54bf4c3e00d78ea5114a2a3a34573641 /sunrise.py
parent26ed0bd9400a888b2603db734d47e9ea64c6c3d7 (diff)
downloadbridge-86761b1f1075953a08944d178b05c0b3c259d243.tar.zst
Modify sunrise show
Diffstat (limited to 'sunrise.py')
-rw-r--r--sunrise.py124
1 files changed, 88 insertions, 36 deletions
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
+ 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()
- 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()
+ #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)