From baaaa10bd2e30428f8ca0f04855a24138e70d097 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Mon, 25 Nov 2019 22:08:47 +0100 Subject: [PATCH] Move color parsing method to command --- src/kaka/cakelight/Console.java | 27 ++++++++++++++++++++++++++ src/kaka/cakelight/mode/TwoColorNoiseMode.java | 23 ---------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/kaka/cakelight/Console.java b/src/kaka/cakelight/Console.java index 422b7a3..0b956ae 100644 --- a/src/kaka/cakelight/Console.java +++ b/src/kaka/cakelight/Console.java @@ -83,5 +83,32 @@ public class Console extends Thread { public interface Command { String[] getNames(); void activate(Console console, String[] args); + + default Color parseColor(String s) { + switch (s.toLowerCase()) { + case "r": return Color.rgb(255, 0, 0); + case "g": return Color.rgb(0, 255, 0); + case "b": return Color.rgb(0, 0, 255); + default: // assume hexadecimal + if (s.startsWith("#")) { + s = s.substring(1); + } + if (s.length() == 3) { + return Color.rgb( + Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16), + Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16), + Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16) + ); + } else if (s.length() == 6) { + return Color.rgb( + Integer.parseInt(s.substring(0, 2), 16), + Integer.parseInt(s.substring(2, 4), 16), + Integer.parseInt(s.substring(4, 6), 16) + ); + } + } + System.out.println("Failed to parse color '" + s + "'. Using black instead."); + return Color.BLACK; + } } } diff --git a/src/kaka/cakelight/mode/TwoColorNoiseMode.java b/src/kaka/cakelight/mode/TwoColorNoiseMode.java index 7577f66..1ebb930 100644 --- a/src/kaka/cakelight/mode/TwoColorNoiseMode.java +++ b/src/kaka/cakelight/mode/TwoColorNoiseMode.java @@ -23,29 +23,6 @@ public class TwoColorNoiseMode extends AmbientMode { )); } } - - private Color parseColor(String s) { - switch (s.toLowerCase()) { - case "r": return Color.rgb(255, 0, 0); - case "g": return Color.rgb(0, 255, 0); - case "b": return Color.rgb(0, 0, 255); - default: // assume hexadecimal - if (s.length() == 3) { - return Color.rgb( - Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16), - Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16), - Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16) - ); - } else if (s.length() == 6) { - return Color.rgb( - Integer.parseInt(s.substring(0, 2), 16), - Integer.parseInt(s.substring(2, 4), 16), - Integer.parseInt(s.substring(4, 6), 16) - ); - } - } - return Color.BLACK; - } }; } -- 2.11.0