X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fkaka%2Fcakelight%2Fmode%2FSunriseMode.java;fp=src%2Fkaka%2Fcakelight%2Fmode%2FSunriseMode.java;h=896646f39d56153adf9222193afd0394729bcd4c;hb=40a06a9bffb56d41e931038fcfdb8f10fe8ca441;hp=0000000000000000000000000000000000000000;hpb=f1a6a6a5cf7d61c2df185206cb0a5b0e7eceb3c1;p=kaka%2Fcakelight.git diff --git a/src/kaka/cakelight/mode/SunriseMode.java b/src/kaka/cakelight/mode/SunriseMode.java new file mode 100644 index 0000000..896646f --- /dev/null +++ b/src/kaka/cakelight/mode/SunriseMode.java @@ -0,0 +1,36 @@ +package kaka.cakelight.mode; + +import kaka.cakelight.Color; +import kaka.cakelight.LedFrame; + +public class SunriseMode extends AmbientMode { + private int durationSeconds; + + public SunriseMode(int durationSeconds) { + this.durationSeconds = durationSeconds; + } + + @Override + protected void updateFrame(LedFrame frame, long time, int count) { + double progress = clamp(time / (durationSeconds * 1000.0)); + double elevation = 2.0 - progress; + double radius = progress * 2.12; + for (int i = 0; i < config.leds.getCount(); i++) { + double x = frame.xOf(i); + double y = frame.yOf(i); + double distance = distanceFromSun(x, y, elevation); + double r = clamp(1.0 - distance + radius); + double g = clamp(0.5 - distance + radius); + double b = clamp(0.0 - distance + radius); + frame.setLedColor(i, Color.rgb(r, g, b)); + } + } + + private double clamp(double value) { + return Math.min(Math.max(value, 0), 1.0); + } + + private double distanceFromSun(double x, double y, double elevation) { + return Math.sqrt(Math.pow(0.5 - x, 2) + Math.pow(elevation - y, 2)); + } +}