+ void handleInput(String input) {
+ String[] splitInput = input.split("\\s+", 2);
+ String name = splitInput[0];
+ String[] args = splitInput.length == 2
+ ? splitInput[1].split("\\s+")
+ : new String[]{};
+
+ Command cmd = commands.get(name);
+ if (cmd != null) {
+ cmd.activate(this, args);
+ } else {
+ out("no command named '" + name + "'");
+ }
+ }
+
+ void out(String text) {
+ System.out.println("(" + text + ")");
+ }
+
+ 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;
+ }
+
+ public interface Command {
+ String[] getNames();
+ void activate(Console console, String[] args);
+ }