Added gamma correction
[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         fillColor(Color.rgb(r, g, b));
17     }
18
19     public void fillColor(Color color) {
20         byte r = (byte)color.r(), g = (byte)color.g(), b = (byte)color.b(); // Gamma corrected values
21         for (int i = 0; i < bytes.length; i += 3) {
22             bytes[i + roff] = r;
23             bytes[i + goff] = g;
24             bytes[i + boff] = b;
25         }
26     }
27
28     public void setLedColor(int led, Color color) {
29         int offset = led * 3;
30         bytes[offset + roff] = (byte)color.r();
31         bytes[offset + goff] = (byte)color.g();
32         bytes[offset + boff] = (byte)color.b();
33     }
34
35     public Color getLedColor(int led) {
36         int offset = led * 3;
37         return Color.rgb(
38                 bytes[offset + roff] & 0xff,
39                 bytes[offset + goff] & 0xff,
40                 bytes[offset + boff] & 0xff
41         );
42     }
43
44     public byte[] getBytes() {
45         return bytes;
46     }
47
48     // TODO this needs to be improved
49     /** The x position of the led from 0.0-1.0. */
50     public double xOf(int led) {
51         /* left   */ if (led >= config.leds.cols * 2 + config.leds.rows) return 0;
52         /* top    */ if (led >= config.leds.cols + config.leds.rows) return 1 - (double)(led - config.leds.cols - config.leds.rows) / config.leds.cols;
53         /* right  */ if (led >= config.leds.cols) return 1;
54         /* bottom */ return (double)led / config.leds.cols;
55     }
56
57     /** The y position of the led from 0.0-1.0. */
58     public double yOf(int led) {
59         /* left   */ if (led >= config.leds.cols * 2 + config.leds.rows) return (double)(led - config.leds.cols * 2 - config.leds.rows) / config.leds.rows;
60         /* top    */ if (led >= config.leds.cols + config.leds.rows) return 0;
61         /* right  */ if (led >= config.leds.cols) return 1 - (double)(led - config.leds.cols) / config.leds.rows;
62         /* bottom */ return 1;
63     }
64 }