-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor.java
More file actions
45 lines (36 loc) · 1.32 KB
/
Color.java
File metadata and controls
45 lines (36 loc) · 1.32 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
import java.io.PrintStream;
// color is just an alias for vec3, but useful for clarity in the code.
public final class Color extends Vec3 {
public Color() {
super();
}
public Color(double r, double g, double b) {
super(r, g, b);
}
public static Color random() {
return new Color(Rtweekend.randomDouble(), Rtweekend.randomDouble(), Rtweekend.randomDouble());
}
public static Color random(double min, double max) {
return new Color(
Rtweekend.randomDouble(min, max),
Rtweekend.randomDouble(min, max),
Rtweekend.randomDouble(min, max)
);
}
private static final Interval INTENSITY = new Interval(0.000, 0.999);
private static double linearToGamma(double linearComponent) {
if (linearComponent > 0) {
return Math.sqrt(linearComponent);
}
return 0;
}
public static void writeColor(PrintStream out, Vec3 pixelColor) {
double r = linearToGamma(pixelColor.x());
double g = linearToGamma(pixelColor.y());
double b = linearToGamma(pixelColor.z());
int rByte = (int) (256 * INTENSITY.clamp(r));
int gByte = (int) (256 * INTENSITY.clamp(g));
int bByte = (int) (256 * INTENSITY.clamp(b));
out.println(rByte + " " + gByte + " " + bByte);
}
}