| 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 NoiseMode extends AmbientMode { |
| 8 | private final Color[] colors; |
| 9 | private SimplexNoise3D noise = new SimplexNoise3D(0); |
| 10 | |
| 11 | public NoiseMode(Color... colors) { |
| 12 | assert colors.length > 1; |
| 13 | this.colors = colors; |
| 14 | } |
| 15 | |
| 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); |
| 22 | // frame.setLedColor(i, primary.interpolate(secondary, v)); |
| 23 | frame.setLedColor(i, getColorAt(v)); |
| 24 | } |
| 25 | } |
| 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 | } |
| 37 | } |