New ambient mode
[kaka/cakelight.git] / src / kaka / cakelight / LedFrame.java
1 package kaka.cakelight;
2
3 public class LedFrame {
4     private Configuration config;
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.
7
8     public static LedFrame from(Configuration config) {
9         LedFrame frame = new LedFrame();
10         frame.config = config;
11         frame.bytes = new byte[config.leds.getCount() * 3];
12         return frame;
13     }
14
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
27     public void setLedColor(int led, Color color) {
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();
32     }
33
34     public Color getLedColor(int led) {
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;
45     }
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     }
63 }