4d6b68add12d99313bfaaedcf0d407434fea0d57
[kaka/cakelight.git] / src / kaka / cakelight / LedFrame.java
1 package kaka.cakelight;
2
3 public class LedFrame {
4     private byte[] bytes;
5     private int roff = 0, goff = 2, boff = 1; // Color values are stored as RBG, which is what the LED list takes.
6
7     public static LedFrame from(Configuration config) {
8         LedFrame frame = new LedFrame();
9         frame.bytes = new byte[config.leds.getCount() * 3];
10         return frame;
11     }
12
13     public void fillColor(int r, int g, int b) {
14         for (int i = 0; i < bytes.length; i += 3) {
15             bytes[i + roff] = (byte)r;
16             bytes[i + goff] = (byte)g;
17             bytes[i + boff] = (byte)b;
18         }
19     }
20
21     public void fillColor(Color color) {
22         fillColor(color.r(), color.g(), color.b());
23     }
24
25     public void setLedColor(int led, Color color) {
26         int offset = led * 3;
27         bytes[offset + roff] = (byte)color.r();
28         bytes[offset + goff] = (byte)color.g();
29         bytes[offset + boff] = (byte)color.b();
30     }
31
32     public Color getLedColor(int led) {
33         int offset = led * 3;
34         return Color.rgb(
35                 bytes[offset + roff] & 0xff,
36                 bytes[offset + goff] & 0xff,
37                 bytes[offset + boff] & 0xff
38         );
39     }
40
41     public byte[] getBytes() {
42         return bytes;
43     }
44 }