Files
vectorcraft/crates/cad/src/aci.rs
T
Dylan M. Taylor e4035a75db DXF import: read the drawing with craft_dxf::model
craft_dxf::parse_recovering reads the tags (ASCII and binary, any code
page), model::Document::read the sections, tables, blocks, layouts and
entity followers, and Entity::read each entity. The pair parser, the
records, the header, layer, block and layout readers and the colour
palette are gone; what stays is the mapping onto VectorCraft art.

Binary DXF files now open instead of being refused. A damaged binary
file says so.
2026-11-14 00:56:01 +00:00

43 lines
1.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! The colour index palette is `craft_dxf::model::aci`; here are the indices each colour depth
//! of the writer may use.
use crate::ColorDepth;
/// The RGB of colour index `i` (1–255; anything else is index 7's white).
pub fn aci_rgb(i: u8) -> [u8; 3] {
craft_dxf::model::aci::rgb(i)
}
/// The indices a colour depth may use (true colour: all of them, for the fallback index).
fn candidates(depth: ColorDepth) -> Box<dyn Iterator<Item = u8>> {
match depth {
ColorDepth::Aci8 => Box::new(1..=8),
ColorDepth::Aci16 => Box::new((1..=9).chain([30]).chain(250..=255)),
ColorDepth::Aci256 | ColorDepth::True => Box::new(1..=255),
}
}
/// The index of `depth` nearest to `rgb` (squared RGB distance; ties go to the lower index).
/// Index 7 stands for both white and black, as CAD apps draw it in the background's contrast.
pub fn nearest_aci(rgb: [u8; 3], depth: ColorDepth) -> u8 {
craft_dxf::model::aci::nearest(rgb, candidates(depth))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn nearest_index_by_depth() {
assert_eq!(nearest_aci([255, 0, 0], ColorDepth::Aci8), 1);
assert_eq!(nearest_aci([0, 0, 0], ColorDepth::Aci256), 7, "black is index 7");
assert_eq!(nearest_aci([255, 255, 255], ColorDepth::Aci16), 7);
assert_eq!(nearest_aci([255, 127, 0], ColorDepth::Aci256), 30);
assert_eq!(nearest_aci([255, 127, 0], ColorDepth::Aci16), 30);
assert_eq!(nearest_aci([255, 127, 0], ColorDepth::Aci8), 1, "8 colours: red is nearest");
assert_eq!(nearest_aci([60, 60, 60], ColorDepth::Aci16), 250);
assert_eq!(nearest_aci([100, 100, 100], ColorDepth::Aci8), 8);
assert_eq!(nearest_aci([60, 60, 60], ColorDepth::Aci8), 7, "dark greys go to black");
}
}