BREP_render 0.1.0

BREP Rust rendering engine: kernel-fed scene store + wgpu renderer (headless artifact, desktop window, and wasm canvas shells).
Documentation
//! Stable per-solid display colors + color-space helpers.
//!
//! `solid_color_srgb` is a byte-faithful port of the retired artifact renderer's
//! `solidColor`: FNV-1a over the name's UTF-16 code
//! units, hue = ((hash >>> 8) % 360)/360, HSL(hue, 0.45, 0.6). Keeping the hash
//! EXACT means re-baselined artifacts keep the colors users already know.

/// FNV-1a over UTF-16 code units with wrapping 32-bit multiplies — byte-compatible
/// with the original implementation.
fn fnv1a_utf16(name: &str) -> u32 {
    let mut hash: u32 = 2166136261;
    for unit in name.encode_utf16() {
        hash ^= unit as u32;
        hash = hash.wrapping_mul(16777619);
    }
    hash
}

/// HSL → sRGB, the exact formula the retired soft raster used.
fn hsl_to_rgb(h: f64, s: f64, l: f64) -> [f64; 3] {
    let q = if l < 0.5 { l * (1.0 + s) } else { l + s - l * s };
    let p = 2.0 * l - q;
    let channel = |t: f64| -> f64 {
        let t = t.rem_euclid(1.0);
        if t < 1.0 / 6.0 {
            p + (q - p) * 6.0 * t
        } else if t < 0.5 {
            q
        } else if t < 2.0 / 3.0 {
            p + (q - p) * (2.0 / 3.0 - t) * 6.0
        } else {
            p
        }
    };
    [channel(h + 1.0 / 3.0), channel(h), channel(h - 1.0 / 3.0)]
}

/// The stable per-solid display color, in sRGB (0..1 per channel).
pub fn solid_color_srgb(name: &str) -> [f64; 3] {
    let hue = ((fnv1a_utf16(name) >> 8) % 360) as f64 / 360.0;
    hsl_to_rgb(hue, 0.45, 0.6)
}

/// sRGB electro-optical transfer (decode): sRGB component → linear.
pub fn srgb_to_linear(c: f64) -> f64 {
    if c <= 0.04045 {
        c / 12.92
    } else {
        ((c + 0.055) / 1.055).powf(2.4)
    }
}

/// A 0xRRGGBB hex color → linear-space RGB (for light/material constants).
pub fn hex_to_linear(hex: u32) -> [f32; 3] {
    let r = ((hex >> 16) & 0xff) as f64 / 255.0;
    let g = ((hex >> 8) & 0xff) as f64 / 255.0;
    let b = (hex & 0xff) as f64 / 255.0;
    [
        srgb_to_linear(r) as f32,
        srgb_to_linear(g) as f32,
        srgb_to_linear(b) as f32,
    ]
}

/// A 0xRRGGBB hex color → sRGB-space RGB in 0..1 (for clear colors written to
/// a non-sRGB target, where the bytes should equal the hex exactly).
pub fn hex_to_srgb(hex: u32) -> [f64; 3] {
    [
        ((hex >> 16) & 0xff) as f64 / 255.0,
        ((hex >> 8) & 0xff) as f64 / 255.0,
        (hex & 0xff) as f64 / 255.0,
    ]
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn solid_color_matches_js_reference() {
        // Reference values computed with the original solidColor implementation.
        // "E17": hash=0x9e08ad25, hue = (0x9e08ad % 360)/360.
        let hash = fnv1a_utf16("E17");
        assert_eq!(hash, {
            let mut h: u32 = 2166136261;
            for c in "E17".chars() {
                h ^= c as u32;
                h = h.wrapping_mul(16777619);
            }
            h
        });
        let rgb = solid_color_srgb("E17");
        for c in rgb {
            assert!((0.0..=1.0).contains(&c));
        }
        // Stable across calls (determinism).
        assert_eq!(solid_color_srgb("E17"), solid_color_srgb("E17"));
        assert_ne!(solid_color_srgb("E17"), solid_color_srgb("P.CU1"));
    }

    #[test]
    fn hsl_formula_reference_points() {
        // HSL(0, 0.45, 0.6) => q=0.78, p=0.42; r=channel(1/3)=0.78? No: hue 0
        // puts red at channel(1/3)... verify grey when s=0.
        let grey = hsl_to_rgb(0.37, 0.0, 0.6);
        for c in grey {
            assert!((c - 0.6).abs() < 1e-12);
        }
    }
}