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
8using NanoXLSX.Colors;
11using NanoXLSX.Registry;
12using NanoXLSX.Registry.Attributes;
13using NanoXLSX.Themes;
14using NanoXLSX.Utils.Xml;
15
17{
21 [NanoXlsxPlugIn(PlugInUUID = PlugInUUID.ThemeWriter)]
22 internal class ThemeWriter : IPluginWriter
23 {
24
25 private IColorWriter colorWriter;
26
27 #region properties
31 public Workbook Workbook { get; set; }
32
36 public XmlElement XmlElement { get; private set; }
37
38 #endregion
39 #region constructors
43 internal ThemeWriter()
44 {
45 }
46
47 #endregion
48 #region methods
53 public void Init(IBaseWriter baseWriter)
54 {
55 this.Workbook = baseWriter.Workbook;
56 this.colorWriter = PlugInLoader.GetPlugIn<IColorWriter>(PlugInUUID.ColorWriter, new ColorWriter());
57 }
58
62 public void Execute()
63 {
64 Theme workbookTheme = Workbook.WorkbookTheme;
65 XmlElement = XmlElement.CreateElement("theme", "a");
66 XmlElement.AddNameSpaceAttribute("a", "xmlns", "http://schemas.openxmlformats.org/drawingml/2006/main");
67 XmlElement.AddAttribute("name", XmlUtils.SanitizeXmlValue(workbookTheme.Name));
68 XmlElement themeElements = XmlElement.AddChildElement("themeElements", "a");
69 themeElements.AddChildElement(GetColorSchemeElement(workbookTheme.Colors));
70
71 WriterPlugInHandler.HandleInlineQueuePlugins(ref themeElements, Workbook, PlugInUUID.ThemeInlineWriter);
72 }
73
79 private static XmlElement GetColorSchemeElement(ColorScheme scheme)
80 {
81 XmlElement colorScheme = XmlElement.CreateElementWithAttribute("clrScheme", "name", XmlUtils.SanitizeXmlValue(scheme.Name), "a");
82 colorScheme.AddChildElement(GetColor("dk1", scheme.Dark1, "a"));
83 colorScheme.AddChildElement(GetColor("lt1", scheme.Light1, "a"));
84 colorScheme.AddChildElement(GetColor("dk2", scheme.Dark2, "a"));
85 colorScheme.AddChildElement(GetColor("lt2", scheme.Light2, "a"));
86 colorScheme.AddChildElement(GetColor("accent1", scheme.Accent1, "a"));
87 colorScheme.AddChildElement(GetColor("accent2", scheme.Accent2, "a"));
88 colorScheme.AddChildElement(GetColor("accent3", scheme.Accent3, "a"));
89 colorScheme.AddChildElement(GetColor("accent4", scheme.Accent4, "a"));
90 colorScheme.AddChildElement(GetColor("accent5", scheme.Accent5, "a"));
91 colorScheme.AddChildElement(GetColor("accent6", scheme.Accent6, "a"));
92 colorScheme.AddChildElement(GetColor("hlink", scheme.Hyperlink, "a"));
93 colorScheme.AddChildElement(GetColor("folHlink", scheme.FollowedHyperlink, "a"));
94 return colorScheme;
95 }
96
104 private static XmlElement GetColor(string name, IColor color, string prefix)
105 {
106 XmlElement colorElement = XmlElement.CreateElement(name, prefix);
107 if (color is SystemColor)
108 {
109 SystemColor sysColor = color as SystemColor;
110 XmlElement sysColorElement = colorElement.AddChildElementWithAttribute("sysClr", "val", sysColor.StringValue, "a");
111 if (!string.IsNullOrEmpty(sysColor.LastColor))
112 {
113 sysColorElement.AddAttribute("lastClr", sysColor.LastColor);
114 }
115 }
116 else if (color is SrgbColor)
117 {
118 colorElement.AddChildElementWithAttribute("srgbClr", "val", color.StringValue, "a");
119 }
120 return colorElement;
121 }
122
123 #endregion
124 }
125}