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