Skip to content

Commit add0e76

Browse files
committed
Resolve #275 - Color class with tests and formatting
1 parent 3cfc06a commit add0e76

3 files changed

Lines changed: 118 additions & 1 deletion

File tree

CSF.Screenplay.Selenium/Color.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,72 @@ public override bool Equals(object obj)
125125
}
126126
#endif
127127

128+
/// <summary>
129+
/// Gets a representation of the current instance in the format <c>rgb(RRR, GGG, BBB)</c>.
130+
/// </summary>
131+
/// <remarks>
132+
/// <para>
133+
/// The characters <c>RRR</c>, <c>GGG</c> and <c>BBB</c> in the format example correpsond to three decimal
134+
/// unsigned byte values (0 to 255) which indicate the red, green and blue components of the color respectively.
135+
/// </para>
136+
/// <para>
137+
/// Note that this format omits the alpha (transparency) component of the color. Thus, the resulting string
138+
/// will not be equal to the <see cref="Color"/> value which created it if the alpha component is not equal to 1.
139+
/// The only string-formatting methods which preserve all of the information in the color instance are
140+
/// <see cref="ToRgbaString"/> and the default <see cref="ToString"/> method, which both return equivalent results.
141+
/// </para>
142+
/// </remarks>
143+
/// <returns>An RGB-format string.</returns>
144+
public string ToRgbString() => $"rgb({Red}, {Green}, {Blue})";
145+
146+
/// <summary>
147+
/// Gets a representation of the current instance in the format <c>rgba(RRR, GGG, BBB, A)</c>.
148+
/// </summary>
149+
/// <remarks>
150+
/// <para>
151+
/// The characters <c>RRR</c>, <c>GGG</c> and <c>BBB</c> in the format example correpsond to three decimal
152+
/// unsigned byte values (0 to 255) which indicate the red, green and blue components of the color respectively.
153+
/// The character <c>A</c> corresponds to the alpha (transparency) component of the color. It will be either
154+
/// the numbers 1 or 0, or it will be a decimal number between one and zero.
155+
/// </para>
156+
/// <para>
157+
/// Out of the string-formatting methods available on the <see cref="Color"/> type, only this one and the default
158+
/// <see cref="ToString"/> method preserve all information in the color instance. Thus this method may be used
159+
/// alongside <see cref="Parse"/> or <see cref="TryParse(string, out Color)"/> to reliably round-trip an RGBA
160+
/// string color representation.
161+
/// </para>
162+
/// </remarks>
163+
/// <returns>An RGBA-format string.</returns>
164+
public string ToRgbaString() => $"rgba({Red}, {Green}, {Blue}, {Alpha})";
165+
166+
/// <summary>
167+
/// Gets a representation of the current instance in the format <c>#RRGGBB</c>.
168+
/// </summary>
169+
/// <remarks>
170+
/// <para>
171+
/// The characters <c>RR</c>, <c>GG</c> and <c>BB</c> in the format example correpsond to three hexadecimal
172+
/// unsigned byte values (00 to FF, corresponding to decimal 0 to 255). These indicate the red, green and
173+
/// blue components of the color respectively.
174+
/// </para>
175+
/// <para>
176+
/// Note that this format omits the alpha (transparency) component of the color. Thus, the resulting string
177+
/// will not be equal to the <see cref="Color"/> value which created it if the alpha component is not equal to 1.
178+
/// The only string-formatting methods which preserve all of the information in the color instance are
179+
/// <see cref="ToRgbaString"/> and the default <see cref="ToString"/> method, which both return equivalent results.
180+
/// </para>
181+
/// </remarks>
182+
/// <returns>An RGB-format string.</returns>
183+
public string ToHexString() => $"#{Red:X}{Green:X}{Blue:X}";
184+
185+
/// <summary>
186+
/// Returns a string in the same format as <see cref="ToRgbaString"/>.
187+
/// </summary>
188+
/// <remarks>
189+
/// <para>This method and <see cref="ToRgbaString"/> are equivalent.</para>
190+
/// </remarks>
191+
/// <returns>An RGBA-format string.</returns>
192+
public override string ToString() => ToRgbaString();
193+
128194
/// <inheritdoc/>
129195
public override int GetHashCode() => HashCode.Combine(Red, Green, Blue, Alpha);
130196

Tests/CSF.Screenplay.Selenium.Tests/ColorTests.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using Microsoft.AspNetCore.Mvc;
32

43
namespace CSF.Screenplay.Selenium;
54

@@ -185,4 +184,31 @@ public void OpInequalityShouldReturnTrueForAColorAndASysColorIfTheyAreNotRgbaEqu
185184
{
186185
Assert.That(() => Color.Parse(first) != System.Drawing.Color.FromArgb(r, g, b), Is.True);
187186
}
187+
188+
[TestCase("rgb(30, 128, 55)")]
189+
public void ParseFollowedByToRgbStringShouldRoundtripTheColor(string colorString)
190+
{
191+
Assert.That(() => Color.Parse(colorString).ToRgbString(), Is.EqualTo(colorString));
192+
}
193+
194+
[TestCase("rgba(30, 128, 55, 0.5)")]
195+
[TestCase("rgba(30, 128, 55, 1)")]
196+
[TestCase("rgba(0, 0, 0, 0)")]
197+
public void ParseFollowedByToRgbaStringShouldRoundtripTheColor(string colorString)
198+
{
199+
Assert.That(() => Color.Parse(colorString).ToRgbaString(), Is.EqualTo(colorString));
200+
}
201+
202+
[TestCase("rgba(30, 128, 55, 0.5)")]
203+
[TestCase("rgba(30, 128, 55, 1)")]
204+
[TestCase("rgba(0, 0, 0, 0)")]
205+
public void ParseFollowedByToStringShouldRoundtripTheColor(string colorString)
206+
{
207+
Assert.That(() => Color.Parse(colorString).ToString(), Is.EqualTo(colorString));
208+
}
209+
[TestCase("#E055D8")]
210+
public void ParseFollowedByToHexStringShouldRoundtripTheColor(string colorString)
211+
{
212+
Assert.That(() => Color.Parse(colorString).ToHexString(), Is.EqualTo(colorString));
213+
}
188214
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace CSF.Screenplay.Selenium;
2+
3+
[TestFixture, Parallelizable]
4+
public class ColorsTests
5+
{
6+
[TestCase("GREEN", 0, 128, 0)]
7+
[TestCase("green", 0, 128, 0)]
8+
[TestCase("gReEn", 0, 128, 0)]
9+
public void GetNamedColorShouldReturnAColorIfTheNameIsValid(string colorName, byte r, byte g, byte b)
10+
{
11+
Assert.That(() => Colors.GetNamedColor(colorName), Is.EqualTo(new Color(r, g, b)));
12+
}
13+
14+
[Test]
15+
public void GetNamedColorShouldThrowAneIfTheNameIsNull()
16+
{
17+
Assert.That(() => Colors.GetNamedColor(null), Throws.ArgumentNullException);
18+
}
19+
20+
[Test]
21+
public void GetNamedColorShouldThrowArgExIfTheNameIsInvalid()
22+
{
23+
Assert.That(() => Colors.GetNamedColor("ElephantsAreNotAColor"), Throws.ArgumentException);
24+
}
25+
}

0 commit comments

Comments
 (0)