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