-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor.java
More file actions
38 lines (31 loc) · 1 KB
/
Color.java
File metadata and controls
38 lines (31 loc) · 1 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
import java.lang.Math;
import java.util.function.*;
public class Color {
public int r, g, b;
public Color(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}
public Color(int value) {
this(value, value, value);
}
public Color clone() {
return new Color(r, g, b);
}
public static Color brighten(Color source, float factor) {
Function<Integer, Integer> brighten = x -> (int)(x * factor);
return new Color(brighten.apply(source.r), brighten.apply(source.g), brighten.apply(source.b));
}
public static Color gradient(Color startingColor, Color endingColor, float progress) {
return new Color(
lerp(startingColor.r, endingColor.r, progress),
lerp(startingColor.g, endingColor.g, progress),
lerp(startingColor.b, endingColor.b, progress)
);
}
private static int lerp(int a, int b, float t) {
if (t < 0 || t > 1) throw new IllegalArgumentException("t must be a value from 0 to 1!");
return Math.round(a + (b - a) * t);
}
}