Skip to main content

umya_spreadsheet/structs/
gradient_stop.rs

1// stop
2use std::io::Cursor;
3
4use md5::Digest;
5use quick_xml::{
6    Reader,
7    Writer,
8    events::{
9        BytesStart,
10        Event,
11    },
12};
13
14use super::{
15    Color,
16    DoubleValue,
17};
18use crate::{
19    reader::driver::{
20        get_attribute,
21        set_string_from_xml,
22        xml_read_loop,
23    },
24    writer::driver::{
25        write_end_tag,
26        write_start_tag,
27    },
28};
29
30#[derive(Default, Debug, Clone, PartialEq, PartialOrd)]
31pub struct GradientStop {
32    position: DoubleValue,
33    color:    Color,
34}
35
36impl GradientStop {
37    #[inline]
38    #[must_use]
39    pub fn position(&self) -> f64 {
40        self.position.value()
41    }
42
43    #[inline]
44    #[must_use]
45    #[deprecated(since = "3.0.0", note = "Use position()")]
46    pub fn get_position(&self) -> f64 {
47        self.position()
48    }
49
50    #[inline]
51    pub fn set_position(&mut self, value: f64) -> &mut Self {
52        self.position.set_value(value);
53        self
54    }
55
56    #[inline]
57    #[must_use]
58    pub fn color(&self) -> &Color {
59        &self.color
60    }
61
62    #[inline]
63    #[must_use]
64    #[deprecated(since = "3.0.0", note = "Use color()")]
65    pub fn get_color(&self) -> &Color {
66        self.color()
67    }
68
69    #[inline]
70    pub fn color_mut(&mut self) -> &mut Color {
71        &mut self.color
72    }
73
74    #[inline]
75    #[deprecated(since = "3.0.0", note = "Use color_mut()")]
76    pub fn get_color_mut(&mut self) -> &mut Color {
77        self.color_mut()
78    }
79
80    #[inline]
81    pub fn set_color(&mut self, value: Color) -> &mut Self {
82        self.color = value;
83        self
84    }
85
86    #[inline]
87    pub(crate) fn hash_code(&self) -> String {
88        format!(
89            "{:x}",
90            md5::Md5::digest(format!(
91                "{}{}",
92                self.position.value_string(),
93                self.color.hash_code(),
94            ))
95        )
96    }
97
98    #[inline]
99    #[deprecated(since = "3.0.0", note = "Use hash_code()")]
100    pub(crate) fn get_hash_code(&self) -> String {
101        self.hash_code()
102    }
103
104    pub(crate) fn set_attributes<R: std::io::BufRead>(
105        &mut self,
106        reader: &mut Reader<R>,
107        e: &BytesStart,
108    ) {
109        set_string_from_xml!(self, e, position, "position");
110
111        xml_read_loop!(
112            reader,
113            Event::Empty(ref e) => {
114                if e.name().into_inner() == b"color" {
115                    let mut obj = Color::default();
116                    obj.set_attributes(reader, e, true);
117                    self.set_color(obj);
118                }
119            },
120            Event::End(ref e) => {
121                if e.name().into_inner() == b"stop" {
122                    return
123                }
124            },
125            Event::Eof => panic!("Error: Could not find {} end element", "stop")
126        );
127    }
128
129    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
130        // stop
131        write_start_tag(
132            writer,
133            "stop",
134            vec![("position", &self.position.value_string()).into()],
135            false,
136        );
137
138        // color
139        self.color.write_to_color(writer);
140
141        write_end_tag(writer, "stop");
142    }
143}