Skip to main content

umya_spreadsheet/structs/drawing/
gradient_stop.rs

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