-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.h
37 lines (28 loc) · 902 Bytes
/
color.h
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
#ifndef COLOR_H
#define COLOR_H
#include "interval.h"
#include "vec3.h"
using color = vec3;
inline double linear_to_gamma(double linear_component) {
if (linear_component > 0) {
return sqrt(linear_component);
}
return 0;
}
void write_color(std::ostream& out, const color& pixel_color) {
auto r = pixel_color.x();
auto g = pixel_color.y();
auto b = pixel_color.z();
r = linear_to_gamma(r);
g = linear_to_gamma(g);
b = linear_to_gamma(b);
// The book calls this a translation from [0,1] to [0, 255].
// I feel like that's more of a transformation.
// Enlargement, maybe? Who knows.
static const interval intensity(0.000, 0.999);
int rbyte = int(256 * intensity.clamp(r));
int gbyte = int(256 * intensity.clamp(g));
int bbyte = int(256 * intensity.clamp(b));
out << rbyte << " " << gbyte << " " << bbyte << "\n";
}
#endif