1 package kaka.cakelight.mode;
3 import kaka.cakelight.*;
4 import kaka.cakelight.util.SimplexNoise3D;
6 public class TwoColorNoiseMode extends AmbientMode {
7 private final Color primary, secondary;
8 private SimplexNoise3D noise = new SimplexNoise3D(0);
10 public static Console.Command getCommand() {
11 return new Console.Command() {
12 public String[] getNames() {
13 return new String[] {"n", "noise"};
16 public void activate(CakeLight cakelight, Configuration config, String[] args) {
17 if (args.length == 3) { // cmd + col1 + col2
18 cakelight.setMode(new TwoColorNoiseMode(
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) {
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)
37 } else if (s.length() == 6) {
39 Integer.parseInt(s.substring(0, 2), 16),
40 Integer.parseInt(s.substring(2, 4), 16),
41 Integer.parseInt(s.substring(4, 6), 16)
50 public TwoColorNoiseMode(Color primary, Color secondary) {
51 this.primary = primary;
52 this.secondary = secondary;
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));