27 lines
662 B
C++
27 lines
662 B
C++
#ifndef COLOR_H
|
|
#define COLOR_H
|
|
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
|
|
#include "rtweekend.hpp"
|
|
|
|
/* Writes color components as a space-delimited string of numbers in the range [0,255] */
|
|
void write_color(FILE *fp, color c, uint32_t samples_per_pixel)
|
|
{
|
|
float scale = 1.0 / samples_per_pixel;
|
|
|
|
// Divide the color by the number of samples
|
|
float r = sqrt(c.x * scale);
|
|
float g = sqrt(c.y * scale);
|
|
float b = sqrt(c.z * scale);
|
|
|
|
/* Write output */
|
|
fprintf(fp,
|
|
"%d %d %d\n",
|
|
(uint8_t) (255 * clamp(r, 0, 1)),
|
|
(uint8_t) (255 * clamp(g, 0, 1)),
|
|
(uint8_t) (255 * clamp(b, 0, 1)));
|
|
}
|
|
|
|
#endif
|