| 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 | 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)); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | private double clamp(double value) { |
| 30 | return Math.min(Math.max(value, 0), 1.0); |
| 31 | } |
| 32 | |
| 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)); |
| 35 | } |
| 36 | } |