cadkit-dgn 0.5.3

Clean-room DGN V7 and V8 reader and seed-based DGN V8 writer.
Documentation
//! Default DGN color palette.

/// The 256-entry palette used when a file carries no color table of its own.
///
/// Values ported from GDAL dgnlib `abyDefaultPCT` (`ogr/ogrsf_frmts/dgn/dgnhelp.cpp`,
/// MIT/X license, Frank Warmerdam / Avenza Systems). Index 0 is white, 1 blue, 2 green,
/// 3 red, 4 yellow, 5 magenta, 6 orange, 7 cyan, then graded shades.
#[rustfmt::skip]
pub const DEFAULT_PALETTE: [[u8; 3]; 256] = [
    [255, 255, 255], [0, 0, 255], [0, 255, 0], [255, 0, 0], [255, 255, 0], [255, 0, 255],
    [255, 127, 0], [0, 255, 255], [64, 64, 64], [192, 192, 192], [254, 0, 96], [160, 224, 0],
    [0, 254, 160], [128, 0, 160], [176, 176, 176], [0, 240, 240], [240, 240, 240], [0, 0, 240],
    [0, 240, 0], [240, 0, 0], [240, 240, 0], [240, 0, 240], [240, 122, 0], [0, 240, 240],
    [240, 240, 240], [0, 0, 240], [0, 240, 0], [240, 0, 0], [240, 240, 0], [240, 0, 240],
    [240, 122, 0], [0, 225, 225], [225, 225, 225], [0, 0, 225], [0, 225, 0], [225, 0, 0],
    [225, 225, 0], [225, 0, 225], [225, 117, 0], [0, 225, 225], [225, 225, 225], [0, 0, 225],
    [0, 225, 0], [225, 0, 0], [225, 225, 0], [225, 0, 225], [225, 117, 0], [0, 210, 210],
    [210, 210, 210], [0, 0, 210], [0, 210, 0], [210, 0, 0], [210, 210, 0], [210, 0, 210],
    [210, 112, 0], [0, 210, 210], [210, 210, 210], [0, 0, 210], [0, 210, 0], [210, 0, 0],
    [210, 210, 0], [210, 0, 210], [210, 112, 0], [0, 195, 195], [195, 195, 195], [0, 0, 195],
    [0, 195, 0], [195, 0, 0], [195, 195, 0], [195, 0, 195], [195, 107, 0], [0, 195, 195],
    [195, 195, 195], [0, 0, 195], [0, 195, 0], [195, 0, 0], [195, 195, 0], [195, 0, 195],
    [195, 107, 0], [0, 180, 180], [180, 180, 180], [0, 0, 180], [0, 180, 0], [180, 0, 0],
    [180, 180, 0], [180, 0, 180], [180, 102, 0], [0, 180, 180], [180, 180, 180], [0, 0, 180],
    [0, 180, 0], [180, 0, 0], [180, 180, 0], [180, 0, 180], [180, 102, 0], [0, 165, 165],
    [165, 165, 165], [0, 0, 165], [0, 165, 0], [165, 0, 0], [165, 165, 0], [165, 0, 165],
    [165, 97, 0], [0, 165, 165], [165, 165, 165], [0, 0, 165], [0, 165, 0], [165, 0, 0],
    [165, 165, 0], [165, 0, 165], [165, 97, 0], [0, 150, 150], [150, 150, 150], [0, 0, 150],
    [0, 150, 0], [150, 0, 0], [150, 150, 0], [150, 0, 150], [150, 92, 0], [0, 150, 150],
    [150, 150, 150], [0, 0, 150], [0, 150, 0], [150, 0, 0], [150, 150, 0], [150, 0, 150],
    [150, 92, 0], [0, 135, 135], [135, 135, 135], [0, 0, 135], [0, 135, 0], [135, 0, 0],
    [135, 135, 0], [135, 0, 135], [135, 87, 0], [0, 135, 135], [135, 135, 135], [0, 0, 135],
    [0, 135, 0], [135, 0, 0], [135, 135, 0], [135, 0, 135], [135, 87, 0], [0, 120, 120],
    [120, 120, 120], [0, 0, 120], [0, 120, 0], [120, 0, 0], [120, 120, 0], [120, 0, 120],
    [120, 82, 0], [0, 120, 120], [120, 120, 120], [0, 0, 120], [0, 120, 0], [120, 0, 0],
    [120, 120, 0], [120, 0, 120], [120, 82, 0], [0, 105, 105], [105, 105, 105], [0, 0, 105],
    [0, 105, 0], [105, 0, 0], [105, 105, 0], [105, 0, 105], [105, 77, 0], [0, 105, 105],
    [105, 105, 105], [0, 0, 105], [0, 105, 0], [105, 0, 0], [105, 105, 0], [105, 0, 105],
    [105, 77, 0], [0, 90, 90], [90, 90, 90], [0, 0, 90], [0, 90, 0], [90, 0, 0],
    [90, 90, 0], [90, 0, 90], [90, 72, 0], [0, 90, 90], [90, 90, 90], [0, 0, 90],
    [0, 90, 0], [90, 0, 0], [90, 90, 0], [90, 0, 90], [90, 72, 0], [0, 75, 75],
    [75, 75, 75], [0, 0, 75], [0, 75, 0], [75, 0, 0], [75, 75, 0], [75, 0, 75],
    [75, 67, 0], [0, 75, 75], [75, 75, 75], [0, 0, 75], [0, 75, 0], [75, 0, 0],
    [75, 75, 0], [75, 0, 75], [75, 67, 0], [0, 60, 60], [60, 60, 60], [0, 0, 60],
    [0, 60, 0], [60, 0, 0], [60, 60, 0], [60, 0, 60], [60, 62, 0], [0, 60, 60],
    [60, 60, 60], [0, 0, 60], [0, 60, 0], [60, 0, 0], [60, 60, 0], [60, 0, 60],
    [60, 62, 0], [0, 45, 45], [45, 45, 45], [0, 0, 45], [0, 45, 0], [45, 0, 0],
    [45, 45, 0], [45, 0, 45], [45, 57, 0], [0, 45, 45], [45, 45, 45], [0, 0, 45],
    [0, 45, 0], [45, 0, 0], [45, 45, 0], [45, 0, 45], [45, 57, 0], [0, 30, 30],
    [30, 30, 30], [0, 0, 30], [0, 30, 0], [30, 0, 0], [30, 30, 0], [30, 0, 30],
    [30, 52, 0], [0, 30, 30], [30, 30, 30], [0, 0, 30], [0, 30, 0], [30, 0, 0],
    [30, 30, 0], [30, 0, 30], [192, 192, 192], [28, 0, 100],
];

