New ambient mode
[kaka/cakelight.git] / src / kaka / cakelight / LedFrame.java
CommitLineData
03b67a73
TW
1package kaka.cakelight;
2
03b67a73 3public class LedFrame {
0bf6c885 4 private Configuration config;
ed56b145
TW
5 private byte[] bytes;
6 private int roff = 0, goff = 2, boff = 1; // Color values are stored as RBG, which is what the LED list takes.
03b67a73
TW
7
8 public static LedFrame from(Configuration config) {
9 LedFrame frame = new LedFrame();
0bf6c885 10 frame.config = config;
ed56b145 11 frame.bytes = new byte[config.leds.getCount() * 3];
03b67a73
TW
12 return frame;
13 }
14
ed56b145
TW
15 public void fillColor(int r, int g, int b) {
16 for (int i = 0; i < bytes.length; i += 3) {
17 bytes[i + roff] = (byte)r;
18 bytes[i + goff] = (byte)g;
19 bytes[i + boff] = (byte)b;
20 }
21 }
22
23 public void fillColor(Color color) {
24 fillColor(color.r(), color.g(), color.b());
25 }
26
03b67a73 27 public void setLedColor(int led, Color color) {
ed56b145
TW
28 int offset = led * 3;
29 bytes[offset + roff] = (byte)color.r();
30 bytes[offset + goff] = (byte)color.g();
31 bytes[offset + boff] = (byte)color.b();
03b67a73
TW
32 }
33
34 public Color getLedColor(int led) {
ed56b145
TW
35 int offset = led * 3;
36 return Color.rgb(
37 bytes[offset + roff] & 0xff,
38 bytes[offset + goff] & 0xff,
39 bytes[offset + boff] & 0xff
40 );
41 }
42
43 public byte[] getBytes() {
44 return bytes;
03b67a73 45 }
0bf6c885
TW
46
47 // TODO this needs to be improved
48 /** The x position of the led from 0.0-1.0. */
49 public double xOf(int led) {
50 /* left */ if (led >= config.leds.cols * 2 + config.leds.rows) return 0;
51 /* top */ if (led >= config.leds.cols + config.leds.rows) return 1 - (double)(led - config.leds.cols - config.leds.rows) / config.leds.cols;
52 /* right */ if (led >= config.leds.cols) return 1;
53 /* bottom */ return (double)led / config.leds.cols;
54 }
55
56 /** The y position of the led from 0.0-1.0. */
57 public double yOf(int led) {
58 /* left */ if (led >= config.leds.cols * 2 + config.leds.rows) return (double)(led - config.leds.cols * 2 - config.leds.rows) / config.leds.rows;
59 /* top */ if (led >= config.leds.cols + config.leds.rows) return 0;
60 /* right */ if (led >= config.leds.cols) return 1 - (double)(led - config.leds.cols) / config.leds.rows;
61 /* bottom */ return 1;
62 }
03b67a73 63}