-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColour.java
More file actions
185 lines (157 loc) · 5.52 KB
/
Copy pathColour.java
File metadata and controls
185 lines (157 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* The MIT License
*
* Copyright 2023 akava.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package ptjava;
public class Colour {
final double r;
final double g;
final double b;
public Colour(Colour c) {
r = c.r;
g = c.g;
b = c.b;
}
public Colour(double R, double G, double B) {
r = R;
g = G;
b = B;
}
public static final Colour Black = new Colour(0, 0, 0);
public static final Colour White = new Colour(1, 1, 1);
public Colour() {
r = 0;
g = 0;
b = 0;
}
public static Colour NewColor(int r, int g, int b) {
return new Colour((double) r / 65535, (double) g / 65535, (double) b / 65535);
}
public static Colour HexColor(int x) {
double r = ((x >> 16) & 0xff) / 255.0;
double g = ((x >> 8) & 0xff) / 255.0;
double b = ((x >> 0) & 0xff) / 255.0;
return new Colour(r, g, b).Pow(2.2);
}
public Colour Pow(double b) {
return new Colour(Math.pow(r, b), Math.pow(g, b), Math.pow(this.b, b));
}
public static <T extends Comparable<T>> T clamp(T val, T min, T max) {
if (val.compareTo(min) < 0) return min;
else if (val.compareTo(max) > 0) return max;
else return val;
}
public double LengthSquared() {
return this.r * this.r + this.g * this.g + this.b * this.b;
}
public static int getIntFromColor(double red, double green, double blue) {
if (Double.isNaN(red))
red = 0.0;
if (Double.isNaN(green))
green = 0.0;
if (Double.isNaN(blue))
blue = 0.0;
var r = (int)(256 * clamp(red, 0.0, 0.999));
var g = (int)(256 * clamp(green, 0.0, 0.999));
var b = (int)(256 * clamp(blue, 0.0, 0.999));
return (255 << 24) | (r << 16) | (g << 8) | b;
}
public static Colour Kelvin(double K) {
double red, green, blue;
double a, b, c, x;
// red
if (K >= 6600) {
a = 351.97690566805693;
b = 0.114206453784165;
c = -40.25366309332127;
x = K / 100 - 55;
red = a + b * x + c * Math.log(x);
} else {
red = 255;
}
if (K >= 6600) {
a = 325.4494125711974;
b = 0.07943456536662342;
c = -28.0852963507957;
x = K / 100 - 50;
green = a + b * x + c * Math.log(x);
} else if (K >= 1000) {
a = -155.25485562709179;
b = -0.44596950469579133;
c = 104.49216199393888;
x = K / 100 - 2;
green = a + b * x + c * Math.log(x);
} else {
green = 0;
}
if (K >= 6600) {
blue = 255;
} else if (K >= 2000) {
a = -254.76935184120902;
b = 0.8274096064007395;
c = 115.67994401066147;
x = K / 100 - 10;
blue = a + b * x + c * Math.log(x);
} else {
blue = 0;
}
red = Math.min(1, red / 255);
green = Math.min(1, green / 255);
blue = Math.min(1, blue / 255);
return new Colour(red, green, blue);
}
public Colour Mix(Colour b, double pct) {
Colour a = MulScalar(1 - pct);
b = b.MulScalar(pct);
return a.Add(b);
}
public Colour MulScalar(double b) {
return new Colour(r * b, g * b, this.b * b);
}
public Colour Add(Colour b) {
return new Colour(r + b.r, g + b.g, this.b + b.b);
}
public Colour Sub(Colour b) {
return new Colour(r - b.r, g - b.g, this.b - b.b);
}
public Colour Mul(Colour b) {
return new Colour(r * b.r, g * b.g, this.b * b.b);
}
public Colour Div(Colour b) {
return new Colour(r / b.r, g / b.g, this.b / b.b);
}
public Colour DivScalar(double b) {
return new Colour(r / b, g / b, this.b / b);
}
public Colour Min(Colour b) {
return new Colour(Math.min(r, b.r), Math.min(g, b.g), Math.min(this.b, b.b));
}
public Colour Max(Colour b) {
return new Colour(Math.max(r, b.r), Math.max(g, b.g), Math.max(this.b, b.b));
}
public double MinComponent() {
return Math.min(Math.min(r, g), b);
}
public double MaxComponent() {
return Math.max(Math.max(r, g), b);
}
}