1 package kaka.cakelight;
4 public static final Color BLACK = Color.rgb(0, 0, 0);
6 private static int[] gammaCorrection = new int[256];
8 public static void calculateGammaCorrection(double gamma) {
9 for (int i = 0, max = 255; i <= max; i++) {
10 gammaCorrection[i] = (int)(Math.pow((double)i / max, gamma) * max);
16 public static Color rgb(double r, double g, double b) {
17 return rgb((int)(255 * r), (int)(255 * g), (int)(255 * b));
20 public static Color rgb(int r, int g, int b) {
21 Color c = new Color();
29 * @param hue 0.0 - 1.0
30 * @param saturation 0.0 - 1.0
31 * @param value 0.0 - 1.0
33 public static Color hsv(double hue, double saturation, double value) {
34 double normalizedHue = hue - Math.floor(hue);
35 int h = (int)(normalizedHue * 6);
36 double f = normalizedHue * 6 - h;
37 double p = value * (1 - saturation);
38 double q = value * (1 - f * saturation);
39 double t = value * (1 - (1 - f) * saturation);
42 case 0: return rgb(value, t, p);
43 case 1: return rgb(q, value, p);
44 case 2: return rgb(p, value, t);
45 case 3: return rgb(p, q, value);
46 case 4: return rgb(t, p, value);
47 case 5: return rgb(value, p, q);
48 default: throw new RuntimeException("Something went wrong when converting from HSV to RGB. Input was " + hue + ", " + saturation + ", " + value);
52 public double[] toHSV() {
53 return toHSV(r / 255.0, g / 255.0, b / 255.0);
57 * @return an array with hsv values ranging from 0.0 to 1.0
59 public static double[] toHSV(double r, double g, double b) {
61 double min = Math.min(Math.min(r, g), b);
62 double max = Math.max(Math.max(r, g), b);
65 return new double[] {0, 0, 0};
72 double delta = max - min;
78 h = (g - b) / delta; // between yellow & magenta
79 } else if (g == max) {
80 h = 2 + (b - r) / delta; // between cyan & yellow
82 h = 4 + (r - g) / delta; // between magenta & cyan
92 return new double[] {h, s, v};
96 return gammaCorrection[r];
100 return gammaCorrection[g];
104 return gammaCorrection[b];
107 public Color interpolate(Color other, double value) {
108 double invertedValue = 1 - value;
110 (int)(r * invertedValue + other.r * value),
111 (int)(g * invertedValue + other.g * value),
112 (int)(b * invertedValue + other.b * value)
117 public String toString() {
118 return "Color{r=" + r + ", g=" + g + ", b=" + b + "}";