Renderer/src/color.rs

34 lines
881 B
Rust

use super::{utility, Color, Rgb, RgbImage};
use std::io::Write;
pub fn write_color(out: &mut impl Write, pixel_color: &Color) {
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();
}
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,
]),
);
}