cadkit-core 0.5.2

CAD document model and operations, bounded byte readers, versioned JSON and SVG export for cadkit.
Documentation
//! AutoCAD Color Index (ACI) to RGB table.
//!
//! Sources, cross-checked entry by entry: the 256-entry `DXF_DEFAULT_COLORS` table of ezdxf
//! (MIT, <https://github.com/mozman/ezdxf>, `src/ezdxf/colors.py`) and the `_indexRgb` table of
//! ACadSharp (MIT, <https://github.com/DomCR/ACadSharp>, `src/ACadSharp/Color.cs`). The values
//! here are ezdxf's; ACadSharp differs in five entries of 10..=249 (29, 91, 145, 192, 203),
//! where ezdxf is the one that follows the tier pattern, and in the greys 250..=254, where it
//! carries other values. Index 0 (ByBlock) is black.

/// The 256-entry ACI palette, `(r, g, b)` per index.
pub const TABLE: [(u8, u8, u8); 256] = [
    (0, 0, 0),
    (255, 0, 0),
    (255, 255, 0),
    (0, 255, 0),
    (0, 255, 255),
    (0, 0, 255),
    (255, 0, 255),
    (255, 255, 255),
    (128, 128, 128),
    (192, 192, 192),
    (255, 0, 0),
    (255, 127, 127),
    (165, 0, 0),
    (165, 82, 82),
    (127, 0, 0),
    (127, 63, 63),
    (76, 0, 0),
    (76, 38, 38),
    (38, 0, 0),
    (38, 19, 19),
    (255, 63, 0),
    (255, 159, 127),
    (165, 41, 0),
    (165, 103, 82),
    (127, 31, 0),
    (127, 79, 63),
    (76, 19, 0),
    (76, 47, 38),
    (38, 9, 0),
    (38, 23, 19),
    (255, 127, 0),
    (255, 191, 127),
    (165, 82, 0),
    (165, 124, 82),
    (127, 63, 0),
    (127, 95, 63),
    (76, 38, 0),
    (76, 57, 38),
    (38, 19, 0),
    (38, 28, 19),
    (255, 191, 0),
    (255, 223, 127),
    (165, 124, 0),
    (165, 145, 82),
    (127, 95, 0),
    (127, 111, 63),
    (76, 57, 0),
    (76, 66, 38),
    (38, 28, 0),
    (38, 33, 19),
    (255, 255, 0),
    (255, 255, 127),
    (165, 165, 0),
    (165, 165, 82),
    (127, 127, 0),
    (127, 127, 63),
    (76, 76, 0),
    (76, 76, 38),
    (38, 38, 0),
    (38, 38, 19),
    (191, 255, 0),
    (223, 255, 127),
    (124, 165, 0),
    (145, 165, 82),
    (95, 127, 0),
    (111, 127, 63),
    (57, 76, 0),
    (66, 76, 38),
    (28, 38, 0),
    (33, 38, 19),
    (127, 255, 0),
    (191, 255, 127),
    (82, 165, 0),
    (124, 165, 82),
    (63, 127, 0),
    (95, 127, 63),
    (38, 76, 0),
    (57, 76, 38),
    (19, 38, 0),
    (28, 38, 19),
    (63, 255, 0),
    (159, 255, 127),
    (41, 165, 0),
    (103, 165, 82),
    (31, 127, 0),
    (79, 127, 63),
    (19, 76, 0),
    (47, 76, 38),
    (9, 38, 0),
    (23, 38, 19),
    (0, 255, 0),
    (127, 255, 127),
    (0, 165, 0),
    (82, 165, 82),
    (0, 127, 0),
    (63, 127, 63),
    (0, 76, 0),
    (38, 76, 38),
    (0, 38, 0),
    (19, 38, 19),
    (0, 255, 63),
    (127, 255, 159),
    (0, 165, 41),
    (82, 165, 103),
    (0, 127, 31),
    (63, 127, 79),
    (0, 76, 19),
    (38, 76, 47),
    (0, 38, 9),
    (19, 88, 23),
    (0, 255, 127),
    (127, 255, 191),
    (0, 165, 82),
    (82, 165, 124),
    (0, 127, 63),
    (63, 127, 95),
    (0, 76, 38),
    (38, 76, 57),
    (0, 38, 19),
    (19, 88, 28),
    (0, 255, 191),
    (127, 255, 223),
    (0, 165, 124),
    (82, 165, 145),
    (0, 127, 95),
    (63, 127, 111),
    (0, 76, 57),
    (38, 76, 66),
    (0, 38, 28),
    (19, 88, 88),
    (0, 255, 255),
    (127, 255, 255),
    (0, 165, 165),
    (82, 165, 165),
    (0, 127, 127),
    (63, 127, 127),
    (0, 76, 76),
    (38, 76, 76),
    (0, 38, 38),
    (19, 88, 88),
    (0, 191, 255),
    (127, 223, 255),
    (0, 124, 165),
    (82, 145, 165),
    (0, 95, 127),
    (63, 111, 127),
    (0, 57, 76),
    (38, 66, 126),
    (0, 28, 38),
    (19, 88, 88),
    (0, 127, 255),
    (127, 191, 255),
    (0, 82, 165),
    (82, 124, 165),
    (0, 63, 127),
    (63, 95, 127),
    (0, 38, 76),
    (38, 57, 126),
    (0, 19, 38),
    (19, 28, 88),
    (0, 63, 255),
    (127, 159, 255),
    (0, 41, 165),
    (82, 103, 165),
    (0, 31, 127),
    (63, 79, 127),
    (0, 19, 76),
    (38, 47, 126),
    (0, 9, 38),
    (19, 23, 88),
    (0, 0, 255),
    (127, 127, 255),
    (0, 0, 165),
    (82, 82, 165),
    (0, 0, 127),
    (63, 63, 127),
    (0, 0, 76),
    (38, 38, 126),
    (0, 0, 38),
    (19, 19, 88),
    (63, 0, 255),
    (159, 127, 255),
    (41, 0, 165),
    (103, 82, 165),
    (31, 0, 127),
    (79, 63, 127),
    (19, 0, 76),
    (47, 38, 126),
    (9, 0, 38),
    (23, 19, 88),
    (127, 0, 255),
    (191, 127, 255),
    (82, 0, 165),
    (124, 82, 165),
    (63, 0, 127),
    (95, 63, 127),
    (38, 0, 76),
    (57, 38, 126),
    (19, 0, 38),
    (28, 19, 88),
    (191, 0, 255),
    (223, 127, 255),
    (124, 0, 165),
    (145, 82, 165),
    (95, 0, 127),
    (111, 63, 127),
    (57, 0, 76),
    (66, 38, 76),
    (28, 0, 38),
    (88, 19, 88),
    (255, 0, 255),
    (255, 127, 255),
    (165, 0, 165),
    (165, 82, 165),
    (127, 0, 127),
    (127, 63, 127),
    (76, 0, 76),
    (76, 38, 76),
    (38, 0, 38),
    (88, 19, 88),
    (255, 0, 191),
    (255, 127, 223),
    (165, 0, 124),
    (165, 82, 145),
    (127, 0, 95),
    (127, 63, 111),
    (76, 0, 57),
    (76, 38, 66),
    (38, 0, 28),
    (88, 19, 88),
    (255, 0, 127),
    (255, 127, 191),
    (165, 0, 82),
    (165, 82, 124),
    (127, 0, 63),
    (127, 63, 95),
    (76, 0, 38),
    (76, 38, 57),
    (38, 0, 19),
    (88, 19, 28),
    (255, 0, 63),
    (255, 127, 159),
    (165, 0, 41),
    (165, 82, 103),
    (127, 0, 31),
    (127, 63, 79),
    (76, 0, 19),
    (76, 38, 47),
    (38, 0, 9),
    (88, 19, 23),
    (0, 0, 0),
    (101, 101, 101),
    (102, 102, 102),
    (153, 153, 153),
    (204, 204, 204),
    (255, 255, 255),
];

