-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorParser.cs
More file actions
243 lines (215 loc) · 9.79 KB
/
Copy pathColorParser.cs
File metadata and controls
243 lines (215 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Text.RegularExpressions;
namespace CSF.Screenplay.Selenium
{
/// <summary>
/// An object which can get an instance of <see cref="Color"/>.
/// </summary>
internal interface IParsesColor
{
/// <summary>
/// Attempts to parse an instance of <see cref="Color"/> from the specified <paramref name="colorValue"/>.
/// </summary>
/// <remarks>
/// <para>
/// If the parsing is successful then this method will return <see langword="true"/> and the <paramref name="color"/>
/// will contain the parsed color value.
/// Otherwise it will return <see langword="false"/> and the <paramref name="color"/> parameter must be ignored.
/// </para>
/// </remarks>
/// <param name="colorValue">A string representing a color.</param>
/// <param name="color">If this method returns <see langword="true"/> then this parameter exposes the parsed
/// color; if not then this value must be ignored.</param>
/// <returns><see langword="true"/> if the parsing was a success; <see langword="false"/> if not.</returns>
bool TryParseColor(string colorValue, out Color color);
}
internal abstract class NumericColorParser : IParsesColor
{
protected static readonly string
BytePart = @"\s*(\d{1,3})\s*",
DecimalAlphaPart = @"\s*(0|1|0\.\d+)\s*",
PercentDecimalPart = @"\s*(\d{1,3}|\d{1,2}\.\d+)%\s*",
PercentPart = @"\s*(\d{1,3})%\s*",
Hex2Part = @"([0-9a-f]{2})",
Hex1Part = @"([0-9a-f])";
/// <inheritdoc/>
public virtual bool TryParseColor(string colorValue, out Color color)
{
color = default;
var match = GetParsingRegex(out var includesAlpha).Match(colorValue);
if(!match.Success) return false;
color = new Color(GetByte(match.Groups[1]),
GetByte(match.Groups[2]),
GetByte(match.Groups[3]),
includesAlpha ? double.Parse(match.Groups[4].Value) : 1);
return true;
}
/// <summary>
/// Gets a regex which parses a string color representation.
/// </summary>
/// <param name="includesAlpha"><see langword="true"/> if this regex includes a value for the alpha channel; <see langword="false"/> if not.</param>
/// <returns>A regex</returns>
protected virtual Regex GetParsingRegex(out bool includesAlpha)
=> new Regex(GetParsingPattern(out includesAlpha),
RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase,
TimeSpan.FromMilliseconds(500));
/// <summary>
/// Gets a regex pattern which parses a string color representation.
/// </summary>
/// <param name="includesAlpha"><see langword="true"/> if this regex includes a value for the alpha channel; <see langword="false"/> if not.</param>
/// <returns>A regex</returns>
protected abstract string GetParsingPattern(out bool includesAlpha);
/// <summary>
/// Parses a byte value from the value of the match group.
/// </summary>
/// <param name="matchGroup">A regex match group</param>
/// <returns>A byte value</returns>
protected virtual byte GetByte(Group matchGroup) => byte.Parse(matchGroup.Value);
}
internal class RgbColorParser : NumericColorParser
{
/// <inheritdoc/>
protected override string GetParsingPattern(out bool includesAlpha)
{
includesAlpha = false;
return $"^\\s*rgb\\({BytePart},{BytePart},{BytePart}\\)\\s*$";
}
}
internal class RgbaColorParser : NumericColorParser
{
/// <inheritdoc/>
protected override string GetParsingPattern(out bool includesAlpha)
{
includesAlpha = true;
return $"^\\s*rgba\\({BytePart},{BytePart},{BytePart},{DecimalAlphaPart}\\)\\s*$";
}
}
internal class RgbPercentageColorParser : NumericColorParser
{
/// <inheritdoc/>
protected override string GetParsingPattern(out bool includesAlpha)
{
includesAlpha = false;
return $"^\\s*rgb\\({PercentDecimalPart},{PercentDecimalPart},{PercentDecimalPart}\\)\\s*$";
}
/// <inheritdoc/>
protected override byte GetByte(Group matchGroup) => (byte) (double.Parse(matchGroup.Value) / 100D * 255D);
}
internal class RgbaPercentageColorParser : RgbPercentageColorParser
{
/// <inheritdoc/>
protected override string GetParsingPattern(out bool includesAlpha)
{
includesAlpha = true;
return $"^\\s*rgba\\({PercentDecimalPart},{PercentDecimalPart},{PercentDecimalPart},{DecimalAlphaPart}\\)\\s*$";
}
}
internal class Hex3ColorParser : NumericColorParser
{
/// <inheritdoc/>
protected override string GetParsingPattern(out bool includesAlpha)
{
includesAlpha = false;
return $"^\\s*#{Hex1Part}{Hex1Part}{Hex1Part}\\s*$";
}
/// <inheritdoc/>
protected override byte GetByte(Group matchGroup) => Convert.ToByte(matchGroup.Value + matchGroup.Value, 16);
}
internal class Hex6ColorParser : NumericColorParser
{
/// <inheritdoc/>
protected override string GetParsingPattern(out bool includesAlpha)
{
includesAlpha = false;
return $"^\\s*#{Hex2Part}{Hex2Part}{Hex2Part}\\s*$";
}
/// <inheritdoc/>
protected override byte GetByte(Group matchGroup) => Convert.ToByte(matchGroup.Value, 16);
}
internal class HslColorParser : NumericColorParser
{
/// <inheritdoc/>
public override bool TryParseColor(string colorValue, out Color color)
{
color = default;
var match = GetParsingRegex(out var includesAlpha).Match(colorValue);
if(!match.Success) return false;
color = HslToRgb(short.Parse(match.Groups[1].Value),
byte.Parse(match.Groups[2].Value),
byte.Parse(match.Groups[3].Value),
includesAlpha ? double.Parse(match.Groups[4].Value) : 1);
return true;
}
/// <inheritdoc/>
protected override string GetParsingPattern(out bool includesAlpha)
{
includesAlpha = false;
return $"^\\s*hsl\\({BytePart},{PercentPart},{PercentPart}\\)\\s*$";
}
/// <summary>
/// Converts an HSL color to the sRGB color space.
/// </summary>
/// <remarks>
/// <para>
/// This is based upon the algorithms available at
/// <see href="https://github.com/SeleniumHQ/selenium/blob/selenium-4.40.0/java/src/org/openqa/selenium/support/Color.java#L258">The
/// Selenium 4.40.0 HSL converter for Java</see>.
/// </para>
/// </remarks>
/// <param name="h">The hue value in degrees (0 to 360)</param>
/// <param name="s">The saturation percentage (0 to 100)</param>
/// <param name="l">The lightness percentage (0 to 100)</param>
/// <param name="alpha">The alpha (transparency) value (0 to 1)</param>
/// <returns>An instance of <see cref="Color"/> in the sRGB color space</returns>
static Color HslToRgb(short h, byte s, byte l, double alpha)
{
var hue = h / 360D;
var saturation = s / 100D;
var lightness = l / 100D;
// FYI: Saturation = zero means gray, making hue irrelevant
if(s == 0)
{
var allChannels = (byte) Math.Round(lightness * 255D, MidpointRounding.AwayFromZero);
return new Color(allChannels, allChannels, allChannels, alpha);
}
var luminosity2 = (lightness < 0.5)
? lightness * (saturation + 1D)
: lightness + saturation - lightness * saturation;
var luminosity1 = 2D * lightness - luminosity2;
return new Color(HueValueToRgbByte(luminosity1, luminosity2, hue + 1D / 3D),
HueValueToRgbByte(luminosity1, luminosity2, hue),
HueValueToRgbByte(luminosity1, luminosity2, hue - 1D / 3D),
alpha);
}
static byte HueValueToRgbByte(double luminosity1,
double luminosity2,
double hueValue)
=> (byte) Math.Round(HueValueToRgb(luminosity1, luminosity2, hueValue) * 255D, MidpointRounding.AwayFromZero);
static double HueValueToRgb(double luminosity1,
double luminosity2,
double hueValue)
{
if (hueValue < 0D) hueValue += 1;
if (hueValue > 1D) hueValue -= 1.0;
if (hueValue < 1D / 6D) return luminosity1 + (luminosity2 - luminosity1) * 6D * hueValue;
if (hueValue < 1D / 2D) return luminosity2;
if (hueValue < 2D / 3D) return luminosity1 + (luminosity2 - luminosity1) * (2D / 3D - hueValue) * 6D;
return luminosity1;
}
}
internal class HslaColorParser : HslColorParser
{
/// <inheritdoc/>
protected override string GetParsingPattern(out bool includesAlpha)
{
includesAlpha = true;
return $"^\\s*hsla\\({BytePart},{PercentPart},{PercentPart},{DecimalAlphaPart}\\)\\s*$";
}
}
internal class NamedColorParser : IParsesColor
{
/// <inheritdoc/>
public bool TryParseColor(string colorValue, out Color color)
=> Colors.TryGetNamedColor(colorValue, out color);
}
}