/// The lowest index of `table` whose entry equals `rgb`, as the DGN writer matches a neutral
/// RGB or ACI color against the seed palette ([`crate::V8Seed::palette`]); `None` when no
/// entry equals it.
pub fn exact_index(table: &[[u8; 3]; 256], rgb: [u8; 3]) -> Option<u8> {
    table
        .iter()
        .position(|c| *c == rgb)
        .and_then(|i| u8::try_from(i).ok())
}

/// The index of the entry of `table` nearest to `rgb` by squared RGB distance, the lowest
/// index on a tie. [`crate::V8Seed::prepare_document`] maps off-palette colors this way.
pub fn nearest_index(table: &[[u8; 3]; 256], rgb: [u8; 3]) -> u8 {
    let mut best = (u32::MAX, 0u8);
    for (i, c) in (0..=u8::MAX).zip(table.iter()) {
        let dist: u32 = c
            .iter()
            .zip(rgb)
            .map(|(&a, b)| u32::from(a.abs_diff(b)).pow(2))
            .sum();
        if dist < best.0 {
            best = (dist, i);
            if dist == 0 {
                break;
            }
        }
    }
    best.1
}

/// RGB of `index` in `table`; `None` for indices outside 0..=255 (V8 extended colors).
pub(crate) fn lookup(table: &[[u8; 3]; 256], index: u32) -> Option<[u8; 3]> {
    usize::try_from(index)
        .ok()
        .and_then(|i| table.get(i))
        .copied()
}

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

    #[test]
    fn known_entries() {
        assert_eq!(DEFAULT_PALETTE[0], [255, 255, 255]);
        assert_eq!(DEFAULT_PALETTE[3], [255, 0, 0]);
        assert_eq!(DEFAULT_PALETTE[255], [28, 0, 100]);
        assert_eq!(lookup(&DEFAULT_PALETTE, 256), None);
    }

    #[test]
    fn exact_and_nearest_indexes() {
        // Entries 17 and 25 are both [0, 0, 240]: the lowest index wins.
        assert_eq!(exact_index(&DEFAULT_PALETTE, [255, 255, 255]), Some(0));
        assert_eq!(exact_index(&DEFAULT_PALETTE, [0, 0, 240]), Some(17));
        assert_eq!(exact_index(&DEFAULT_PALETTE, [1, 2, 3]), None);
        assert_eq!(nearest_index(&DEFAULT_PALETTE, [255, 0, 0]), 3);
        assert_eq!(nearest_index(&DEFAULT_PALETTE, [250, 12, 18]), 3);
        // Equal distances go to the lowest index.
        let mut table = [[0u8; 3]; 256];
        table[7] = [10, 10, 10];
        table[9] = [10, 10, 10];
        assert_eq!(nearest_index(&table, [9, 10, 11]), 7);
        assert_eq!(exact_index(&table, [10, 10, 10]), Some(7));
        assert_eq!(nearest_index(&table, [0, 0, 0]), 0);
    }
}