Commit | Line | Data |
---|---|---|
242486dc TW |
1 | package kaka.cakelight; |
2 | ||
3 | public class Color { | |
38c759f8 TW |
4 | private static int[] gammaCorrection = new int[256]; |
5 | ||
6 | public static void calculateGammaCorrection(double gamma) { | |
7 | for (int i = 0, max = 255; i <= max; i++) { | |
8 | gammaCorrection[i] = (int)(Math.pow((double)i / max, gamma) * max); | |
9 | } | |
10 | } | |
11 | ||
242486dc TW |
12 | private int r, g, b; |
13 | ||
8aea44b5 TW |
14 | public static Color rgb(double r, double g, double b) { |
15 | return rgb((int)(255 * r), (int)(255 * g), (int)(255 * b)); | |
16 | } | |
17 | ||
242486dc TW |
18 | public static Color rgb(int r, int g, int b) { |
19 | Color c = new Color(); | |
20 | c.r = r; | |
21 | c.g = g; | |
22 | c.b = b; | |
23 | return c; | |
24 | } | |
25 | ||
65af4342 TW |
26 | public static Color hsv(double hue, double saturation, double value) { |
27 | double normalizedHue = hue - Math.floor(hue); | |
28 | int h = (int)(normalizedHue * 6); | |
29 | double f = normalizedHue * 6 - h; | |
30 | double p = value * (1 - saturation); | |
31 | double q = value * (1 - f * saturation); | |
32 | double t = value * (1 - (1 - f) * saturation); | |
33 | ||
34 | switch (h) { | |
35 | case 0: return rgb(value, t, p); | |
36 | case 1: return rgb(q, value, p); | |
37 | case 2: return rgb(p, value, t); | |
38 | case 3: return rgb(p, q, value); | |
39 | case 4: return rgb(t, p, value); | |
40 | case 5: return rgb(value, p, q); | |
41 | default: throw new RuntimeException("Something went wrong when converting from HSV to RGB. Input was " + hue + ", " + saturation + ", " + value); | |
42 | } | |
43 | } | |
44 | ||
242486dc | 45 | public int r() { |
38c759f8 | 46 | return gammaCorrection[r]; |
242486dc TW |
47 | } |
48 | ||
49 | public int g() { | |
38c759f8 | 50 | return gammaCorrection[g]; |
242486dc TW |
51 | } |
52 | ||
53 | public int b() { | |
38c759f8 | 54 | return gammaCorrection[b]; |
242486dc TW |
55 | } |
56 | ||
57 | public Color interpolate(Color other, double value) { | |
58 | double invertedValue = 1 - value; | |
59 | return Color.rgb( | |
60 | (int)(r * invertedValue + other.r * value), | |
61 | (int)(g * invertedValue + other.g * value), | |
62 | (int)(b * invertedValue + other.b * value) | |
63 | ); | |
64 | } | |
65 | } |