1 package kaka.cakelight;
3 public class LedFrame {
4 private Configuration config;
6 private int roff = 0, goff = 2, boff = 1; // Color values are stored as RBG, which is what the LED list takes.
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];
15 public void fillColor(int r, int g, int b) {
16 fillColor(Color.rgb(r, g, b));
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) {
28 public void setLedColor(int led, Color color) {
30 bytes[offset + roff] = (byte)color.r();
31 bytes[offset + goff] = (byte)color.g();
32 bytes[offset + boff] = (byte)color.b();
35 public Color getLedColor(int led) {
38 bytes[offset + roff] & 0xff,
39 bytes[offset + goff] & 0xff,
40 bytes[offset + boff] & 0xff
44 public byte[] getBytes() {
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;
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;