| 1 | package kaka.cakelight.mode; |
| 2 | |
| 3 | import kaka.cakelight.*; |
| 4 | import kaka.cakelight.util.SimplexNoise3D; |
| 5 | |
| 6 | public class TwoColorNoiseMode extends AmbientMode { |
| 7 | private final Color primary, secondary; |
| 8 | private SimplexNoise3D noise = new SimplexNoise3D(0); |
| 9 | |
| 10 | public static Console.Command getCommand() { |
| 11 | return new Console.Command() { |
| 12 | public String[] getNames() { |
| 13 | return new String[] {"n", "noise"}; |
| 14 | } |
| 15 | |
| 16 | public void activate(CakeLight cakelight, Configuration config, String[] args) { |
| 17 | if (args.length == 3) { // cmd + col1 + col2 |
| 18 | cakelight.setMode(new TwoColorNoiseMode( |
| 19 | parseColor(args[1]), |
| 20 | parseColor(args[2]) |
| 21 | )); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | private Color parseColor(String s) { |
| 26 | switch (s.toLowerCase()) { |
| 27 | case "r": return Color.rgb(255, 0, 0); |
| 28 | case "g": return Color.rgb(0, 255, 0); |
| 29 | case "b": return Color.rgb(0, 0, 255); |
| 30 | default: // assume hexadecimal |
| 31 | if (s.length() == 3) { |
| 32 | return Color.rgb( |
| 33 | Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16), |
| 34 | Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16), |
| 35 | Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16) |
| 36 | ); |
| 37 | } else if (s.length() == 6) { |
| 38 | return Color.rgb( |
| 39 | Integer.parseInt(s.substring(0, 2), 16), |
| 40 | Integer.parseInt(s.substring(2, 4), 16), |
| 41 | Integer.parseInt(s.substring(4, 6), 16) |
| 42 | ); |
| 43 | } |
| 44 | } |
| 45 | return Color.BLACK; |
| 46 | } |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | public TwoColorNoiseMode(Color primary, Color secondary) { |
| 51 | this.primary = primary; |
| 52 | this.secondary = secondary; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | protected void updateFrame(LedFrame frame, long time, int count) { |
| 57 | for (int i = 0; i < config.leds.getCount(); i++) { |
| 58 | double x = frame.xOf(i); |
| 59 | double y = frame.yOf(i); |
| 60 | double v = Math.pow(Math.min(1, Math.max(0, noise.getr(0.0, 1.0, 1, x, y, time / 7000.0))), 1.5); |
| 61 | frame.setLedColor(i, primary.interpolate(secondary, v)); |
| 62 | } |
| 63 | } |
| 64 | } |