Skip to content

Instantly share code, notes, and snippets.

@joshskeen
Last active September 17, 2020 05:05
Show Gist options
  • Save joshskeen/0bc7ccd32062db34049142de3aae824f to your computer and use it in GitHub Desktop.
Save joshskeen/0bc7ccd32062db34049142de3aae824f to your computer and use it in GitHub Desktop.
hue-interpolate.rb
require 'date'
# the keyframe colors, specified in rgb for certain hours of the day (0 - 24. 0 & 24 values should be the same.)
$keyframes = {
0 => [54, 46, 44],
4 => [204, 59, 22],
12 => [22, 31, 14],
24 => [54, 46, 44]
}
# the size of the step to use for the interpolation.
$num_seconds_per_step = 3
# interpolate function. define the interpolation between a given start value and an end value
def interpolate(start, _end, step, last_step)
return (_end - start) * step / last_step + start
end
# calculate the color values for the values between the keyframes, using num_seconds_per_step
def calculate
# a "sliding window" of values for each keyframe, 2 elements at a time.
out = {}
keyframes.keys.each_cons(2) do | x |
time_start = x.first
time_end = x.last
start_color = keyframes[x.first]
end_color = keyframes[x.last]
diff = time_end - time_start
last_step = (diff * 60 * 60)/ $num_seconds_per_step
for i in 0..last_step
r = interpolate(start_color[0], end_color[0], i, last_step)
g = interpolate(start_color[1], end_color[1], i, last_step)
b = interpolate(start_color[2], end_color[2], i, last_step)
out[(time_start * 60 * 60) + (i * $num_seconds_per_step)] = [r,g,b]
end
end
return out
end
# this function is called when the lights should be updated with a new value.
# wire up the lights!!
def update_lights(color, current_second)
puts "--> update lights, color #{color}, second: #{current_second}"
puts "--> TODO: hook up lights"
end
$out = calculate
last_color = nil
# continuously check the time every second. check the color value for the current second.
# if it changed, updated the lights.
while true do
current_second = Time.now.to_i - Date.today.to_time.to_i
color = $out[current_second]
if !color.nil?
if last_color != color
#TODO: update the hue lights here.
update_lights(color, current_second)
last_color = color
else
puts "no color change at #{current_second} (color #{color})"
end
end
sleep(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment