NanoXLSX.Core 3.0.0-rc.5
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 © 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 System.Collections.Generic;
9using System.Text;
10using NanoXLSX.Colors;
12
13namespace NanoXLSX.Styles
14{
18 public class Fill : AbstractStyle
19 {
20 #region constants
32 public static readonly PatternValue DefaultPatternFill = PatternValue.None;
33
34 #endregion
35
36 #region enums
40 public enum FillType
41 {
46 }
47
71 #endregion
72
73 #region privateFields
74 private Color backgroundColor = DefaultColor;
75 private Color foregroundColor = DefaultColor;
76 #endregion
77
78 #region properties
84 [Append]
86 {
87 get => backgroundColor;
88 set
89 {
90 backgroundColor = value;
91 if (PatternFill == PatternValue.None)
92 {
94 }
95 }
96 }
97
102 [Append]
104 {
105 get => foregroundColor;
106 set
107 {
108 foregroundColor = value;
109 if (PatternFill == PatternValue.None)
110 {
112 }
113 }
114 }
115
118 [Append]
119 public PatternValue PatternFill { get; set; }
120 #endregion
121
122 #region constructors
126 public Fill()
127 {
129 foregroundColor = DefaultColor;
130 backgroundColor = DefaultColor;
131 }
132
137 public Fill(string foreground, string background)
138 {
139 BackgroundColor = Color.CreateRgb(background);
140 ForegroundColor = Color.CreateRgb(foreground);
142 }
143
149 public Fill(string value, FillType fillType)
150 {
151 if (fillType == FillType.FillColor)
152 {
153 backgroundColor = DefaultColor;
155 }
156 else
157 {
159 foregroundColor = DefaultColor;
160 }
162 }
163 #endregion
164
165 #region methods
166
171 public override string ToString()
172 {
173 StringBuilder sb = new StringBuilder();
174 sb.Append("\"Fill\": {\n");
175 AddPropertyAsJson(sb, "BackgroundColor", BackgroundColor);
176 AddPropertyAsJson(sb, "ForegroundColor", ForegroundColor);
177 AddPropertyAsJson(sb, "PatternFill", PatternFill);
178 AddPropertyAsJson(sb, "HashCode", this.GetHashCode(), true);
179 sb.Append("\n}");
180 return sb.ToString();
181 }
182
187 public override AbstractStyle Copy()
188 {
189 Fill copy = new Fill
190 {
194 };
195 return copy;
196 }
197
204 public override int GetHashCode()
205 {
206 unchecked
207 {
208 int hashCode = -1564173520;
209 hashCode = hashCode * -1521134295 + EqualityComparer<Color>.Default.GetHashCode(BackgroundColor);
210 hashCode = hashCode * -1521134295 + EqualityComparer<Color>.Default.GetHashCode(ForegroundColor);
211 hashCode = hashCode * -1521134295 + PatternFill.GetHashCode();
212 return hashCode;
213 }
214 }
215
221 public override bool Equals(object obj)
222 {
223 return obj is Fill fill &&
224 BackgroundColor == fill.BackgroundColor &&
225 ForegroundColor == fill.ForegroundColor &&
226 PatternFill == fill.PatternFill;
227 }
228
233 public Fill CopyFill()
234 {
235 return (Fill)Copy();
236 }
237
243 public void SetColor(string value, FillType fillType)
244 {
245 if (fillType == FillType.FillColor)
246 {
247 backgroundColor = DefaultColor;
249 }
250 else
251 {
253 foregroundColor = DefaultColor;
254 }
256 }
257
263 public void SetColor(Color value, FillType fillType)
264 {
265 if (fillType == FillType.FillColor)
266 {
267 backgroundColor = DefaultColor;
268 ForegroundColor = value;
269 }
270 else
271 {
272 BackgroundColor = value;
273 foregroundColor = DefaultColor;
274 }
276 }
277
283 public void SetColor(IColor value, FillType fillType)
284 {
285 SetColor(GetColorByComponent(value), fillType);
286 }
287 #endregion
288
289 #region staticMethods
294 public static implicit operator Fill(string value)
295 {
296 return new Fill(value, FillType.FillColor);
297 }
298
303 public static implicit operator Fill(IndexedColor.Value index)
304 {
305 Fill fill = new Fill();
306 fill.PatternFill = PatternValue.Solid;
307 fill.ForegroundColor = Color.CreateIndexed(index);
308 return fill;
309 }
310
315 public static implicit operator Fill(int index)
316 {
317 Fill fill = new Fill();
318 fill.PatternFill = PatternValue.Solid;
319 fill.ForegroundColor = Color.CreateIndexed(index);
320 return fill;
321 }
322
323
329 internal static string GetPatternName(PatternValue pattern)
330 {
331 string output;
332 switch (pattern)
333 {
334 case PatternValue.Solid:
335 output = "solid";
336 break;
337 case PatternValue.DarkGray:
338 output = "darkGray";
339 break;
340 case PatternValue.MediumGray:
341 output = "mediumGray";
342 break;
343 case PatternValue.LightGray:
344 output = "lightGray";
345 break;
346 case PatternValue.Gray0625:
347 output = "gray0625";
348 break;
349 case PatternValue.Gray125:
350 output = "gray125";
351 break;
352 default:
353 output = "none";
354 break;
355 }
356 return output;
357 }
358
362 internal static PatternValue GetPatternEnum(string name)
363 {
364 switch (name)
365 {
366 case "none": return PatternValue.None;
367 case "solid": return PatternValue.Solid;
368 case "darkGray": return PatternValue.DarkGray;
369 case "mediumGray": return PatternValue.MediumGray;
370 case "lightGray": return PatternValue.LightGray;
371 case "gray0625": return PatternValue.Gray0625;
372 case "gray125": return PatternValue.Gray125;
373 default:
374 return PatternValue.None;
375 }
376 }
377
383 private static Color GetColorByComponent(IColor component)
384 {
385 if (component is SrgbColor)
386 {
387 return Color.CreateRgb((SrgbColor)component);
388 }
389 else if (component is IndexedColor)
390 {
391 return Color.CreateIndexed((IndexedColor)component);
392 }
393 else if (component is ThemeColor)
394 {
395 return Color.CreateTheme((ThemeColor)component);
396 }
397 else if (component is SystemColor)
398 {
399 return Color.CreateSystem((SystemColor)component);
400 }
401 else // AutoColor
402 {
403 return Color.CreateAuto();
404 }
405 }
406 #endregion
407
408 }
409}
Compound class representing a color in various representations (RGB, indexed, theme,...
Definition Color.cs:20
static Color CreateIndexed(IndexedColor color)
Creates an Color from an indexed color.
Definition Color.cs:202
static Color CreateRgb(SrgbColor color)
Creates an Color from an RGB/ARGB color.
Definition Color.cs:171
Class representing an indexed color from the legacy OOXML / Excel indexed color palette.
Value
Legacy OOXML / Excel indexed color palette.
const Value DefaultIndexedColor
Default indexed color (system foreground color).
Class representing a generic sRGB color (with or without alpha channel).
Definition SrgbColor.cs:19
const string DefaultSrgbColor
Default color value (opaque black: #000000).
Definition SrgbColor.cs:24
Class represents an abstract style component.
FillType
Enum for the type of the color, used by the Fill class.
Definition Fill.cs:41
@ PatternColor
Color defines a pattern color.
Definition Fill.cs:43
@ FillColor
Color defines a solid fill color.
Definition Fill.cs:45
static readonly Color DefaultIndexedColor
Default index color.
Definition Fill.cs:28
Fill CopyFill()
Method to copy the current object to a new one with casting.
Definition Fill.cs:233
override bool Equals(object obj)
Returns whether two instances are the same.
Definition Fill.cs:221
Color BackgroundColor
Gets or sets the background color of the fill. The value is expressed as hex string with the format A...
Definition Fill.cs:86
Fill()
Default constructor.
Definition Fill.cs:126
static readonly PatternValue DefaultPatternFill
Default pattern.
Definition Fill.cs:32
static readonly Color DefaultColor
Default Color (foreground or background).
Definition Fill.cs:24
override int GetHashCode()
Returns a hash code for this instance.
Definition Fill.cs:204
void SetColor(string value, FillType fillType)
Sets the color depending on fill type, using a sRGB value (without alpha).
Definition Fill.cs:243
void SetColor(Color value, FillType fillType)
Sets the color depending on fill type, using a color object of the type Color.
Definition Fill.cs:263
override string ToString()
Override toString method.
Definition Fill.cs:171
void SetColor(IColor value, FillType fillType)
Sets the color depending on fill type, using a color object, deriving from IColor.
Definition Fill.cs:283
Fill(string value, FillType fillType)
Constructor with color value as sRGB and fill type.
Definition Fill.cs:149
override AbstractStyle Copy()
Method to copy the current object to a new one without casting.
Definition Fill.cs:187
PatternValue
Enum for the pattern values, used by the Fill class.
Definition Fill.cs:52
@ Gray125
12.5% gray fill
Definition Fill.cs:69
@ MediumGray
Medium gray fill.
Definition Fill.cs:63
@ Gray0625
6.25% gray fill
Definition Fill.cs:67
@ None
No pattern (default).
Definition Fill.cs:57
@ DarkGray
Dark gray fill.
Definition Fill.cs:61
@ Solid
Solid fill (for colors).
Definition Fill.cs:59
@ LightGray
Light gray fill.
Definition Fill.cs:65
Color ForegroundColor
Gets or sets the foreground color of the fill. The value is expressed as hex string with the format A...
Definition Fill.cs:104
PatternValue PatternFill
Gets or sets the pattern type of the fill (Default is none).
Definition Fill.cs:119
Fill(string foreground, string background)
Constructor with foreground and background color as sRGB values (without alpha).
Definition Fill.cs:137
Interface to represent non typed color, either defined by the system or the user.
Definition IColor.cs:14