INNER CODE UNIT · Rust

colors_match

autopilot-rs/autopilot-rs · src/bitmap.rs:420

fn colors_match(c1: Rgba<u8>, c2: Rgba<u8>, tolerance: f64) -> bool {
    assert!(
        (0.0..=1.0).contains(&tolerance),
        "Tolerance must be between 0 and 1."
    );
    if tolerance == 0.0 {
        return c1 == c2;
    }

    let c1_channels = c1.channels();
    let c2_channels = c2.channels();
    let d1: f64 = (f64::from(c1_channels[0]) - f64::from(c2_channels[0])).abs();
    let d2: f64 = (f64::from(c1_channels[1]) - f64::from(c2_channels[1])).abs();
    let d3: f64 = (f64::from(c1_channels[2]) - f64::from(c2_channels[2])).abs();
    (d1 * d1 + d2 * d2 + d3 * d3).sqrt() <= tolerance * MAX_TOLERANCE_DELTA
}

const MAX_TOLERANCE_DELTA: f64 = 441.672_955_930_1; // => (3.0f64 * 255.0f64 * 255.0f64).sqrt();

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…