ph-color-bake 0.1.0

Host-side generator for auditable ph-color matrices, fixed-point LUTs, and golden vectors
Documentation
//! Host baker CLI. Derivation stays in this crate; the target never links it.

use ph_color_bake::Primaries;
use ph_color_bake::emit::{
    interp_lut, matrix_from_q428, oklab_module, oklab_tables, srgb_eotf, srgb_oetf,
};
use ph_color_bake::lut::{bake_lut, identity};
use ph_color_bake::matrix::rgb_to_rgb;
use ph_color_bake::srgb::{eotf, oetf};

fn main() {
    let args: Vec<String> = std::env::args().skip(1).collect();
    match args.as_slice() {
        [] => emit_rust(),
        [a] if a == "--emit-rust" => emit_rust(),
        [a] if a == "--emit-oklab-module" => print!("{}", oklab_module()),
        [a] if a == "--emit-golden" => emit_golden(),
        [a] if a == "--help" || a == "-h" => {
            println!(
                "ph-color-bake — host-only baker\n\nUsage:\n  ph-color-bake --emit-rust\n  ph-color-bake --emit-oklab-module\n  ph-color-bake --emit-golden\n\n--emit-rust writes Matrix3 and InterpLut const source to stdout.\n--emit-oklab-module writes the checked-in Oklab module to stdout.\n--emit-golden writes frozen CSV vectors under crates/color/tests/golden/."
            );
        }
        _ => {
            eprintln!("ph-color-bake: unknown args. Try --help");
            std::process::exit(2);
        }
    }
}

fn emit_golden() {
    let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../color/tests/golden");
    match ph_color_bake::golden::write_all(&dir) {
        Ok(()) => println!("wrote goldens under {}", dir.display()),
        Err(e) => {
            eprintln!("ph-color-bake: {e}");
            std::process::exit(1);
        }
    }
}

fn emit_rust() {
    let coefs = match rgb_to_rgb(Primaries::srgb(), Primaries::srgb()) {
        Ok(c) => c,
        Err(e) => {
            eprintln!("ph-color-bake: {e:?}");
            std::process::exit(1);
        }
    };
    print!(
        "{}",
        matrix_from_q428(
            "SRGB_TO_SRGB",
            "::ph_color::Srgb",
            "::ph_color::Srgb",
            coefs
        )
    );
    let lut = bake_lut::<17>(identity);
    print!(
        "{}",
        interp_lut("IDENTITY_LUT_17", lut.knots, lut.max_err_lsb)
    );
    let oetf257 = bake_lut::<257>(oetf);
    print!(
        "{}",
        srgb_oetf("SRGB_OETF", oetf257.knots, oetf257.max_err_lsb)
    );
    let eotf257 = bake_lut::<257>(eotf);
    print!(
        "{}",
        srgb_eotf("SRGB_EOTF", eotf257.knots, eotf257.max_err_lsb)
    );
    print!("{}", oklab_tables());
}