Move color parsing method to command
[kaka/cakelight.git] / src / kaka / cakelight / Console.java
... / ...
CommitLineData
1package kaka.cakelight;
2
3import kaka.cakelight.mode.AmbientMode;
4import kaka.cakelight.mode.SingleColorMode;
5import kaka.cakelight.mode.TwoColorNoiseMode;
6import kaka.cakelight.mode.VideoMode;
7
8import java.io.BufferedReader;
9import java.io.IOException;
10import java.io.InputStreamReader;
11
12public class Console extends Thread {
13 private CakeLight cakelight;
14 private Configuration config;
15 private BufferedReader reader;
16
17 public static void start(CakeLight cakelight, Configuration config) {
18 new Console(cakelight, config).start();
19 }
20
21 private Console(CakeLight cakelight, Configuration config) {
22 this.cakelight = cakelight;
23 this.config = config;
24 reader = new BufferedReader(new InputStreamReader(System.in));
25
26 public CakeLight getCakelight() {
27 return cakelight;
28 }
29
30 public Configuration getConfig() {
31 return config;
32 }
33 }
34
35 @Override
36 public void run() {
37 while (true) {
38 System.out.print("> ");
39 try {
40 String input = reader.readLine();
41 if (input.matches("[0-5]")) {
42 cakelight.setMode(new AmbientMode(new String[] {input}));
43 System.out.println("setting ambient mode to " + input);
44 } else if (input.matches("v|video")) {
45 cakelight.setMode(new VideoMode());
46 } else if (input.matches("(b|brightness)\\s+[0-9]+")) {
47 String[] split = input.split("\\s+");
48 config.leds.brightness = Integer.parseInt(split[1]);
49 System.out.println("setting brightness to " + config.leds.brightness);
50 } else if (input.matches("q|quit")) {
51 cakelight.turnOff();
52 System.out.println("stopping cakelight");
53 break;
54 } else if (input.matches("(c|col|color)(\\s+[0-9]+){3}")) {
55 String[] split = input.split("\\s+");
56 Color c = Color.rgb(
57 Integer.parseInt(split[1]),
58 Integer.parseInt(split[2]),
59 Integer.parseInt(split[3])
60 );
61 cakelight.setMode(new SingleColorMode(c));
62 System.out.println("setting color to " + c);
63 } else if (input.matches("(g|gamma)\\s+[0-9.]+")) {
64 String[] split = input.split("\\s+");
65 config.gamma = Double.parseDouble(split[1]);
66 Color.calculateGammaCorrection(config.gamma);
67 System.out.println("setting gamma to " + config.gamma);
68 } else if (input.matches("(s|saturation)\\s+[0-9.]+")) {
69 String[] split = input.split("\\s+");
70 config.video.saturation = Double.parseDouble(split[1]);
71 System.out.println("setting saturation to " + config.video.saturation);
72 } else if (input.matches("(n|noise)(\\s+[a-z0-9]+){2}")) {
73 TwoColorNoiseMode.getCommand().activate(this, input.split("\\s+"));
74 System.out.println("setting two-color noise mode");
75 }
76 } catch (IOException e) {
77 System.out.println("Error reading from command line");
78 break;
79 }
80 }
81 }
82
83 public interface Command {
84 String[] getNames();
85 void activate(Console console, String[] args);
86
87 default Color parseColor(String s) {
88 switch (s.toLowerCase()) {
89 case "r": return Color.rgb(255, 0, 0);
90 case "g": return Color.rgb(0, 255, 0);
91 case "b": return Color.rgb(0, 0, 255);
92 default: // assume hexadecimal
93 if (s.startsWith("#")) {
94 s = s.substring(1);
95 }
96 if (s.length() == 3) {
97 return Color.rgb(
98 Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16),
99 Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16),
100 Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16)
101 );
102 } else if (s.length() == 6) {
103 return Color.rgb(
104 Integer.parseInt(s.substring(0, 2), 16),
105 Integer.parseInt(s.substring(2, 4), 16),
106 Integer.parseInt(s.substring(4, 6), 16)
107 );
108 }
109 }
110 System.out.println("Failed to parse color '" + s + "'. Using black instead.");
111 return Color.BLACK;
112 }
113 }
114}