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