Renderer/src/color.rs

35 lines
881 B
Rust
Raw Normal View History

2022-05-11 18:38:30 +02:00
use super::{utility, Color, Rgb, RgbImage};
2022-04-20 18:59:29 +02:00
use std::io::Write;
2022-04-20 18:58:58 +02:00
pub fn write_color(out: &mut impl Write, pixel_color: &Color) {
2022-04-20 18:59:29 +02:00
write!(
out,
"{} {} {}\n",
(255.999 * pixel_color.x()) as u32,
(255.999 * pixel_color.y()) as u32,
(255.999 * pixel_color.z()) as u32
)
.unwrap();
}
2022-05-11 18:38:30 +02:00
pub fn put_color(img: &mut RgbImage, pixel_color: &Color, x: u32, y: u32, samples_per_pixel: u32) {
let mut r = pixel_color.x();
let mut g = pixel_color.y();
let mut b = pixel_color.z();
let scale = 1.0 / samples_per_pixel as f64;
r *= scale;
g *= scale;
b *= scale;
img.put_pixel(
x,
y,
Rgb([
(256.0 * utility::clamp(r, 0.0, 0.999)) as u8,
(256.0 * utility::clamp(g, 0.0, 0.999)) as u8,
(256.0 * utility::clamp(b, 0.0, 0.999)) as u8,
]),
);
}