umya_spreadsheet/structs/drawing/
preset_color.rs1use std::io::Cursor;
3
4use quick_xml::{
5 Reader,
6 Writer,
7 events::{
8 BytesStart,
9 Event,
10 },
11};
12
13use super::alpha::Alpha;
14use crate::{
15 reader::driver::{
16 get_attribute,
17 xml_read_loop,
18 },
19 writer::driver::{
20 write_end_tag,
21 write_start_tag,
22 },
23};
24
25#[derive(Clone, Default, Debug)]
26pub struct PresetColor {
27 val: Box<str>,
28 alpha: Option<Alpha>,
29}
30
31impl PresetColor {
32 #[inline]
33 #[must_use]
34 pub fn val(&self) -> &str {
35 &self.val
36 }
37
38 #[inline]
39 #[must_use]
40 #[deprecated(since = "3.0.0", note = "Use val()")]
41 pub fn get_val(&self) -> &str {
42 self.val()
43 }
44
45 #[inline]
46 pub fn set_val<S: Into<String>>(&mut self, value: S) {
47 self.val = value.into().into_boxed_str();
48 }
49
50 #[inline]
51 #[must_use]
52 pub fn alpha(&self) -> Option<&Alpha> {
53 self.alpha.as_ref()
54 }
55
56 #[inline]
57 #[must_use]
58 #[deprecated(since = "3.0.0", note = "Use alpha()")]
59 pub fn get_alpha(&self) -> Option<&Alpha> {
60 self.alpha()
61 }
62
63 #[inline]
64 pub fn alpha_mut(&mut self) -> Option<&mut Alpha> {
65 self.alpha.as_mut()
66 }
67
68 #[inline]
69 #[deprecated(since = "3.0.0", note = "Use alpha_mut()")]
70 pub fn get_alpha_mut(&mut self) -> Option<&mut Alpha> {
71 self.alpha_mut()
72 }
73
74 #[inline]
75 pub fn set_alpha(&mut self, value: Alpha) {
76 self.alpha = Some(value);
77 }
78
79 pub(crate) fn set_attributes<R: std::io::BufRead>(
80 &mut self,
81 reader: &mut Reader<R>,
82 e: &BytesStart,
83 ) {
84 self.set_val(get_attribute(e, b"val").unwrap());
85
86 xml_read_loop!(
87 reader,
88 Event::Empty(ref e) => {
89 if e.name().into_inner() == b"a:alpha" {
90 let mut alpha = Alpha::default();
91 alpha.set_attributes(reader, e);
92 self.set_alpha(alpha);
93 }
94 },
95 Event::End(ref e) => {
96 if e.name().into_inner() == b"a:prstClr" {
97 return
98 }
99 },
100 Event::Eof => panic!("Error: Could not find {} end element", "a:prstClr")
101 );
102 }
103
104 pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
105 write_start_tag(writer, "a:prstClr", vec![("val", &self.val).into()], false);
107
108 if let Some(v) = &self.alpha {
110 v.write_to(writer);
111 }
112
113 write_end_tag(writer, "a:prstClr");
114 }
115}