NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
Fill.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 System.Collections.Generic;
9using System.Text;
10using NanoXLSX.Utils;
11
12namespace NanoXLSX.Styles
13{
17 public class Fill : AbstractStyle
18 {
19 #region constants
23 public static readonly string DefaultColor = "FF000000";
27 public static readonly int DefaultIndexedColor = 64;
31 public static readonly PatternValue DefaultPatternFill = PatternValue.None;
32
33 #endregion
34
35 #region enums
39 public enum FillType
40 {
45 }
46
69 #endregion
70
71 #region privateFields
72 private string backgroundColor = DefaultColor;
73 private string foregroundColor = DefaultColor;
74 #endregion
75
76 #region properties
82 [Append]
83 public string BackgroundColor
84 {
85 get => backgroundColor;
86 set
87 {
88 Validators.ValidateColor(value, true);
89 backgroundColor = ParserUtils.ToUpper(value);
90 if (PatternFill == PatternValue.None)
91 {
93 }
94 }
95 }
96
101 [Append]
102 public string ForegroundColor
103 {
104 get => foregroundColor;
105 set
106 {
107 Validators.ValidateColor(value, true);
108 foregroundColor = ParserUtils.ToUpper(value);
109 if (PatternFill == PatternValue.None)
110 {
112 }
113 }
114 }
115
118 [Append]
119 public int IndexedColor { get; set; }
123 [Append]
124 public PatternValue PatternFill { get; set; }
125 #endregion
126
127 #region constructors
131 public Fill()
132 {
135 foregroundColor = DefaultColor;
136 backgroundColor = DefaultColor;
137 }
138
143 public Fill(string foreground, string background)
144 {
145 BackgroundColor = background;
146 ForegroundColor = foreground;
149 }
150
156 public Fill(string value, FillType fillType)
157 {
158 if (fillType == FillType.FillColor)
159 {
160 backgroundColor = DefaultColor;
161 ForegroundColor = value;
162 }
163 else
164 {
165 BackgroundColor = value;
166 foregroundColor = DefaultColor;
167 }
170 }
171 #endregion
172
173 #region methods
174
179 public override string ToString()
180 {
181 StringBuilder sb = new StringBuilder();
182 sb.Append("\"Fill\": {\n");
183 AddPropertyAsJson(sb, "BackgroundColor", BackgroundColor);
184 AddPropertyAsJson(sb, "ForegroundColor", ForegroundColor);
185 AddPropertyAsJson(sb, "IndexedColor", IndexedColor);
186 AddPropertyAsJson(sb, "PatternFill", PatternFill);
187 AddPropertyAsJson(sb, "HashCode", this.GetHashCode(), true);
188 sb.Append("\n}");
189 return sb.ToString();
190 }
191
196 public override AbstractStyle Copy()
197 {
198 Fill copy = new Fill
199 {
204 };
205 return copy;
206 }
207
214 public override int GetHashCode()
215 {
216 unchecked
217 {
218 int hashCode = -1564173520;
219 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(BackgroundColor);
220 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ForegroundColor);
221 hashCode = hashCode * -1521134295 + IndexedColor.GetHashCode();
222 hashCode = hashCode * -1521134295 + PatternFill.GetHashCode();
223 return hashCode;
224 }
225 }
226
232 public override bool Equals(object obj)
233 {
234 return obj is Fill fill &&
235 BackgroundColor == fill.BackgroundColor &&
236 ForegroundColor == fill.ForegroundColor &&
237 IndexedColor == fill.IndexedColor &&
238 PatternFill == fill.PatternFill;
239 }
240
245 public Fill CopyFill()
246 {
247 return (Fill)Copy();
248 }
249
255 public void SetColor(string value, FillType fillType)
256 {
257 if (fillType == FillType.FillColor)
258 {
259 backgroundColor = DefaultColor;
260 ForegroundColor = value;
261 }
262 else
263 {
264 BackgroundColor = value;
265 foregroundColor = DefaultColor;
266 }
268 }
269 #endregion
270
271 #region staticMethods
277 internal static string GetPatternName(PatternValue pattern)
278 {
279 string output;
280 switch (pattern)
281 {
282 case PatternValue.Solid:
283 output = "solid";
284 break;
285 case PatternValue.DarkGray:
286 output = "darkGray";
287 break;
288 case PatternValue.MediumGray:
289 output = "mediumGray";
290 break;
291 case PatternValue.LightGray:
292 output = "lightGray";
293 break;
294 case PatternValue.Gray0625:
295 output = "gray0625";
296 break;
297 case PatternValue.Gray125:
298 output = "gray125";
299 break;
300 default:
301 output = "none";
302 break;
303 }
304 return output;
305 }
306
310 internal static PatternValue GetPatternEnum(string name)
311 {
312 switch (name)
313 {
314 case "none": return PatternValue.None;
315 case "solid": return PatternValue.Solid;
316 case "darkGray": return PatternValue.DarkGray;
317 case "mediumGray": return PatternValue.MediumGray;
318 case "lightGray": return PatternValue.LightGray;
319 case "gray0625": return PatternValue.Gray0625;
320 case "gray125": return PatternValue.Gray125;
321 default:
322 return PatternValue.None;
323 }
324 }
325
326 #endregion
327
328 }
329}
Class represents an abstract style component.
FillType
Enum for the type of the color, used by the Fill class.
Definition Fill.cs:40
@ PatternColor
Color defines a pattern color.
Definition Fill.cs:42
@ FillColor
Color defines a solid fill color.
Definition Fill.cs:44
Fill CopyFill()
Method to copy the current object to a new one with casting.
Definition Fill.cs:245
override bool Equals(object obj)
Returns whether two instances are the same.
Definition Fill.cs:232
string BackgroundColor
Gets or sets the background color of the fill. The value is expressed as hex string with the format A...
Definition Fill.cs:84
Fill()
Default constructor.
Definition Fill.cs:131
static readonly PatternValue DefaultPatternFill
Default pattern.
Definition Fill.cs:31
override int GetHashCode()
Returns a hash code for this instance.
Definition Fill.cs:214
void SetColor(string value, FillType fillType)
Sets the color and the depending on fill type.
Definition Fill.cs:255
override string ToString()
Override toString method.
Definition Fill.cs:179
static readonly int DefaultIndexedColor
Default index color.
Definition Fill.cs:27
static readonly string DefaultColor
Default Color (foreground or background).
Definition Fill.cs:23
Fill(string value, FillType fillType)
Constructor with color value and fill type.
Definition Fill.cs:156
int IndexedColor
Gets or sets the indexed color (Default is 64).
Definition Fill.cs:119
override AbstractStyle Copy()
Method to copy the current object to a new one without casting.
Definition Fill.cs:196
PatternValue
Enum for the pattern values, used by the Fill class.
Definition Fill.cs:50
@ Gray125
12.5% gray fill
Definition Fill.cs:67
@ MediumGray
Medium gray fill.
Definition Fill.cs:61
@ Gray0625
6.25% gray fill
Definition Fill.cs:65
@ None
No pattern (default).
Definition Fill.cs:55
@ DarkGray
Dark gray fill.
Definition Fill.cs:59
@ Solid
Solid fill (for colors).
Definition Fill.cs:57
@ LightGray
Light gray fill.
Definition Fill.cs:63
string ForegroundColor
Gets or sets the foreground color of the fill. The value is expressed as hex string with the format A...
Definition Fill.cs:103
PatternValue PatternFill
Gets or sets the pattern type of the fill (Default is none).
Definition Fill.cs:124
Fill(string foreground, string background)
Constructor with foreground and background color.
Definition Fill.cs:143
Class providing static methods to parse string values to specific types or to print object as languag...
static string ToUpper(string input)
Transforms a string to upper case with null check and invariant culture.
Class providing general validator methods.
Definition Validators.cs:11
static void ValidateColor(string hexCode, bool useAlpha, bool allowEmpty=false)
Validates the passed string, whether it is a valid RGB or ARGB value that can be used for Fills or Fo...
Definition Validators.cs:25