Move color parsing method to command
[kaka/cakelight.git] / src / kaka / cakelight / Console.java
CommitLineData
cd28f68c
TW
1package kaka.cakelight;
2
67b0a758
TW
3import kaka.cakelight.mode.AmbientMode;
4import kaka.cakelight.mode.SingleColorMode;
eca6fd31 5import kaka.cakelight.mode.TwoColorNoiseMode;
67b0a758
TW
6import kaka.cakelight.mode.VideoMode;
7
cd28f68c
TW
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));
fa013e4b
TW
25
26 public CakeLight getCakelight() {
27 return cakelight;
28 }
29
30 public Configuration getConfig() {
31 return config;
32 }
cd28f68c
TW
33 }
34
35 @Override
36 public void run() {
37 while (true) {
38 System.out.print("> ");
39 try {
40 String input = reader.readLine();
c8657a8d 41 if (input.matches("[0-5]")) {
cd28f68c
TW
42 cakelight.setMode(new AmbientMode(new String[] {input}));
43 System.out.println("setting ambient mode to " + input);
0b5bef0c
TW
44 } else if (input.matches("v|video")) {
45 cakelight.setMode(new VideoMode());
cd28f68c
TW
46 } else if (input.matches("(b|brightness)\\s+[0-9]+")) {
47 String[] split = input.split("\\s+");
48 config.leds.brightness = Integer.parseInt(split[1]);
8a0c98f8 49 System.out.println("setting brightness to " + config.leds.brightness);
cd28f68c 50 } else if (input.matches("q|quit")) {
a276d5ab 51 cakelight.turnOff();
cd28f68c
TW
52 System.out.println("stopping cakelight");
53 break;
88438046
TW
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);
6b446d77 63 } else if (input.matches("(g|gamma)\\s+[0-9.]+")) {
8a0c98f8
TW
64 String[] split = input.split("\\s+");
65 config.gamma = Double.parseDouble(split[1]);
b1899e91 66 Color.calculateGammaCorrection(config.gamma);
8a0c98f8 67 System.out.println("setting gamma to " + config.gamma);
6b446d77 68 } else if (input.matches("(s|saturation)\\s+[0-9.]+")) {
53b74d64
TW
69 String[] split = input.split("\\s+");
70 config.video.saturation = Double.parseDouble(split[1]);
71 System.out.println("setting saturation to " + config.video.saturation);
e2721d51 72 } else if (input.matches("(n|noise)(\\s+[a-z0-9]+){2}")) {
fa013e4b 73 TwoColorNoiseMode.getCommand().activate(this, input.split("\\s+"));
e2721d51 74 System.out.println("setting two-color noise mode");
cd28f68c
TW
75 }
76 } catch (IOException e) {
77 System.out.println("Error reading from command line");
78 break;
79 }
80 }
81 }
eca6fd31
TW
82
83 public interface Command {
84 String[] getNames();
fa013e4b 85 void activate(Console console, String[] args);
baaaa10b
TW
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 }
eca6fd31 113 }
cd28f68c 114}