NanoXLSX.Writer 3.0.0-rc.3
Loading...
Searching...
No Matches
ColorWriter.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;
10using NanoXLSX.Registry;
11using NanoXLSX.Registry.Attributes;
12using NanoXLSX.Utils;
13using NanoXLSX.Utils.Xml;
14using System.Collections.Generic;
15
17{
21 [NanoXlsxPlugIn(PlugInUUID = PlugInUUID.ColorWriter)]
22 internal class ColorWriter : IColorWriter
23 {
30 public IEnumerable<XmlAttribute> GetAttributes(Color color)
31 {
32 List<XmlAttribute> attributes = new List<XmlAttribute>();
33 string name = GetAttributeName(color);
34 if (name != null)
35 {
36 string value = GetAttributeValue(color);
37 attributes.Add(new XmlAttribute(name, value));
38 }
39 if (UseTintAttribute(color))
40 {
41 attributes.Add(new XmlAttribute("tint", GetTintAttributeValue(color)));
42 }
43 return attributes;
44 }
45
52 public string GetAttributeName(Color color)
53 {
54 switch (color.Type)
55 {
56 case Color.ColorType.Auto:
57 return "auto";
58 case Color.ColorType.Rgb:
59 return "rgb";
60 case Color.ColorType.Indexed:
61 return "indexed";
62 case Color.ColorType.Theme:
63 return "theme";
64 case Color.ColorType.System:
65 return "system";
66 default:
67 return null;
68 }
69 }
70
77 public string GetAttributeValue(Color color)
78 {
79 string value = null;
80 switch (color.Type)
81 {
82 case Color.ColorType.Auto:
83 value = "1";
84 break;
85 case Color.ColorType.Rgb:
86 value = color.RgbColor.StringValue;
87 break;
88 case Color.ColorType.Indexed:
89 value = color.IndexedColor.StringValue;
90 break;
91 case Color.ColorType.Theme:
92 value = color.ThemeColor.StringValue;
93 break;
94 case Color.ColorType.System:
95 value = color.SystemColor.StringValue;
96 break;
97 }
98 return value;
99 }
100
106 public string GetTintAttributeValue(Color color)
107 {
108 if (!color.Tint.HasValue)
109 {
110 return null;
111 }
112 double tint = color.Tint.Value;
113 return tint == 0.0 ? null : ParserUtils.ToString(tint);
114 }
115
121 public bool UseTintAttribute(Color color)
122 {
123 return color != null && color.Type == Color.ColorType.Theme && (!color.Tint.HasValue || !Comparators.IsZero(color.Tint.Value));
124 }
125 }
126}
Interface, used by specific writers that provides color handling.