NanoXLSX.Writer 3.2.1
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 © 2026
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;
9using NanoXLSX.Interfaces;
10using NanoXLSX.Interfaces.Writer;
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; // Currently NoOp (see TODO in GetColor)
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 // TODO check whether colorWriter should be used here
107 XmlElement colorElement = XmlElement.CreateElement(name, prefix);
108 if (color is SystemColor)
109 {
110 SystemColor sysColor = color as SystemColor;
111 XmlElement sysColorElement = colorElement.AddChildElementWithAttribute("sysClr", "val", sysColor.StringValue, "a");
112 if (!string.IsNullOrEmpty(sysColor.LastColor))
113 {
114 sysColorElement.AddAttribute("lastClr", sysColor.LastColor);
115 }
116 }
117 else if (color is SrgbColor)
118 {
119 colorElement.AddChildElementWithAttribute("srgbClr", "val", color.StringValue, "a");
120 }
121 return colorElement;
122 }
123
124 #endregion
125 }
126}