NanoXLSX.Writer 3.0.0-rc.3
Loading...
Searching...
No Matches
ThemeWriter.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2025
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
10using NanoXLSX.Registry;
11using NanoXLSX.Registry.Attributes;
12using NanoXLSX.Themes;
13using NanoXLSX.Utils.Xml;
14
16{
20 [NanoXlsxPlugIn(PlugInUUID = PlugInUUID.ThemeWriter)]
21 internal class ThemeWriter : IPlugInWriter
22 {
23
24 #region properties
28 public Workbook Workbook { get; set; }
29
33 public XmlElement XmlElement { get; private set; }
34
35 #endregion
36 #region constructors
40 internal ThemeWriter()
41 {
42 }
43
44 #endregion
45 #region methods
50 public void Init(IBaseWriter baseWriter)
51 {
52 this.Workbook = baseWriter.Workbook;
53 }
54
58 public void Execute()
59 {
60 Theme workbookTheme = Workbook.WorkbookTheme;
61 XmlElement = XmlElement.CreateElement("theme", "a");
62 XmlElement.AddNameSpaceAttribute("a", "xmlns", "http://schemas.openxmlformats.org/drawingml/2006/main");
63 XmlElement.AddAttribute("name", XmlUtils.SanitizeXmlValue(workbookTheme.Name));
64 XmlElement themeElements = XmlElement.AddChildElement("themeElements", "a");
65 themeElements.AddChildElement(GetColorSchemeElement(workbookTheme.Colors));
66
67 WriterPlugInHandler.HandleInlineQueuePlugins(ref themeElements, Workbook, PlugInUUID.ThemeInlineWriter);
68 }
69
75 private static XmlElement GetColorSchemeElement(ColorScheme scheme)
76 {
77 XmlElement colorScheme = XmlElement.CreateElementWithAttribute("clrScheme", "name", XmlUtils.SanitizeXmlValue(scheme.Name), "a");
78 colorScheme.AddChildElement(GetColor("dk1", scheme.Dark1, "a"));
79 colorScheme.AddChildElement(GetColor("lt1", scheme.Light1, "a"));
80 colorScheme.AddChildElement(GetColor("dk2", scheme.Dark2, "a"));
81 colorScheme.AddChildElement(GetColor("lt2", scheme.Light2, "a"));
82 colorScheme.AddChildElement(GetColor("accent1", scheme.Accent1, "a"));
83 colorScheme.AddChildElement(GetColor("accent2", scheme.Accent2, "a"));
84 colorScheme.AddChildElement(GetColor("accent3", scheme.Accent3, "a"));
85 colorScheme.AddChildElement(GetColor("accent4", scheme.Accent4, "a"));
86 colorScheme.AddChildElement(GetColor("accent5", scheme.Accent5, "a"));
87 colorScheme.AddChildElement(GetColor("accent6", scheme.Accent6, "a"));
88 colorScheme.AddChildElement(GetColor("hlink", scheme.Hyperlink, "a"));
89 colorScheme.AddChildElement(GetColor("folHlink", scheme.FollowedHyperlink, "a"));
90 return colorScheme;
91 }
92
100 private static XmlElement GetColor(string name, IColor color, string prefix)
101 {
102 XmlElement colorElement = XmlElement.CreateElement(name, prefix);
103 if (color is SystemColor)
104 {
105 SystemColor sysColor = color as SystemColor;
106 XmlElement sysColorElement = colorElement.AddChildElementWithAttribute("sysClr", "val", sysColor.StringValue, "a");
107 if (!string.IsNullOrEmpty(sysColor.LastColor))
108 {
109 sysColorElement.AddAttribute("lastClr", sysColor.LastColor);
110 }
111 }
112 else if (color is SrgbColor)
113 {
114 colorElement.AddChildElementWithAttribute("srgbClr", "val", color.StringValue, "a");
115 }
116 return colorElement;
117 }
118
119 #endregion
120 }
121}