Bugfix - seconds to milliseconds
[kaka/cakelight.git] / src / kaka / cakelight / mode / SunriseMode.java
1 package kaka.cakelight.mode;
2
3 import kaka.cakelight.Color;
4 import kaka.cakelight.LedFrame;
5
6 public class SunriseMode extends AmbientMode {
7     private int durationSeconds;
8
9     public SunriseMode(int durationSeconds) {
10         this.durationSeconds = durationSeconds;
11     }
12
13     @Override
14     protected void updateFrame(LedFrame frame, long time, int count) {
15         if (time > durationSeconds * 1000) {
16             frame.fillColor(Color.rgb(255, 255, 255));
17             return;
18         }
19         double progress = clamp(time / (durationSeconds * 1000.0));
20         double elevation = 2.0 - progress;
21         double radius = progress * 2.12;
22         for (int i = 0; i < config.leds.getCount(); i++) {
23             double x = frame.xOf(i);
24             double y = frame.yOf(i);
25             double distance = distanceFromSun(x, y, elevation);
26             double r = clamp(1.0 - distance + radius);
27             double g = clamp(0.5 - distance + radius);
28             double b = clamp(0.0 - distance + radius);
29             frame.setLedColor(i, Color.rgb(r, g, b));
30         }
31     }
32
33     private double clamp(double value) {
34         return Math.min(Math.max(value, 0), 1.0);
35     }
36
37     private double distanceFromSun(double x, double y, double elevation) {
38         return Math.sqrt(Math.pow(0.5 - x, 2) + Math.pow(elevation - y, 2));
39     }
40 }