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