1 package kaka.cakelight.mode;
3 import kaka.cakelight.Color;
4 import kaka.cakelight.LedFrame;
6 public class SunriseMode extends AmbientMode {
7 private int durationSeconds;
9 public SunriseMode(int durationSeconds) {
10 this.durationSeconds = durationSeconds;
14 protected void updateFrame(LedFrame frame, long time, int count) {
15 double progress = clamp(time / (durationSeconds * 1000.0));
16 double elevation = 2.0 - progress;
17 double radius = progress * 2.12;
18 for (int i = 0; i < config.leds.getCount(); i++) {
19 double x = frame.xOf(i);
20 double y = frame.yOf(i);
21 double distance = distanceFromSun(x, y, elevation);
22 double r = clamp(1.0 - distance + radius);
23 double g = clamp(0.5 - distance + radius);
24 double b = clamp(0.0 - distance + radius);
25 frame.setLedColor(i, Color.rgb(r, g, b));
29 private double clamp(double value) {
30 return Math.min(Math.max(value, 0), 1.0);
33 private double distanceFromSun(double x, double y, double elevation) {
34 return Math.sqrt(Math.pow(0.5 - x, 2) + Math.pow(elevation - y, 2));