1 package kaka.cakelight;
4 private static int[] gammaCorrection = new int[256];
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);
14 public static Color rgb(double r, double g, double b) {
15 return rgb((int)(255 * r), (int)(255 * g), (int)(255 * b));
18 public static Color rgb(int r, int g, int b) {
19 Color c = new Color();
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);
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);
46 return gammaCorrection[r];
50 return gammaCorrection[g];
54 return gammaCorrection[b];
57 public Color interpolate(Color other, double value) {
58 double invertedValue = 1 - value;
60 (int)(r * invertedValue + other.r * value),
61 (int)(g * invertedValue + other.g * value),
62 (int)(b * invertedValue + other.b * value)