解决标注颜色问题

master
liaoxianglian 5 months ago
parent dc7edc3de0
commit 55ca13ecb4

@ -90,9 +90,10 @@ impl From<&DynamicText> for XMLElement {
let base_y = {
let base_y_pos = txt.y + 0.5 - (7.0 / 5.0 * pt_size + 26.0 / 5.0) + pt_size;
match txt.v_alignment {
VAlignment::Top => base_y_pos,
VAlignment::Top => base_y_pos + pt_size / 2.0,
VAlignment::Center => base_y_pos - pt_size / 2.0,
VAlignment::Bottom => base_y_pos - pt_size,
VAlignment::Bottom => base_y_pos - pt_size + pt_size / 2.0,
}
};
@ -136,6 +137,12 @@ impl From<&DynamicText> for XMLElement {
text_xml.add_text(&txt.text);
dtxt_xml.add_child(text_xml);
// 添加颜色
let mut color_xml = XMLElement::new("color");
color_xml.add_text(&txt.color.display_rgb());
dtxt_xml.add_child(color_xml);
if let Some(i_name) = &txt.info_name {
dtxt_xml.add_attribute("info_name", i_name);
}

@ -704,7 +704,13 @@ impl<'a> ObjectsBuilder<'a> {
//how best to pass in the flag for dynamic text or not....should the flag also default to true?
let mut text: Text = (
text,
HexColor::from_u32(self.ent.common.color_24_bit as u32),
{
if let Some(aci_index) = self.ent.common.color.index() {
aci_to_hex_color(aci_index)
} else {
HexColor::from_u32(self.ent.common.color_24_bit as u32)
}
},
)
.into();
@ -716,7 +722,15 @@ impl<'a> ObjectsBuilder<'a> {
Objects::Text(text)
} else {
let mut dtext = DTextBuilder::from_text(text)
.color(HexColor::from_u32(self.ent.common.color_24_bit as u32))
.color({
// 优先使用 ACI 颜色索引
if let Some(aci_index) = self.ent.common.color.index() {
aci_to_hex_color(aci_index)
} else {
// 回退到24位颜色
HexColor::from_u32(self.ent.common.color_24_bit as u32)
}
})
.build();
dtext.scale(self.scale_fact.x, self.scale_fact.y);
@ -971,7 +985,16 @@ impl<'a> ObjectsBuilder<'a> {
EntityType::AttributeDefinition(attrib) => Ok({
//need to look up the proper way to get the color for the Attrib
let mut dtext = DTextBuilder::from_attrib(attrib)
.color(HexColor::from_u32(self.ent.common.color_24_bit as u32))
.color({
println!("color index:{:?}", self.ent.common.color.index());
// 优先使用 ACI 颜色索引
if let Some(aci_index) = self.ent.common.color.index() {
aci_to_hex_color(aci_index)
} else {
// 回退到24位颜色
HexColor::from_u32(self.ent.common.color_24_bit as u32)
}
})
.build();
dtext.scale(self.scale_fact.x, self.scale_fact.y);
@ -1662,3 +1685,26 @@ enum TextEntity<'a> {
Attrib(&'a AttributeDefinition),
}
// AutoCAD Color Index (ACI) 到 RGB 转换函数
fn aci_to_rgb(aci: u8) -> (u8, u8, u8) {
match aci {
1 => (255, 0, 0), // 红色
2 => (255, 255, 0), // 黄色
3 => (0, 255, 0), // 绿色
4 => (0, 255, 255), // 青色
5 => (0, 0, 255), // 蓝色
6 => (255, 0, 255), // 洋红色
7 => (255, 255, 255), // 白色
8 => (128, 128, 128), // 深灰色
9 => (192, 192, 192), // 浅灰色
94 => (0, 129, 0), // 深绿色
// 更多标准颜色可以根据需要添加
_ => (0, 0, 0), // 默认黑色
}
}
// ACI 转换为 HexColor
fn aci_to_hex_color(aci: u8) -> HexColor {
let (r, g, b) = aci_to_rgb(aci);
HexColor::rgb(r, g, b)
}
Loading…
Cancel
Save