6e8de55480e07d4b40cf93b88756a1b2f697cd2e
[kaka/cakelight.git] / src / kaka / cakelight / mode / TwoColorNoiseMode.java
1 package kaka.cakelight.mode;
2
3 import kaka.cakelight.Color;
4 import kaka.cakelight.LedFrame;
5 import kaka.cakelight.util.SimplexNoise3D;
6
7 public class TwoColorNoiseMode extends AmbientMode {
8     //    private final Color primary, secondary;
9     private final Color[] colors;
10     private SimplexNoise3D noise = new SimplexNoise3D(0);
11
12     public TwoColorNoiseMode(Color... colors) {
13         assert colors.length > 1;
14         this.colors = colors;
15     }
16
17     public TwoColorNoiseMode(Color primary, Color secondary) {
18         this(new Color[] {primary, secondary});
19 //        this.primary = primary;
20 //        this.secondary = secondary;
21     }
22
23     @Override
24     protected void updateFrame(LedFrame frame, long time, int count) {
25         for (int i = 0; i < config.leds.getCount(); i++) {
26             double x = frame.xOf(i);
27             double y = frame.yOf(i);
28             double v = Math.pow(Math.min(1, Math.max(0, noise.getr(0.0, 1.0, 1, x, y, time / 7000.0))), 1.5);
29             //            frame.setLedColor(i, primary.interpolate(secondary, v));
30             frame.setLedColor(i, getColorAt(v));
31         }
32     }
33
34     private Color getColorAt(double value) { // 0.0 to 1.0
35         double localRange = 1.0 / (colors.length - 1);
36         int index = (int)(value / localRange);
37         double localValue = (value / localRange) - index;
38         if (index == colors.length - 1) {
39             return colors[colors.length - 1];
40         } else {
41             return colors[index].interpolate(colors[index + 1], localValue);
42         }
43     }
44 }