NanoXLSX.Core 3.0.0-rc.4
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.ComponentModel;
10using System.Text;
11using NanoXLSX.Colors;
13
14namespace NanoXLSX.Styles
15{
19 public class Fill : AbstractStyle
20 {
21 #region constants
33 public static readonly PatternValue DefaultPatternFill = PatternValue.None;
34
35 #endregion
36
37 #region enums
41 public enum FillType
42 {
47 }
48
72 #endregion
73
74 #region privateFields
75 private Color backgroundColor = DefaultColor;
76 private Color foregroundColor = DefaultColor;
77 #endregion
78
79 #region properties
85 [Append]
87 {
88 get => backgroundColor;
89 set
90 {
91 backgroundColor = value;
92 if (PatternFill == PatternValue.None)
93 {
95 }
96 }
97 }
98
103 [Append]
105 {
106 get => foregroundColor;
107 set
108 {
109 foregroundColor = value;
110 if (PatternFill == PatternValue.None)
111 {
113 }
114 }
115 }
116
119 [Append]
120 public PatternValue PatternFill { get; set; }
121 #endregion
122
123 #region constructors
127 public Fill()
128 {
130 foregroundColor = DefaultColor;
131 backgroundColor = DefaultColor;
132 }
133
138 public Fill(string foreground, string background)
139 {
140 BackgroundColor = Color.CreateRgb(background);
141 ForegroundColor = Color.CreateRgb(foreground);
143 }
144
150 public Fill(string value, FillType fillType)
151 {
152 if (fillType == FillType.FillColor)
153 {
154 backgroundColor = DefaultColor;
156 }
157 else
158 {
160 foregroundColor = DefaultColor;
161 }
163 }
164 #endregion
165
166 #region methods
167
172 public override string ToString()
173 {
174 StringBuilder sb = new StringBuilder();
175 sb.Append("\"Fill\": {\n");
176 AddPropertyAsJson(sb, "BackgroundColor", BackgroundColor);
177 AddPropertyAsJson(sb, "ForegroundColor", ForegroundColor);
178 AddPropertyAsJson(sb, "PatternFill", PatternFill);
179 AddPropertyAsJson(sb, "HashCode", this.GetHashCode(), true);
180 sb.Append("\n}");
181 return sb.ToString();
182 }
183
188 public override AbstractStyle Copy()
189 {
190 Fill copy = new Fill
191 {
195 };
196 return copy;
197 }
198
205 public override int GetHashCode()
206 {
207 unchecked
208 {
209 int hashCode = -1564173520;
210 hashCode = hashCode * -1521134295 + EqualityComparer<Color>.Default.GetHashCode(BackgroundColor);
211 hashCode = hashCode * -1521134295 + EqualityComparer<Color>.Default.GetHashCode(ForegroundColor);
212 hashCode = hashCode * -1521134295 + PatternFill.GetHashCode();
213 return hashCode;
214 }
215 }
216
222 public override bool Equals(object obj)
223 {
224 return obj is Fill fill &&
225 BackgroundColor == fill.BackgroundColor &&
226 ForegroundColor == fill.ForegroundColor &&
227 PatternFill == fill.PatternFill;
228 }
229
234 public Fill CopyFill()
235 {
236 return (Fill)Copy();
237 }
238
244 public void SetColor(string value, FillType fillType)
245 {
246 if (fillType == FillType.FillColor)
247 {
248 backgroundColor = DefaultColor;
250 }
251 else
252 {
254 foregroundColor = DefaultColor;
255 }
257 }
258
264 public void SetColor(Color value, FillType fillType)
265 {
266 if (fillType == FillType.FillColor)
267 {
268 backgroundColor = DefaultColor;
269 ForegroundColor = value;
270 }
271 else
272 {
273 BackgroundColor = value;
274 foregroundColor = DefaultColor;
275 }
277 }
278
284 public void SetColor(IColor value, FillType fillType)
285 {
286 SetColor(GetColorByComponent(value), fillType);
287 }
288 #endregion
289
290 #region staticMethods
295 public static implicit operator Fill(string value)
296 {
297 return new Fill(value, FillType.FillColor);
298 }
299
304 public static implicit operator Fill(IndexedColor.Value index)
305 {
306 Fill fill = new Fill();
307 fill.PatternFill = PatternValue.Solid;
308 fill.ForegroundColor = Color.CreateIndexed(index);
309 return fill;
310 }
311
316 public static implicit operator Fill(int index)
317 {
318 Fill fill = new Fill();
319 fill.PatternFill = PatternValue.Solid;
320 fill.ForegroundColor = Color.CreateIndexed(index);
321 return fill;
322 }
323
324
330 internal static string GetPatternName(PatternValue pattern)
331 {
332 string output;
333 switch (pattern)
334 {
335 case PatternValue.Solid:
336 output = "solid";
337 break;
338 case PatternValue.DarkGray:
339 output = "darkGray";
340 break;
341 case PatternValue.MediumGray:
342 output = "mediumGray";
343 break;
344 case PatternValue.LightGray:
345 output = "lightGray";
346 break;
347 case PatternValue.Gray0625:
348 output = "gray0625";
349 break;
350 case PatternValue.Gray125:
351 output = "gray125";
352 break;
353 default:
354 output = "none";
355 break;
356 }
357 return output;
358 }
359
363 internal static PatternValue GetPatternEnum(string name)
364 {
365 switch (name)
366 {
367 case "none": return PatternValue.None;
368 case "solid": return PatternValue.Solid;
369 case "darkGray": return PatternValue.DarkGray;
370 case "mediumGray": return PatternValue.MediumGray;
371 case "lightGray": return PatternValue.LightGray;
372 case "gray0625": return PatternValue.Gray0625;
373 case "gray125": return PatternValue.Gray125;
374 default:
375 return PatternValue.None;
376 }
377 }
378
384 private static Color GetColorByComponent(IColor component)
385 {
386 if (component is SrgbColor)
387 {
388 return Color.CreateRgb((SrgbColor)component);
389 }
390 else if (component is IndexedColor)
391 {
392 return Color.CreateIndexed((IndexedColor)component);
393 }
394 else if (component is ThemeColor)
395 {
396 return Color.CreateTheme((ThemeColor)component);
397 }
398 else if (component is SystemColor)
399 {
400 return Color.CreateSystem((SystemColor)component);
401 }
402 else // AutoColor
403 {
404 return Color.CreateAuto();
405 }
406 }
407 #endregion
408
409 }
410}
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:20
const string DefaultSrgbColor
Default color value (opaque black: #000000).
Definition SrgbColor.cs:25
Class represents an abstract style component.
FillType
Enum for the type of the color, used by the Fill class.
Definition Fill.cs:42
@ PatternColor
Color defines a pattern color.
Definition Fill.cs:44
@ FillColor
Color defines a solid fill color.
Definition Fill.cs:46
static readonly Color DefaultIndexedColor
Default index color.
Definition Fill.cs:29
Fill CopyFill()
Method to copy the current object to a new one with casting.
Definition Fill.cs:234
override bool Equals(object obj)
Returns whether two instances are the same.
Definition Fill.cs:222
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:87
Fill()
Default constructor.
Definition Fill.cs:127
static readonly PatternValue DefaultPatternFill
Default pattern.
Definition Fill.cs:33
static readonly Color DefaultColor
Default Color (foreground or background).
Definition Fill.cs:25
override int GetHashCode()
Returns a hash code for this instance.
Definition Fill.cs:205
void SetColor(string value, FillType fillType)
Sets the color depending on fill type, using a sRGB value (without alpha).
Definition Fill.cs:244
void SetColor(Color value, FillType fillType)
Sets the color depending on fill type, using a color object of the type Color.
Definition Fill.cs:264
override string ToString()
Override toString method.
Definition Fill.cs:172
void SetColor(IColor value, FillType fillType)
Sets the color depending on fill type, using a color object, deriving from IColor.
Definition Fill.cs:284
Fill(string value, FillType fillType)
Constructor with color value as sRGB and fill type.
Definition Fill.cs:150
override AbstractStyle Copy()
Method to copy the current object to a new one without casting.
Definition Fill.cs:188
PatternValue
Enum for the pattern values, used by the Fill class.
Definition Fill.cs:53
@ Gray125
12.5% gray fill
Definition Fill.cs:70
@ MediumGray
Medium gray fill.
Definition Fill.cs:64
@ Gray0625
6.25% gray fill
Definition Fill.cs:68
@ None
No pattern (default).
Definition Fill.cs:58
@ DarkGray
Dark gray fill.
Definition Fill.cs:62
@ Solid
Solid fill (for colors).
Definition Fill.cs:60
@ LightGray
Light gray fill.
Definition Fill.cs:66
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:105
PatternValue PatternFill
Gets or sets the pattern type of the fill (Default is none).
Definition Fill.cs:120
Fill(string foreground, string background)
Constructor with foreground and background color as sRGB values (without alpha).
Definition Fill.cs:138
Interface to represent non typed color, either defined by the system or the user.
Definition IColor.cs:14