/// RGB value of ACI `index`, exactly as in [`TABLE`] (index 7 is white).
///
/// Use [`crate::Entity::resolved_color`] for display colors; it maps the "foreground"
/// color 7 to black so drawings read on a white background.
// A u8 index can never exceed the 256-entry table.
#[allow(clippy::indexing_slicing)]
pub const fn to_rgb(index: u8) -> (u8, u8, u8) {
    TABLE[index as usize]
}

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

    #[test]
    fn standard_colors() {
        assert_eq!(to_rgb(1), (255, 0, 0));
        assert_eq!(to_rgb(2), (255, 255, 0));
        assert_eq!(to_rgb(3), (0, 255, 0));
        assert_eq!(to_rgb(4), (0, 255, 255));
        assert_eq!(to_rgb(5), (0, 0, 255));
        assert_eq!(to_rgb(6), (255, 0, 255));
        assert_eq!(to_rgb(7), (255, 255, 255));
        assert_eq!(to_rgb(8), (128, 128, 128));
        assert_eq!(to_rgb(9), (192, 192, 192));
        assert_eq!(to_rgb(10), (255, 0, 0));
        assert_eq!(to_rgb(255), (255, 255, 255));
    }

    /// Tier pattern of 10..=249: hue group `g = (i-10)/10` (15 degree steps), tier from the
    /// position in the group (even: 255/165/127/76/38 of the pure hue; odd: halfway to grey).
    /// Entries listed in KNOWN_DEVIATIONS differ by more than 1 in both ezdxf and ACadSharp
    /// (AutoCAD's own table is irregular there), so they are pinned instead of derived.
    #[test]
    fn tier_pattern() {
        const KNOWN_DEVIATIONS: &[usize] = &[
            109, 119, 129, 139, 147, 149, 157, 159, 167, 169, 177, 179, 187, 189, 197, 199, 209,
            219, 229, 239, 249,
        ];
        let tiers = [255.0_f64, 165.0, 127.0, 76.0, 38.0];
        for i in 10..=249usize {
            let (g, p) = ((i - 10) / 10, (i - 10) % 10);
            let k = tiers[p / 2];
            let h = g as f64 * 15.0;
            let x = (h % 60.0) / 60.0;
            let (m, n) = ((255.0 * x).floor(), (255.0 * (1.0 - x)).floor());
            let hue = match (h / 60.0) as u32 {
                0 => [255.0, m, 0.0],
                1 => [n, 255.0, 0.0],
                2 => [0.0, 255.0, m],
                3 => [0.0, n, 255.0],
                4 => [m, 0.0, 255.0],
                _ => [255.0, 0.0, n],
            };
            let even = hue.map(|c| (c * k / 255.0 + 0.5).floor());
            let want = if p % 2 == 0 {
                even
            } else {
                even.map(|c| ((c + k) / 2.0).floor())
            };
            let (r, gg, b) = to_rgb(i as u8);
            let got = [f64::from(r), f64::from(gg), f64::from(b)];
            let off = got
                .iter()
                .zip(want.iter())
                .any(|(a, b)| (a - b).abs() > 1.0);
            assert_eq!(
                off,
                KNOWN_DEVIATIONS.contains(&i),
                "index {i}: got {got:?}, pattern {want:?}"
            );
        }
    }

    #[test]
    fn table_has_256_entries() {
        assert_eq!(TABLE.len(), 256);
    }
}