NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
Border.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 Border : AbstractStyle
18 {
19
20 #region constants
24 public static readonly StyleValue DefaultBorderStyle = StyleValue.None;
25
29 public static readonly string DefaultBorderColor = "";
30
31 #endregion
32
33 #region privateFields
34 private string diagonalColor;
35 private string leftColor;
36 private string rightColor;
37 private string topColor;
38 private string bottomColor;
39 #endregion
40
41
42 #region enums
46 public enum StyleValue
47 {
75#pragma warning disable CA1720 // Suppress: Identifiers should not contain types
77#pragma warning restore CA1720
78 }
79 #endregion
80
81 #region properties
85 [Append]
86 public string BottomColor
87 {
88 get => bottomColor;
89 set
90 {
91 Validators.ValidateColor(value, true, true);
92 if (value != null)
93 {
94 bottomColor = ParserUtils.ToUpper(value);
95 }
96 else
97 {
98 bottomColor = value;
99 }
100 }
101 }
102
105 [Append]
106 public StyleValue BottomStyle { get; set; }
110 [Append]
111 public string DiagonalColor
112 {
113 get => diagonalColor;
114 set
115 {
116 Validators.ValidateColor(value, true, true);
117 if (value != null)
118 {
119 diagonalColor = ParserUtils.ToUpper(value);
120 }
121 else
122 {
123 diagonalColor = value;
124 }
125 }
126 }
127
130 [Append]
131 public bool DiagonalDown { get; set; }
135 [Append]
136 public bool DiagonalUp { get; set; }
140 [Append]
141 public StyleValue DiagonalStyle { get; set; }
145 [Append]
146 public string LeftColor
147 {
148 get => leftColor;
149 set
150 {
151 Validators.ValidateColor(value, true, true);
152 if (value != null)
153 {
154 leftColor = ParserUtils.ToUpper(value);
155 }
156 else
157 {
158 leftColor = value;
159 }
160 }
161 }
162
165 [Append]
166 public StyleValue LeftStyle { get; set; }
170 [Append]
171 public string RightColor
172 {
173 get => rightColor;
174 set
175 {
176 Validators.ValidateColor(value, true, true);
177 if (value != null)
178 {
179 rightColor = ParserUtils.ToUpper(value);
180 }
181 else
182 {
183 rightColor = value;
184 }
185 }
186 }
187
190 [Append]
191 public StyleValue RightStyle { get; set; }
195 [Append]
196 public string TopColor
197 {
198 get => topColor; set
199 {
200 Validators.ValidateColor(value, true, true);
201 if (value != null)
202 {
203 topColor = ParserUtils.ToUpper(value);
204 }
205 else
206 {
207 topColor = value;
208 }
209 }
210 }
211
214 [Append]
215 public StyleValue TopStyle { get; set; }
216 #endregion
217
218 #region constructors
237 #endregion
238
239 #region methods
246 public override int GetHashCode()
247 {
248 unchecked
249 {
250 int hashCode = -153001865;
251 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(BottomColor);
252 hashCode = hashCode * -1521134295 + BottomStyle.GetHashCode();
253 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(DiagonalColor);
254 hashCode = hashCode * -1521134295 + DiagonalDown.GetHashCode();
255 hashCode = hashCode * -1521134295 + DiagonalUp.GetHashCode();
256 hashCode = hashCode * -1521134295 + DiagonalStyle.GetHashCode();
257 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(LeftColor);
258 hashCode = hashCode * -1521134295 + LeftStyle.GetHashCode();
259 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(RightColor);
260 hashCode = hashCode * -1521134295 + RightStyle.GetHashCode();
261 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(TopColor);
262 hashCode = hashCode * -1521134295 + TopStyle.GetHashCode();
263 return hashCode;
264 }
265 }
266
272 public override bool Equals(object obj)
273 {
274 return obj is Border border &&
275 BottomColor == border.BottomColor &&
276 BottomStyle == border.BottomStyle &&
277 DiagonalColor == border.DiagonalColor &&
278 DiagonalDown == border.DiagonalDown &&
279 DiagonalUp == border.DiagonalUp &&
280 DiagonalStyle == border.DiagonalStyle &&
281 LeftColor == border.LeftColor &&
282 LeftStyle == border.LeftStyle &&
283 RightColor == border.RightColor &&
284 RightStyle == border.RightStyle &&
285 TopColor == border.TopColor &&
286 TopStyle == border.TopStyle;
287 }
288
312
318 {
319 return (Border)Copy();
320 }
321
326 public override string ToString()
327 {
328 StringBuilder sb = new StringBuilder();
329 sb.Append("\"Border\": {\n");
330 AddPropertyAsJson(sb, "BottomStyle", BottomStyle);
331 AddPropertyAsJson(sb, "DiagonalColor", DiagonalColor);
332 AddPropertyAsJson(sb, "DiagonalDown", DiagonalDown);
333 AddPropertyAsJson(sb, "DiagonalStyle", DiagonalStyle);
334 AddPropertyAsJson(sb, "DiagonalUp", DiagonalUp);
335 AddPropertyAsJson(sb, "LeftColor", LeftColor);
336 AddPropertyAsJson(sb, "LeftStyle", LeftStyle);
337 AddPropertyAsJson(sb, "RightColor", RightColor);
338 AddPropertyAsJson(sb, "RightStyle", RightStyle);
339 AddPropertyAsJson(sb, "TopColor", TopColor);
340 AddPropertyAsJson(sb, "TopStyle", TopStyle);
341 AddPropertyAsJson(sb, "HashCode", this.GetHashCode(), true);
342 sb.Append("\n}");
343 return sb.ToString();
344 }
345
350 internal bool IsEmpty()
351 {
352 bool state = true;
354 { state = false; }
356 { state = false; }
358 { state = false; }
360 { state = false; }
362 { state = false; }
364 { state = false; }
366 { state = false; }
368 { state = false; }
370 { state = false; }
372 { state = false; }
373 if (DiagonalDown)
374 { state = false; }
375 if (DiagonalUp)
376 { state = false; }
377 return state;
378 }
379 #endregion
380
381 #region staticMethods
387 internal static string GetStyleName(StyleValue style)
388 {
389 string output = "";
390 switch (style)
391 {
392 case StyleValue.Hair: output = "hair"; break;
393 case StyleValue.Dotted: output = "dotted"; break;
394 case StyleValue.DashDotDot: output = "dashDotDot"; break;
395 case StyleValue.DashDot: output = "dashDot"; break;
396 case StyleValue.Dashed: output = "dashed"; break;
397 case StyleValue.Thin: output = "thin"; break;
398 case StyleValue.MediumDashDotDot: output = "mediumDashDotDot"; break;
399 case StyleValue.SlantDashDot: output = "slantDashDot"; break;
400 case StyleValue.MediumDashDot: output = "mediumDashDot"; break;
401 case StyleValue.MediumDashed: output = "mediumDashed"; break;
402 case StyleValue.Medium: output = "medium"; break;
403 case StyleValue.Thick: output = "thick"; break;
404 case StyleValue.Double: output = "double"; break; // Default / none is already handled (ignored)
405 }
406 return output;
407 }
408
414 internal static StyleValue GetStyleEnum(string styleName)
415 {
416 switch (styleName)
417 {
418 case "hair": return StyleValue.Hair;
419 case "dotted": return StyleValue.Dotted;
420 case "dashDotDot": return StyleValue.DashDotDot;
421 case "dashDot": return StyleValue.DashDot;
422 case "dashed": return StyleValue.Dashed;
423 case "thin": return StyleValue.Thin;
424 case "mediumDashDotDot": return StyleValue.MediumDashDotDot;
425 case "slantDashDot": return StyleValue.SlantDashDot;
426 case "mediumDashDot": return StyleValue.MediumDashDot;
427 case "mediumDashed": return StyleValue.MediumDashed;
428 case "medium": return StyleValue.Medium;
429 case "thick": return StyleValue.Thick;
430 case "double": return StyleValue.Double;
431
432 default:
433 return StyleValue.None; // fallback – change to throw if you'd prefer strict handling
434 }
435 }
436 #endregion
437
438 }
439}
Class represents an abstract style component.
override string ToString()
Override toString method.
Definition Border.cs:326
StyleValue DiagonalStyle
Gets or sets the style of the diagonal lines.
Definition Border.cs:141
StyleValue LeftStyle
Gets or sets the style of left cell border.
Definition Border.cs:166
bool DiagonalDown
Gets or sets whether the downwards diagonal line is used. If true, the line is used.
Definition Border.cs:131
static readonly StyleValue DefaultBorderStyle
Default border style as constant.
Definition Border.cs:24
StyleValue RightStyle
Gets or sets the style of right cell border.
Definition Border.cs:191
string RightColor
Gets or sets the color code of the right border. The value is expressed as hex string with the format...
Definition Border.cs:172
static readonly string DefaultBorderColor
Default border color as constant.
Definition Border.cs:29
StyleValue BottomStyle
Gets or sets the style of bottom cell border.
Definition Border.cs:106
string LeftColor
Gets or sets the color code of the left border. The value is expressed as hex string with the format ...
Definition Border.cs:147
StyleValue TopStyle
Gets or sets the style of top cell border.
Definition Border.cs:215
Border()
Default constructor.
Definition Border.cs:222
bool DiagonalUp
Gets or sets whether the upwards diagonal line is used. If true, the line is used.
Definition Border.cs:136
string BottomColor
Gets or sets the color code of the bottom border. The value is expressed as hex string with the forma...
Definition Border.cs:87
string TopColor
Gets or sets the color code of the top border. The value is expressed as hex string with the format A...
Definition Border.cs:197
override int GetHashCode()
Returns a hash code for this instance.
Definition Border.cs:246
StyleValue
Enum for the border style, used by the Border class.
Definition Border.cs:47
@ MediumDashDot
medium dash-dotted border
Definition Border.cs:67
@ MediumDashDotDot
medium-dashed border with double-dots
Definition Border.cs:63
@ MediumDashed
medium dashed border
Definition Border.cs:69
@ SlantDashDot
slant dash-dotted border
Definition Border.cs:65
@ DashDotDot
dashed border with double-dots
Definition Border.cs:55
@ DashDot
dash-dotted border
Definition Border.cs:57
string DiagonalColor
Gets or sets the color code of the diagonal lines. The value is expressed as hex string with the form...
Definition Border.cs:112
Border CopyBorder()
Method to copy the current object to a new one with casting.
Definition Border.cs:317
override AbstractStyle Copy()
Method to copy the current object to a new one without casting.
Definition Border.cs:293
override bool Equals(object obj)
Returns whether two instances are the same.
Definition Border.cs:272
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