Commit | Line | Data |
---|---|---|
eca6fd31 TW |
1 | package kaka.cakelight.mode; |
2 | ||
fa013e4b | 3 | import kaka.cakelight.Color; |
fa013e4b | 4 | import kaka.cakelight.LedFrame; |
eca6fd31 TW |
5 | import kaka.cakelight.util.SimplexNoise3D; |
6 | ||
7 | public class TwoColorNoiseMode extends AmbientMode { | |
0c8d61b6 TW |
8 | // private final Color primary, secondary; |
9 | private final Color[] colors; | |
eca6fd31 TW |
10 | private SimplexNoise3D noise = new SimplexNoise3D(0); |
11 | ||
0c8d61b6 TW |
12 | public TwoColorNoiseMode(Color... colors) { |
13 | assert colors.length > 1; | |
14 | this.colors = colors; | |
15 | } | |
16 | ||
eca6fd31 | 17 | public TwoColorNoiseMode(Color primary, Color secondary) { |
0c8d61b6 TW |
18 | this(new Color[] {primary, secondary}); |
19 | // this.primary = primary; | |
20 | // this.secondary = secondary; | |
eca6fd31 TW |
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); | |
0c8d61b6 TW |
29 | // frame.setLedColor(i, primary.interpolate(secondary, v)); |
30 | frame.setLedColor(i, getColorAt(v)); | |
eca6fd31 TW |
31 | } |
32 | } | |
0c8d61b6 TW |
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 | } | |
eca6fd31 | 44 | } |