NanoXLSX.Core 3.0.0-rc.4
Loading...
Searching...
No Matches
Validators.cs
1using System.Text.RegularExpressions;
3
4
5namespace NanoXLSX.Utils
6{
10 public static class Validators
11 {
12
19 public static void ValidateGenericColor(string hexCode, bool allowEmpty = false)
20 {
21 string argbMessage = ValidateColorInternal(hexCode, true, allowEmpty);
22 string rgbMessage = null;
23 if ( argbMessage != null)
24 {
25 rgbMessage = ValidateColorInternal(hexCode, false, allowEmpty);
26 if (rgbMessage != null)
27 {
28 throw new StyleException(argbMessage);
29 }
30 }
31 }
32
40 public static void ValidateColor(string hexCode, bool useAlpha, bool allowEmpty = false)
41 {
42 string message = ValidateColorInternal(hexCode, useAlpha, allowEmpty);
43 if (message != null)
44 {
45 throw new StyleException(message);
46 }
47 }
48
56 private static string ValidateColorInternal(string hexCode, bool useAlpha, bool allowEmpty)
57 {
58 if (string.IsNullOrEmpty(hexCode))
59 {
60 if (allowEmpty)
61 {
62 return null;
63 }
64 return "The color expression cannot be null or empty";
65 }
66
67 int length = useAlpha ? 8 : 6;
68 if (hexCode.Length != length)
69 {
70 return "The value '" + hexCode + "' is invalid. A valid value must contain " + length + " hex characters";
71 }
72 if (!Regex.IsMatch(hexCode, "[a-fA-F0-9]{6,8}"))
73 {
74 return "The expression '" + hexCode + "' is not a valid hex value";
75 }
76 return null;
77 }
78 }
79}
Class for exceptions regarding Style incidents.
Class providing general validator methods.
Definition Validators.cs:11
static void ValidateGenericColor(string hexCode, bool allowEmpty=false)
Validates the passed string, whether it is a valid RGB or ARGB value that can be used for Fills,...
Definition Validators.cs:19
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,...
Definition Validators.cs:40