Skip to main content

umya_spreadsheet/structs/drawing/
solid_fill.rs

1// a:solidFill
2use std::io::Cursor;
3
4use quick_xml::{
5    Reader,
6    Writer,
7    events::{
8        BytesStart,
9        Event,
10    },
11};
12
13use super::{
14    rgb_color_model_hex::RgbColorModelHex,
15    scheme_color::SchemeColor,
16};
17use crate::{
18    drawing::SystemColor,
19    reader::driver::xml_read_loop,
20    writer::driver::{
21        write_end_tag,
22        write_start_tag,
23    },
24};
25
26#[derive(Clone, Default, Debug)]
27pub struct SolidFill {
28    scheme_color:        Option<Box<SchemeColor>>,
29    rgb_color_model_hex: Option<Box<RgbColorModelHex>>,
30    system_color:        Option<Box<SystemColor>>,
31}
32
33impl SolidFill {
34    #[inline]
35    #[must_use]
36    pub fn scheme_color(&self) -> Option<&SchemeColor> {
37        self.scheme_color.as_deref()
38    }
39
40    #[inline]
41    #[must_use]
42    #[deprecated(since = "3.0.0", note = "Use scheme_color()")]
43    pub fn get_scheme_color(&self) -> Option<&SchemeColor> {
44        self.scheme_color()
45    }
46
47    #[inline]
48    pub fn scheme_color_mut(&mut self) -> Option<&mut SchemeColor> {
49        self.scheme_color.as_deref_mut()
50    }
51
52    #[inline]
53    #[deprecated(since = "3.0.0", note = "Use scheme_color_mut()")]
54    pub fn get_scheme_color_mut(&mut self) -> Option<&mut SchemeColor> {
55        self.scheme_color_mut()
56    }
57
58    #[inline]
59    pub fn set_scheme_color(&mut self, value: SchemeColor) {
60        self.scheme_color = Some(Box::new(value));
61    }
62
63    #[inline]
64    #[must_use]
65    pub fn rgb_color_model_hex(&self) -> Option<&RgbColorModelHex> {
66        self.rgb_color_model_hex.as_deref()
67    }
68
69    #[inline]
70    #[must_use]
71    #[deprecated(since = "3.0.0", note = "Use rgb_color_model_hex()")]
72    pub fn get_rgb_color_model_hex(&self) -> Option<&RgbColorModelHex> {
73        self.rgb_color_model_hex()
74    }
75
76    #[inline]
77    pub fn rgb_color_model_hex_mut(&mut self) -> Option<&mut RgbColorModelHex> {
78        self.rgb_color_model_hex.as_deref_mut()
79    }
80
81    #[inline]
82    #[deprecated(since = "3.0.0", note = "Use rgb_color_model_hex_mut()")]
83    pub fn get_rgb_color_model_hex_mut(&mut self) -> Option<&mut RgbColorModelHex> {
84        self.rgb_color_model_hex_mut()
85    }
86
87    #[inline]
88    pub fn set_rgb_color_model_hex(&mut self, value: RgbColorModelHex) {
89        self.rgb_color_model_hex = Some(Box::new(value));
90    }
91
92    #[inline]
93    #[must_use]
94    pub fn system_color(&self) -> Option<&SystemColor> {
95        self.system_color.as_deref()
96    }
97
98    #[inline]
99    #[must_use]
100    #[deprecated(since = "3.0.0", note = "Use system_color()")]
101    pub fn get_system_color(&self) -> Option<&SystemColor> {
102        self.system_color()
103    }
104
105    #[inline]
106    pub fn system_color_mut(&mut self) -> Option<&mut SystemColor> {
107        self.system_color.as_deref_mut()
108    }
109
110    #[inline]
111    #[deprecated(since = "3.0.0", note = "Use system_color_mut()")]
112    pub fn get_system_color_mut(&mut self) -> Option<&mut SystemColor> {
113        self.system_color_mut()
114    }
115
116    #[inline]
117    pub fn set_system_color(&mut self, value: SystemColor) {
118        self.system_color = Some(Box::new(value));
119    }
120
121    pub(crate) fn set_attributes<R: std::io::BufRead>(
122        &mut self,
123        reader: &mut Reader<R>,
124        _e: &BytesStart,
125    ) {
126        xml_read_loop!(
127            reader,
128            ref n @ (Event::Empty(ref e) | Event::Start(ref e)) => {
129                let is_empty = matches!(n, Event::Empty(_));
130                match e.name().into_inner() {
131                b"a:schemeClr" => {
132                    let mut scheme_color = SchemeColor::default();
133                    scheme_color.set_attributes(reader, e, is_empty);
134                    self.set_scheme_color(scheme_color);
135                }
136                b"a:srgbClr" => {
137                    let mut rgb_color_model_hex = RgbColorModelHex::default();
138                    rgb_color_model_hex.set_attributes(reader, e, is_empty);
139                    self.set_rgb_color_model_hex(rgb_color_model_hex);
140                }
141                b"a:sysClr" => {
142                    let mut obj = SystemColor::default();
143                    obj.set_attributes(reader, e, is_empty);
144                    self.set_system_color(obj);
145                }
146                _ => (),
147                }
148            },
149            Event::End(ref e) => {
150                if e.name().into_inner() == b"a:solidFill" {
151                    return;
152                }
153            },
154            Event::Eof => panic!("Error: Could not find {} end element", "a:solidFill")
155        );
156    }
157
158    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
159        // a:solidFill
160        write_start_tag(writer, "a:solidFill", vec![], false);
161        if let Some(color) = &self.scheme_color {
162            color.write_to(writer);
163        }
164
165        // a:srgbClr
166        if let Some(hex) = &self.rgb_color_model_hex {
167            hex.write_to(writer);
168        }
169
170        // a:sysClr
171        if let Some(v) = &self.system_color {
172            v.write_to(writer);
173        }
174
175        write_end_tag(writer, "a:solidFill");
176    }
177}