edit_xlsx/api/format/color.rs
1//!
2//! This module contains the [`FormatColor`] Enum, which used to set the color for the format.
3//! [`FormatColor`] is mainly used for setting fonts, backgrounds, and sheet tab colors.
4//! # Examples
5//!
6//! Use [`FormatColor`] to set font color
7//!
8//! ```
9//! use edit_xlsx::{Format, FormatColor, Workbook, Write};
10//! let mut workbook = Workbook::from_path("./examples/xlsx/accounting.xlsx").unwrap();
11//! let worksheet = workbook.get_worksheet_mut_by_name("worksheet").unwrap();
12//! let green = Format::default().set_color(FormatColor::RGB(0, 255, 0));
13//! worksheet.write_with_format("B6", "Green text", &green).unwrap();
14//! workbook.save_as("./examples/color_set_font_color.xlsx").unwrap();
15//! ```
16//!
17//! Use [`FormatColor`] to set background color
18//!
19//! **NOTICE**: According to Excel file format standard,
20//! when we are setting the background of xlsx,
21//! we are just modifying the background color of all the columns in xlsx.
22//!
23//! And when you set a color for a column,
24//! you must also set its width, or it will be hidden (i.e. width 0)
25//!
26//! ```
27//! use edit_xlsx::{Column, Format, FormatColor, Workbook, WorkSheetCol, Write};
28//! let mut workbook = Workbook::from_path("./examples/xlsx/accounting.xlsx").unwrap();
29//! let worksheet = workbook.get_worksheet_mut_by_name("worksheet").unwrap();
30//! let grey = Format::default().set_background_color(FormatColor::RGB(100, 100, 100));
31//! worksheet.set_columns_width_with_format("A:XFD", 8.12, &grey).unwrap();
32//! workbook.save_as("./examples/color_set_background_color.xlsx").unwrap();
33//! ```
34//!
35//! Use [`FormatColor`] to set sheet tab color
36//!
37//! ```
38//! use edit_xlsx::{Column, Format, FormatColor, Workbook, WorkSheetCol, Write};
39//! let mut workbook = Workbook::from_path("./examples/xlsx/accounting.xlsx").unwrap();
40//! let worksheet = workbook.get_worksheet_mut_by_name("worksheet").unwrap();
41//! let yellow = FormatColor::RGB(255, 255, 0);
42//! worksheet.set_tab_color(&yellow);
43//! workbook.save_as("./examples/color_set_tab_color.xlsx").unwrap();
44//! ```
45
46use crate::xml::common::FromFormat;
47use crate::xml::style::color::Color;
48
49/// [`FormatColor`] is mainly used for setting fonts, backgrounds, and sheet tab colors.
50///
51/// # Fields:
52/// | unit | fields |meaning|
53/// | ---- | ---- |----|
54/// | `Default` | |Default, no color|
55/// | `Index` | `index_id: u8,` | Using the [colorIndex property](https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2007/cc296089(v=office.12)),<br/> `index_id`: color index id. |
56/// | `Theme` | `theme_id: u8,`<br/>`tint: f64` |Use the Theme color format, i.e. leave the color decision to the current theme of xlsx.<br/>You can determine the theme color id and tint by `theme_id` and `tint` |
57/// | `RGB` | `red: u8,`<br/>`green: u8,`<br/>`blue: u8,` |Using the RGB color format.|
58#[derive(Clone, Debug, PartialEq, Copy)]
59pub enum FormatColor {
60 /// Default, no color
61 Default,
62 /// Using the [colorIndex property](https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2007/cc296089(v=office.12)),<br/> `index_id`: color index id.
63 Index(u8),
64 /// Use the Theme color format, i.e. leave the color decision to the current theme of xlsx.<br/>You can determine the theme color id and tint by `theme_id` and `tint`
65 Theme(u8, f64),
66 /// Using the RGB color format.
67 RGB(u8, u8, u8),
68}
69
70
71impl Default for FormatColor {
72 fn default() -> Self {
73 Self::Default
74 }
75}
76
77impl FromFormat<FormatColor> for Color {
78 fn set_attrs_by_format(&mut self, format: &FormatColor) {
79 match format {
80 FormatColor::Default => *self = Color::default(),
81 FormatColor::RGB(r, g, b) => *self = Color::from_rgb(*r, *g, *b),
82 FormatColor::Index(id) => *self = Color::from_index(*id),
83 FormatColor::Theme(theme, tint) => *self = Color::from_theme(*theme, *tint),
84 }
85 }
86
87 fn set_format(&self, format: &mut FormatColor) {
88 *format = if let Some(id) = self.indexed {
89 FormatColor::Index(id)
90 } else if let (Some(theme), tint) = (self.theme, self.tint) {
91 FormatColor::Theme(theme, tint.unwrap_or_default())
92 } else if let Some(color) = &self.rgb {
93 let argb: Vec<u8> = color
94 .chars()
95 .collect::<Vec<char>>()
96 .chunks(2)
97 .map(|chunk| {
98 let hex_string: String = chunk.iter().collect();
99 u8::from_str_radix(&hex_string, 16).unwrap()
100 })
101 .collect();
102 FormatColor::RGB(argb[1], argb[2], argb[3])
103 } else {
104 FormatColor::Default
105 };
106 }
107}