Skip to content

Commit 68fa08e

Browse files
committed
WIP #275 - Fix some technical issues
1 parent 6cf0917 commit 68fa08e

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

CSF.Screenplay.Selenium/Color.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,13 @@ public readonly double Alpha
7373
init => this.alpha = SanitizeAlpha(value);
7474
}
7575
#else
76-
public double Alpha
77-
{
78-
get => alpha;
79-
private set => alpha = SanitizeAlpha(value);
80-
}
76+
public double Alpha { get => alpha; }
8177
#endif
8278

8379
/// <summary>
8480
/// Gets a representation of <see cref="Alpha"/>, as a byte.
8581
/// </summary>
86-
public byte AlphaAsByte => (byte) Math.Floor(Math.Round(Alpha * 255, 0));
82+
public byte AlphaAsByte => (byte) Math.Floor(Math.Round(Alpha * 255, 0, MidpointRounding.AwayFromZero));
8783

8884
/// <summary>
8985
/// Converts this color to a <see cref="System.Drawing.Color"/>.
@@ -104,7 +100,7 @@ public double Alpha
104100
public bool Equals(Color other) => other.Red == Red
105101
&& other.Green == Green
106102
&& other.Blue == Blue
107-
&& other.Alpha == Alpha;
103+
&& other.AlphaAsByte == AlphaAsByte;
108104

109105
/// <inheritdoc/>
110106
public bool Equals(string other) => TryParse(other, out var color) && Equals(color);

CSF.Screenplay.Selenium/Colors.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class Colors
2020
/// Caches the named colors and their names, for performance reasons when using <see cref="GetNamedColor(string)"/> and/or
2121
/// <see cref="TryGetNamedColor(string, out Color)"/>.
2222
/// </summary>
23-
static readonly ReadOnlyDictionary<string,Color> namedColors;
23+
static readonly ReadOnlyDictionary<string,Color> namedColors = GetNamedColorCache();
2424

2525
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
2626
public static Color TRANSPARENT => new Color(0, 0, 0, 0);
@@ -219,14 +219,21 @@ public static bool TryGetNamedColor(string colorName, out Color color)
219219
}
220220

221221
/// <summary>
222-
/// Static constructor initializes a cache of the named colors.
222+
/// Gets a dictionary of the names/instances of the well-known colors.
223223
/// </summary>
224-
static Colors()
224+
/// <remarks>
225+
/// <para>
226+
/// The keys of this dictionary are case insensitive and correspond to the names of the static properties of this type.
227+
/// The values are instances of the <see cref="Color"/> that the corresponding property returns.
228+
/// </para>
229+
/// </remarks>
230+
/// <returns>A cache of the named color values.</returns>
231+
static ReadOnlyDictionary<string, Color> GetNamedColorCache()
225232
{
226233
var namedColorValues = typeof(Colors)
227234
.GetProperties(BindingFlags.Static)
228235
.ToDictionary(k => k.Name, v => (Color) v.GetValue(null), StringComparer.InvariantCultureIgnoreCase);
229-
namedColors = new ReadOnlyDictionary<string, Color>(namedColorValues);
236+
return new ReadOnlyDictionary<string, Color>(namedColorValues);
230237
}
231238
}
232239
}

0 commit comments

Comments
 (0)