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 | ||
6a3d0eda | 7 | public class NoiseMode extends AmbientMode { |
0c8d61b6 | 8 | private final Color[] colors; |
eca6fd31 TW |
9 | private SimplexNoise3D noise = new SimplexNoise3D(0); |
10 | ||
6a3d0eda | 11 | public NoiseMode(Color... colors) { |
0c8d61b6 TW |
12 | assert colors.length > 1; |
13 | this.colors = colors; | |
14 | } | |
15 | ||
eca6fd31 TW |
16 | @Override |
17 | protected void updateFrame(LedFrame frame, long time, int count) { | |
18 | for (int i = 0; i < config.leds.getCount(); i++) { | |
19 | double x = frame.xOf(i); | |
20 | double y = frame.yOf(i); | |
21 | 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 |
22 | // frame.setLedColor(i, primary.interpolate(secondary, v)); |
23 | frame.setLedColor(i, getColorAt(v)); | |
eca6fd31 TW |
24 | } |
25 | } | |
0c8d61b6 TW |
26 | |
27 | private Color getColorAt(double value) { // 0.0 to 1.0 | |
28 | double localRange = 1.0 / (colors.length - 1); | |
29 | int index = (int)(value / localRange); | |
30 | double localValue = (value / localRange) - index; | |
31 | if (index == colors.length - 1) { | |
32 | return colors[colors.length - 1]; | |
33 | } else { | |
34 | return colors[index].interpolate(colors[index + 1], localValue); | |
35 | } | |
36 | } | |
eca6fd31 | 37 | } |