Added gamma correction
[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 15 public void fillColor(int r, int g, int b) {
38c759f8 16 fillColor(Color.rgb(r, g, b));
ed56b145
TW
17 }
18
19 public void fillColor(Color color) {
38c759f8
TW
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 }
ed56b145
TW
26 }
27
03b67a73 28 public void setLedColor(int led, Color color) {
ed56b145
TW
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();
03b67a73
TW
33 }
34
35 public Color getLedColor(int led) {
ed56b145
TW
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;
03b67a73 46 }
0bf6c885
TW
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 }
03b67a73 64}