X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fkaka%2Fcakelight%2FColor.java;h=5ea98cdc106913022658f78360ed21a799b48aed;hb=579c24625e93b67fd74245fe835cb34906ee7090;hp=ba85866aa98eb2fe1c030d60dc32f4373fc3024b;hpb=242486dc84c4f5b0c1bfad9807099355246f4ee2;p=kaka%2Fcakelight.git diff --git a/src/kaka/cakelight/Color.java b/src/kaka/cakelight/Color.java index ba85866..5ea98cd 100644 --- a/src/kaka/cakelight/Color.java +++ b/src/kaka/cakelight/Color.java @@ -1,8 +1,22 @@ package kaka.cakelight; public class Color { + public static final Color BLACK = Color.rgb(0, 0, 0); + + 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) { + return rgb((int)(255 * r), (int)(255 * g), (int)(255 * b)); + } + public static Color rgb(int r, int g, int b) { Color c = new Color(); c.r = r; @@ -11,16 +25,83 @@ public class Color { return c; } + /** + * @param hue 0.0 - 1.0 + * @param saturation 0.0 - 1.0 + * @param value 0.0 - 1.0 + */ + public static Color hsv(double hue, double saturation, double value) { + double normalizedHue = hue - Math.floor(hue); + int h = (int)(normalizedHue * 6); + double f = normalizedHue * 6 - h; + double p = value * (1 - saturation); + double q = value * (1 - f * saturation); + double t = value * (1 - (1 - f) * saturation); + + switch (h) { + case 0: return rgb(value, t, p); + case 1: return rgb(q, value, p); + case 2: return rgb(p, value, t); + case 3: return rgb(p, q, value); + case 4: return rgb(t, p, value); + case 5: return rgb(value, p, q); + default: throw new RuntimeException("Something went wrong when converting from HSV to RGB. Input was " + hue + ", " + saturation + ", " + value); + } + } + + public double[] toHSV() { + return toHSV(r / 255.0, g / 255.0, b / 255.0); + } + + /** + * @return an array with hsv values ranging from 0.0 to 1.0 + */ + public static double[] toHSV(double r, double g, double b) { + double h, s, v; + double min = Math.min(Math.min(r, g), b); + double max = Math.max(Math.max(r, g), b); + + if (max == 0) { + return new double[] {0, 0, 0}; + } + + // Value + v = max; + + // Saturation + double delta = max - min; + s = delta / max; + + // Hue + if (delta > 0) { + if (r == max) { + h = (g - b) / delta; // between yellow & magenta + } else if (g == max) { + h = 2 + (b - r) / delta; // between cyan & yellow + } else { + h = 4 + (r - g) / delta; // between magenta & cyan + } + } else { + h = 0; + } + + h /= 6.0; + if (h < 0) + h += 1; + + return new double[] {h, s, v}; + } + 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) { @@ -31,4 +112,9 @@ public class Color { (int)(b * invertedValue + other.b * value) ); } + + @Override + public String toString() { + return "Color{r=" + r + ", g=" + g + ", b=" + b + "}"; + } }