From: Tomas Wenström Date: Sun, 22 Nov 2020 18:48:58 +0000 (+0100) Subject: Merge branch 'master' of ssh://dolda2000.com/srv/git/r/kaka/cakelight X-Git-Url: http://www.dolda2000.com/gitweb/?a=commitdiff_plain;h=8d0b33bf3dbf21aab3f94ac48db8da4fcee5d0cc;hp=eba8feca754a0875f30b8284dab67c8c01f85ef6;p=kaka%2Fcakelight.git Merge branch 'master' of ssh://dolda2000.com/srv/git/r/kaka/cakelight --- diff --git a/src/kaka/cakelight/Commands.java b/src/kaka/cakelight/Commands.java index 9a2be3b..cbd9e38 100644 --- a/src/kaka/cakelight/Commands.java +++ b/src/kaka/cakelight/Commands.java @@ -3,6 +3,7 @@ package kaka.cakelight; import kaka.cakelight.mode.*; import java.util.function.BiFunction; +import java.util.stream.Stream; class Commands { private static Console.Command command(String[] names, BiFunction activate) { @@ -156,11 +157,13 @@ class Commands { static Console.Command twoColorNoiseMode() { return modeCommand(new String[] {"n", "noise"}, (console, args) -> { - if (args.length == 2) { - console.out("setting two-color noise mode"); - return new TwoColorNoiseMode( - console.parseColor(args[0]), - console.parseColor(args[1]) + if (args.length > 1) { + console.out("setting multi-color noise mode"); + return new TwoColorNoiseMode(Stream.of(args) + .map(arg -> console.parseColor(arg)) + .toArray(Color[]::new) +// console.parseColor(args[0]), +// console.parseColor(args[1]) ); } return null; diff --git a/src/kaka/cakelight/mode/TwoColorNoiseMode.java b/src/kaka/cakelight/mode/TwoColorNoiseMode.java index 7e39705..6e8de55 100644 --- a/src/kaka/cakelight/mode/TwoColorNoiseMode.java +++ b/src/kaka/cakelight/mode/TwoColorNoiseMode.java @@ -5,12 +5,19 @@ import kaka.cakelight.LedFrame; import kaka.cakelight.util.SimplexNoise3D; public class TwoColorNoiseMode extends AmbientMode { - private final Color primary, secondary; + // private final Color primary, secondary; + private final Color[] colors; private SimplexNoise3D noise = new SimplexNoise3D(0); + public TwoColorNoiseMode(Color... colors) { + assert colors.length > 1; + this.colors = colors; + } + public TwoColorNoiseMode(Color primary, Color secondary) { - this.primary = primary; - this.secondary = secondary; + this(new Color[] {primary, secondary}); +// this.primary = primary; +// this.secondary = secondary; } @Override @@ -19,7 +26,19 @@ public class TwoColorNoiseMode extends AmbientMode { double x = frame.xOf(i); double y = frame.yOf(i); double v = Math.pow(Math.min(1, Math.max(0, noise.getr(0.0, 1.0, 1, x, y, time / 7000.0))), 1.5); - frame.setLedColor(i, primary.interpolate(secondary, v)); + // frame.setLedColor(i, primary.interpolate(secondary, v)); + frame.setLedColor(i, getColorAt(v)); } } + + private Color getColorAt(double value) { // 0.0 to 1.0 + double localRange = 1.0 / (colors.length - 1); + int index = (int)(value / localRange); + double localValue = (value / localRange) - index; + if (index == colors.length - 1) { + return colors[colors.length - 1]; + } else { + return colors[index].interpolate(colors[index + 1], localValue); + } + } }