From: Tomas Wenström Date: Mon, 5 Jun 2017 20:04:03 +0000 (+0200) Subject: Added gamma correction X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=38c759f87fc7de47d9dee60088f2dbc60e0a55fb;p=kaka%2Fcakelight.git Added gamma correction --- diff --git a/config.properties b/config.properties index 4fec08b..7e04ee2 100644 --- a/config.properties +++ b/config.properties @@ -14,3 +14,5 @@ video.crop.bottom=18 leds.cols=32 leds.rows=17 + +gamma=1.5 diff --git a/src/kaka/cakelight/CakeLight.java b/src/kaka/cakelight/CakeLight.java index 5401c0f..c51a534 100644 --- a/src/kaka/cakelight/CakeLight.java +++ b/src/kaka/cakelight/CakeLight.java @@ -8,6 +8,7 @@ public class CakeLight { public CakeLight(Configuration config, LedController ledController) { this.config = config; this.ledController = ledController; + Color.calculateGammaCorrection(config.gamma); } public void setMode(Mode mode) { diff --git a/src/kaka/cakelight/Color.java b/src/kaka/cakelight/Color.java index 1e92da9..0756209 100644 --- a/src/kaka/cakelight/Color.java +++ b/src/kaka/cakelight/Color.java @@ -1,6 +1,14 @@ package kaka.cakelight; public class Color { + private static int[] gammaCorrection = new int[256]; + + public static void calculateGammaCorrection(double gamma) { + for (int i = 0, max = 255; i <= max; i++) { + gammaCorrection[i] = (int)(Math.pow((double)i / max, gamma) * max); + } + } + private int r, g, b; public static Color rgb(double r, double g, double b) { @@ -35,15 +43,15 @@ public class Color { } public int r() { - return r; + return gammaCorrection[r]; } public int g() { - return g; + return gammaCorrection[g]; } public int b() { - return b; + return gammaCorrection[b]; } public Color interpolate(Color other, double value) { diff --git a/src/kaka/cakelight/Configuration.java b/src/kaka/cakelight/Configuration.java index 2932c01..86ade8e 100644 --- a/src/kaka/cakelight/Configuration.java +++ b/src/kaka/cakelight/Configuration.java @@ -12,10 +12,12 @@ public class Configuration { private List> settings = new ArrayList<>(); public VideoConfiguration video; public LedConfiguration leds; + public double gamma; private Configuration(Properties prop) { video = new VideoConfiguration(prop); leds = new LedConfiguration(prop); + gamma = Double.parseDouble(get(prop,"gamma", "1")); } public static Configuration from(String propertiesFile) { diff --git a/src/kaka/cakelight/LedFrame.java b/src/kaka/cakelight/LedFrame.java index 9d95b0c..ab4ef5c 100644 --- a/src/kaka/cakelight/LedFrame.java +++ b/src/kaka/cakelight/LedFrame.java @@ -13,15 +13,16 @@ public class LedFrame { } public void fillColor(int r, int g, int b) { - for (int i = 0; i < bytes.length; i += 3) { - bytes[i + roff] = (byte)r; - bytes[i + goff] = (byte)g; - bytes[i + boff] = (byte)b; - } + fillColor(Color.rgb(r, g, b)); } public void fillColor(Color color) { - fillColor(color.r(), color.g(), color.b()); + byte r = (byte)color.r(), g = (byte)color.g(), b = (byte)color.b(); // Gamma corrected values + for (int i = 0; i < bytes.length; i += 3) { + bytes[i + roff] = r; + bytes[i + goff] = g; + bytes[i + boff] = b; + } } public void setLedColor(int led, Color color) {