const std = @import("std");

const clamp = @import("./utils.zig").clamp;
const Color = @import("./vec.zig").Color;

pub fn write_color(c: Color, samples_per_pixel: isize) anyerror!void {
    var r = c.x();
    var g = c.y();
    var b = c.z();

    // Divide the color by the number of samples and gamma-correct for gamma=2.0.
    const scale = 1.0 / @intToFloat(f32, samples_per_pixel);
    r = @sqrt(scale * r);
    g = @sqrt(scale * g);
    b = @sqrt(scale * b);

    // Write the translated [0,255] value of each color component.
    var ir = @floatToInt(u8, 256 * clamp(r, 0, 0.999));
    var ig = @floatToInt(u8, 256 * clamp(g, 0, 0.999));
    var ib = @floatToInt(u8, 256 * clamp(b, 0, 0.999));

    const stdout = std.io.getStdOut();
    try stdout.writer().print("{d} {d} {d}\n", .{ ir, ig, ib });
}
