1 package kaka.cakelight.mode;
3 import kaka.cakelight.Color;
4 import kaka.cakelight.Console;
5 import kaka.cakelight.LedFrame;
6 import kaka.cakelight.util.SimplexNoise3D;
8 public class TwoColorNoiseMode extends AmbientMode {
9 private final Color primary, secondary;
10 private SimplexNoise3D noise = new SimplexNoise3D(0);
12 public static Console.Command getCommand() {
13 return new Console.Command() {
14 public String[] getNames() {
15 return new String[] {"n", "noise"};
18 public void activate(Console console, String[] args) {
19 if (args.length == 3) { // cmd + col1 + col2
20 console.getCakelight().setMode(new TwoColorNoiseMode(
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) {
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)
39 } else if (s.length() == 6) {
41 Integer.parseInt(s.substring(0, 2), 16),
42 Integer.parseInt(s.substring(2, 4), 16),
43 Integer.parseInt(s.substring(4, 6), 16)
52 public TwoColorNoiseMode(Color primary, Color secondary) {
53 this.primary = primary;
54 this.secondary = secondary;
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));