-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.h
65 lines (51 loc) · 1.62 KB
/
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
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
#ifndef COLOR_H
#define COLOR_H
#include "vec3.h"
#include <iostream>
using color = vec3;
inline double linear_to_gamma(double linear_component) {
return sqrt(linear_component);
}
void write_color(std::ostream& out, color pixel_color, int samples_per_pixel) {
auto r = pixel_color.x();
auto g = pixel_color.y();
auto b = pixel_color.z();
// Divide the color by the number of samples.
auto scale = 1.0 / samples_per_pixel;
r *= scale;
g *= scale;
b *= scale;
// Apply linear to gamma transform.
r = linear_to_gamma(r);
g = linear_to_gamma(g);
b = linear_to_gamma(b);
// Write the translated [0,255] value of each color component.
static const interval intensity(0.000, 0.999);
out << static_cast<int>(255.999 * intensity.clamp(r)) << ' '
<< static_cast<int>(255.999 * intensity.clamp(g)) << ' '
<< static_cast<int>(255.999 * intensity.clamp(b)) << '\n';
}
void write_color(
unsigned char& _r, unsigned char& _g, unsigned char& _b,
color pixel_color,
int samples_per_pixel)
{
auto r = pixel_color.x();
auto g = pixel_color.y();
auto b = pixel_color.z();
// Divide the color by the number of samples.
auto scale = 1.0 / samples_per_pixel;
r *= scale;
g *= scale;
b *= scale;
// Apply linear to gamma transform.
r = linear_to_gamma(r);
g = linear_to_gamma(g);
b = linear_to_gamma(b);
// Write the translated [0,255] value of each color component.
static const interval intensity(0.000, 0.999);
_r = static_cast<int>(255.999 * intensity.clamp(r));
_g = static_cast<int>(255.999 * intensity.clamp(g));
_b = static_cast<int>(255.999 * intensity.clamp(b));
}
#endif