docx_rust/formatting/
color.rs

1use hard_xml::{XmlRead, XmlWrite};
2use std::borrow::Cow;
3
4use crate::{__string_enum, __xml_test_suites};
5
6/// Text Color
7///
8/// Specifies the color to be used to display text.
9///
10/// ```rust
11/// use docx_rust::formatting::Color;
12///
13/// let color = Color::from("000000");
14/// let color = Color::from(String::from("000000"));
15/// let color = Color::from(0u32); // "000000"
16/// let color = Color::from((0u8, 0u8, 0u8)); // "000000"
17/// ```
18#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
19#[cfg_attr(test, derive(PartialEq))]
20#[xml(tag = "w:color")]
21pub struct Color<'a> {
22    #[xml(attr = "w:val")]
23    pub value: Cow<'a, str>,
24}
25
26impl<'a> From<&'a str> for Color<'a> {
27    fn from(val: &'a str) -> Self {
28        Color {
29            value: Cow::Borrowed(val),
30        }
31    }
32}
33
34impl From<String> for Color<'_> {
35    fn from(val: String) -> Self {
36        Color {
37            value: Cow::Owned(val),
38        }
39    }
40}
41
42impl From<u32> for Color<'_> {
43    fn from(val: u32) -> Self {
44        Color {
45            value: Cow::Owned(format!("{:06x}", val)),
46        }
47    }
48}
49
50impl From<(u8, u8, u8)> for Color<'_> {
51    fn from(val: (u8, u8, u8)) -> Self {
52        Color {
53            value: Cow::Owned(format!("{:02x}{:02x}{:02x}", val.0, val.1, val.2)),
54        }
55    }
56}
57
58#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
59#[cfg_attr(test, derive(PartialEq))]
60#[xml(tag = "w:highlight")]
61pub struct Highlight {
62    #[xml(attr = "w:val")]
63    pub value: Option<HighlightType>,
64}
65
66impl From<HighlightType> for Highlight {
67    fn from(val: HighlightType) -> Self {
68        Highlight { value: Some(val) }
69    }
70}
71
72#[derive(Debug, Clone)]
73#[cfg_attr(test, derive(PartialEq))]
74pub enum HighlightType {
75    Black,       //Black Highlighting Color
76    Blue,        //Blue Highlighting Color
77    Cyan,        //Cyan Highlighting Color
78    Green,       //Green Highlighting Color
79    Magenta,     //Magenta Highlighting Color
80    Red,         //Red Highlighting Color
81    Yellow,      //Yellow Highlighting Color
82    White,       //White Highlighting Color
83    DarkBlue,    //Dark Blue Highlighting Color
84    DarkCyan,    //Dark Cyan Highlighting Color
85    DarkGreen,   //Dark Green Highlighting Color
86    DarkMagenta, //Dark Magenta Highlighting Color
87    DarkRed,     //Dark Red Highlighting Color
88    DarkYellow,  //Dark Yellow Highlighting Color
89    DarkGray,    //Dark Gray Highlighting Color
90    LightGray,   //Light Gray Highlighting Color
91    None,        //No Text Highlighting
92}
93
94__string_enum! {
95    HighlightType {
96        Black = "black",
97        Blue = "blue",
98        Cyan = "cyan",
99        Green = "green",
100        Magenta = "magenta",
101        Red = "red",
102        Yellow = "yellow",
103        White = "white",
104        DarkBlue = "darkBlue",
105        DarkCyan = "darkCyan",
106        DarkGreen = "darkGreen",
107        DarkMagenta = "darkMagenta",
108        DarkRed = "darkRed",
109        DarkYellow = "darkYellow",
110        DarkGray = "darkGray",
111        LightGray = "lightGray",
112        None = "none",
113    }
114}
115
116#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
117#[cfg_attr(test, derive(PartialEq))]
118#[xml(tag = "w:vertAlign")]
119pub struct VertAlign {
120    #[xml(attr = "w:val")]
121    pub value: Option<VertAlignType>,
122}
123
124#[derive(Debug, Clone)]
125#[cfg_attr(test, derive(PartialEq))]
126pub enum VertAlignType {
127    Baseline,    //Regular Vertical Positioning
128    Superscript, //	Superscript
129    Subscript,   //	Subscript
130}
131
132__string_enum! {
133    VertAlignType {
134        Baseline = "baseline",
135        Subscript = "subscript",
136        Superscript = "superscript",
137    }
138}
139
140__xml_test_suites!(
141    Color,
142    Color::from("000000"),
143    r#"<w:color w:val="000000"/>"#,
144    Color::from(0u32),
145    r#"<w:color w:val="000000"/>"#,
146    Color::from((0u8, 0u8, 0u8)),
147    r#"<w:color w:val="000000"/>"#,
148);