-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.h
36 lines (26 loc) · 999 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
#ifndef COLOR_H
#define COLOR_H
#include "vec3.h"
/*
Purpose:
- Defines a utility function that writes a single pixel's colour out to the standard output stream
Notes:
*/
using color = vec3;
void write_color(std::ostream& out, const 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 and gamma-correct for gamma=2.0
auto scale = 1.0 / samples_per_pixel;
r = sqrt(scale * r);
g = sqrt(scale * g);
b = sqrt(scale * b);
//Translate the [0,1] component values to the byte range [0,255]
int rbyte = int(255.999 * r);
int gbyte = int(255.999 * g);
int bbyte = int(255.999 * b);
//Write the translated [0,255] value of each colour component
out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << ' ' << static_cast<int>(256 * clamp(g, 0.0, 0.999)) << ' ' << static_cast<int>(256 * clamp(b, 0.0, 0.999)) << '\n';
}
#endif