Skip to main content

umya_spreadsheet/structs/drawing/
scheme_color.rs

1// a:schemeClr
2use std::io::Cursor;
3
4use quick_xml::{
5    Reader,
6    Writer,
7    events::{
8        BytesStart,
9        Event,
10    },
11};
12
13use super::{
14    super::EnumValue,
15    PercentageType,
16    PositiveFixedPercentageType,
17    SchemeColorValues,
18};
19use crate::{
20    reader::driver::{
21        get_attribute,
22        xml_read_loop,
23    },
24    writer::driver::{
25        write_end_tag,
26        write_start_tag,
27    },
28};
29
30#[derive(Clone, Default, Debug)]
31pub struct SchemeColor {
32    val:                   EnumValue<SchemeColorValues>,
33    luminance:             Option<PercentageType>,
34    luminance_modulation:  Option<PercentageType>,
35    luminance_offset:      Option<PercentageType>,
36    saturation:            Option<PercentageType>,
37    saturation_modulation: Option<PercentageType>,
38    shade:                 Option<PositiveFixedPercentageType>,
39    alpha:                 Option<PositiveFixedPercentageType>,
40    tint:                  Option<PositiveFixedPercentageType>,
41}
42
43impl SchemeColor {
44    #[inline]
45    #[must_use]
46    pub fn val(&self) -> &SchemeColorValues {
47        self.val.value()
48    }
49
50    #[inline]
51    #[must_use]
52    #[deprecated(since = "3.0.0", note = "Use val()")]
53    pub fn get_val(&self) -> &SchemeColorValues {
54        self.val()
55    }
56
57    #[inline]
58    pub fn set_val(&mut self, value: SchemeColorValues) -> &mut Self {
59        self.val.set_value(value);
60        self
61    }
62
63    #[inline]
64    #[must_use]
65    pub fn luminance(&self) -> Option<&PercentageType> {
66        self.luminance.as_ref()
67    }
68
69    #[inline]
70    #[must_use]
71    #[deprecated(since = "3.0.0", note = "Use luminance()")]
72    pub fn get_luminance(&self) -> Option<&PercentageType> {
73        self.luminance()
74    }
75
76    #[inline]
77    pub fn luminance_mut(&mut self) -> Option<&mut PercentageType> {
78        self.luminance.as_mut()
79    }
80
81    #[inline]
82    #[deprecated(since = "3.0.0", note = "Use luminance_mut()")]
83    pub fn get_luminance_mut(&mut self) -> Option<&mut PercentageType> {
84        self.luminance_mut()
85    }
86
87    #[inline]
88    pub fn set_luminance(&mut self, value: PercentageType) {
89        self.luminance = Some(value);
90    }
91
92    #[inline]
93    #[must_use]
94    pub fn luminance_modulation(&self) -> Option<&PercentageType> {
95        self.luminance_modulation.as_ref()
96    }
97
98    #[inline]
99    #[must_use]
100    #[deprecated(since = "3.0.0", note = "Use luminance_modulation()")]
101    pub fn get_luminance_modulation(&self) -> Option<&PercentageType> {
102        self.luminance_modulation()
103    }
104
105    #[inline]
106    pub fn luminance_modulation_mut(&mut self) -> Option<&mut PercentageType> {
107        self.luminance_modulation.as_mut()
108    }
109
110    #[inline]
111    #[deprecated(since = "3.0.0", note = "Use luminance_modulation_mut()")]
112    pub fn get_luminance_modulation_mut(&mut self) -> Option<&mut PercentageType> {
113        self.luminance_modulation_mut()
114    }
115
116    #[inline]
117    pub fn set_luminance_modulation(&mut self, value: PercentageType) {
118        self.luminance_modulation = Some(value);
119    }
120
121    #[inline]
122    #[must_use]
123    pub fn luminance_offset(&self) -> Option<&PercentageType> {
124        self.luminance_offset.as_ref()
125    }
126
127    #[inline]
128    #[must_use]
129    #[deprecated(since = "3.0.0", note = "Use luminance_offset()")]
130    pub fn get_luminance_offset(&self) -> Option<&PercentageType> {
131        self.luminance_offset()
132    }
133
134    #[inline]
135    pub fn luminance_offset_mut(&mut self) -> Option<&mut PercentageType> {
136        self.luminance_offset.as_mut()
137    }
138
139    #[inline]
140    #[deprecated(since = "3.0.0", note = "Use luminance_offset_mut()")]
141    pub fn get_luminance_offset_mut(&mut self) -> Option<&mut PercentageType> {
142        self.luminance_offset_mut()
143    }
144
145    #[inline]
146    pub fn set_luminance_offset(&mut self, value: PercentageType) {
147        self.luminance_offset = Some(value);
148    }
149
150    #[inline]
151    #[must_use]
152    pub fn saturation(&self) -> Option<&PercentageType> {
153        self.saturation.as_ref()
154    }
155
156    #[inline]
157    #[must_use]
158    #[deprecated(since = "3.0.0", note = "Use saturation()")]
159    pub fn get_saturation(&self) -> Option<&PercentageType> {
160        self.saturation()
161    }
162
163    #[inline]
164    pub fn saturation_mut(&mut self) -> Option<&mut PercentageType> {
165        self.saturation.as_mut()
166    }
167
168    #[inline]
169    #[deprecated(since = "3.0.0", note = "Use saturation_mut()")]
170    pub fn get_saturation_mut(&mut self) -> Option<&mut PercentageType> {
171        self.saturation_mut()
172    }
173
174    #[inline]
175    pub fn set_saturation(&mut self, value: PercentageType) {
176        self.saturation = Some(value);
177    }
178
179    #[inline]
180    #[must_use]
181    pub fn saturation_modulation(&self) -> Option<&PercentageType> {
182        self.saturation_modulation.as_ref()
183    }
184
185    #[inline]
186    #[must_use]
187    #[deprecated(since = "3.0.0", note = "Use saturation_modulation()")]
188    pub fn get_saturation_modulation(&self) -> Option<&PercentageType> {
189        self.saturation_modulation()
190    }
191
192    #[inline]
193    pub fn saturation_modulation_mut(&mut self) -> Option<&mut PercentageType> {
194        self.saturation_modulation.as_mut()
195    }
196
197    #[inline]
198    #[deprecated(since = "3.0.0", note = "Use saturation_modulation_mut()")]
199    pub fn get_saturation_modulation_mut(&mut self) -> Option<&mut PercentageType> {
200        self.saturation_modulation_mut()
201    }
202
203    #[inline]
204    pub fn set_saturation_modulation(&mut self, value: PercentageType) {
205        self.saturation_modulation = Some(value);
206    }
207
208    #[inline]
209    #[must_use]
210    pub fn shade(&self) -> Option<&PositiveFixedPercentageType> {
211        self.shade.as_ref()
212    }
213
214    #[inline]
215    #[must_use]
216    #[deprecated(since = "3.0.0", note = "Use shade()")]
217    pub fn get_shade(&self) -> Option<&PositiveFixedPercentageType> {
218        self.shade()
219    }
220
221    #[inline]
222    pub fn shade_mut(&mut self) -> Option<&mut PositiveFixedPercentageType> {
223        self.shade.as_mut()
224    }
225
226    #[inline]
227    #[deprecated(since = "3.0.0", note = "Use shade_mut()")]
228    pub fn get_shade_mut(&mut self) -> Option<&mut PositiveFixedPercentageType> {
229        self.shade_mut()
230    }
231
232    #[inline]
233    pub fn set_shade(&mut self, value: PositiveFixedPercentageType) {
234        self.shade = Some(value);
235    }
236
237    #[inline]
238    #[must_use]
239    pub fn alpha(&self) -> Option<&PositiveFixedPercentageType> {
240        self.alpha.as_ref()
241    }
242
243    #[inline]
244    #[must_use]
245    #[deprecated(since = "3.0.0", note = "Use alpha()")]
246    pub fn get_alpha(&self) -> Option<&PositiveFixedPercentageType> {
247        self.alpha()
248    }
249
250    #[inline]
251    pub fn alpha_mut(&mut self) -> Option<&mut PositiveFixedPercentageType> {
252        self.alpha.as_mut()
253    }
254
255    #[inline]
256    #[deprecated(since = "3.0.0", note = "Use alpha_mut()")]
257    pub fn get_alpha_mut(&mut self) -> Option<&mut PositiveFixedPercentageType> {
258        self.alpha_mut()
259    }
260
261    #[inline]
262    pub fn set_alpha(&mut self, value: PositiveFixedPercentageType) {
263        self.alpha = Some(value);
264    }
265
266    #[inline]
267    #[must_use]
268    pub fn tint(&self) -> Option<&PositiveFixedPercentageType> {
269        self.tint.as_ref()
270    }
271
272    #[inline]
273    #[must_use]
274    #[deprecated(since = "3.0.0", note = "Use tint()")]
275    pub fn get_tint(&self) -> Option<&PositiveFixedPercentageType> {
276        self.tint()
277    }
278
279    #[inline]
280    pub fn tint_mut(&mut self) -> Option<&mut PositiveFixedPercentageType> {
281        self.tint.as_mut()
282    }
283
284    #[inline]
285    #[deprecated(since = "3.0.0", note = "Use tint_mut()")]
286    pub fn get_tint_mut(&mut self) -> Option<&mut PositiveFixedPercentageType> {
287        self.tint_mut()
288    }
289
290    #[inline]
291    pub fn set_tint(&mut self, value: PositiveFixedPercentageType) {
292        self.tint = Some(value);
293    }
294
295    #[inline]
296    pub(crate) fn with_inner_params(&self) -> bool {
297        self.luminance.is_some()
298            || self.luminance_modulation.is_some()
299            || self.luminance_offset.is_some()
300            || self.saturation.is_some()
301            || self.saturation_modulation.is_some()
302            || self.shade.is_some()
303            || self.alpha.is_some()
304            || self.tint.is_some()
305    }
306
307    pub(crate) fn set_attributes<R: std::io::BufRead>(
308        &mut self,
309        reader: &mut Reader<R>,
310        e: &BytesStart,
311        empty_flag: bool,
312    ) {
313        self.val.set_value_string(get_attribute(e, b"val").unwrap());
314
315        if empty_flag {
316            return;
317        }
318
319        xml_read_loop!(
320            reader,
321            ref n @ (Event::Empty(ref e) | Event::Start(ref e)) => {
322                let is_empty = matches!(n, Event::Empty(_));
323                match e.name().into_inner() {
324                    b"a:lum" => {
325                        let mut obj = PercentageType::default();
326                        obj.set_attributes(reader, e, is_empty);
327                        self.luminance = Some(obj);
328                    }
329                    b"a:lumMod" => {
330                        let mut obj = PercentageType::default();
331                        obj.set_attributes(reader, e, is_empty);
332                        self.luminance_modulation = Some(obj);
333                    }
334                    b"a:lumOff" => {
335                        let mut obj = PercentageType::default();
336                        obj.set_attributes(reader, e, is_empty);
337                        self.luminance_offset = Some(obj);
338                    }
339                    b"a:sat" => {
340                        let mut obj = PercentageType::default();
341                        obj.set_attributes(reader, e, is_empty);
342                        self.saturation = Some(obj);
343                    }
344                    b"a:satMod" => {
345                        let mut obj = PercentageType::default();
346                        obj.set_attributes(reader, e, is_empty);
347                        self.saturation_modulation = Some(obj);
348                    }
349                    b"a:shade" => {
350                        let mut obj = PositiveFixedPercentageType::default();
351                        obj.set_attributes(reader, e, is_empty);
352                        self.shade = Some(obj);
353                    }
354                    b"a:alpha" => {
355                        let mut obj = PositiveFixedPercentageType::default();
356                        obj.set_attributes(reader, e, is_empty);
357                        self.alpha = Some(obj);
358                    }
359                    b"a:tint" => {
360                        let mut obj = PositiveFixedPercentageType::default();
361                        obj.set_attributes(reader, e, is_empty);
362                        self.tint = Some(obj);
363                    }
364                    _ => (),
365                }
366            },
367            Event::End(ref e) => {
368                if e.name().into_inner() == b"a:schemeClr" {
369                    return;
370                }
371            },
372            Event::Eof => panic!("Error: Could not find {} end element", "a:schemeClr")
373        );
374    }
375
376    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
377        // a:schemeClr
378        if self.with_inner_params() {
379            write_start_tag(
380                writer,
381                "a:schemeClr",
382                vec![("val", self.val.value_string()).into()],
383                false,
384            );
385
386            // a:luminance
387            if let Some(v) = &self.luminance {
388                v.write_to_lum(writer);
389            }
390
391            // a:lumMod
392            if let Some(v) = &self.luminance_modulation {
393                v.write_to_lum_mod(writer);
394            }
395
396            // a:lumOff
397            if let Some(v) = &self.luminance_offset {
398                v.write_to_lum_off(writer);
399            }
400
401            // a:sat
402            if let Some(v) = &self.saturation {
403                v.write_to_sat(writer);
404            }
405
406            // a:satMod
407            if let Some(v) = &self.saturation_modulation {
408                v.write_to_sat_mod(writer);
409            }
410
411            // a:shade
412            if let Some(v) = &self.shade {
413                v.write_to_shade(writer);
414            }
415
416            // a:alpha
417            if let Some(v) = &self.alpha {
418                v.write_to_alpha(writer);
419            }
420
421            // a:tint
422            if let Some(v) = &self.tint {
423                v.write_to_tint(writer);
424            }
425
426            write_end_tag(writer, "a:schemeClr");
427        } else {
428            write_start_tag(
429                writer,
430                "a:schemeClr",
431                vec![("val", self.val.value_string()).into()],
432                true,
433            );
434        }
435    }
436}