Struct Color
- Namespace
- CSF.Screenplay.Selenium
- Assembly
- CSF.Screenplay.Selenium.dll
Immutable type represents a color on the web, in a manner which may be converted and compared between web-supported formats.
public struct Color : IEquatable<Color>, IEquatable<Color>, IEquatable<string>
- Implements
- Inherited Members
Remarks
An uninitialized instance of this type will represent "fully-transparent black". This is equivalent to the value available at TRANSPARENT. Internally, this type stores color as three unsigned byte values (Red, Green, Blue) and a double representing the transparency (Alpha). The Alpha value must be between 0 (fully transparent) and 1 (fully opaque).
For a list of the well-known named colors, such as green or rosybrown, see the Colors class.
This has static properties to get instances of Color for each of these.
It also has methods to get a color based upon a string color name.
This struct contains functionality to compare equality with, and convert to/from the .NET built-in Color. It also contains parsing logic to parse/format an instance from/to string representations of color which are used by web browsers. See the MDN writeup of web color for more information about the valid formats.
When using a sufficiently high version of .NET & the C# language (.NET 6 or higher), this type is a readonly record struct.
This offers improved and extended functionality, such as access to
non-destructive
mutation using the with keyword. In lower .NET/C# versions, this type is a plain struct with functionality
supported by that language version.
Constructors
Color(byte, byte, byte, double)
Creates a new instance of Color from the specified RGBA values.
public Color(byte red, byte green, byte blue, double alpha = 1)
Parameters
redbyteThe red component in the sRGB color space
greenbyteThe green component in the sRGB color space
bluebyteThe blue component in the sRGB color space
alphadoubleThe alpha (transparency) component, which must be between zero and one.
Remarks
The byte data type for the R, G and B components of the color ensure that it is impossible to specify
values outside the permitted range. As for the alpha, if a value of less than zero is specified
here then it is treated as zero. Likewise if a value of greater than one is specified then it will be treated as one.
Properties
Alpha
Gets the alpha (transparency) component in the sRGB color space.
public double Alpha { get; }
Property Value
Remarks
The value of this property will always be between zero (fully transparent) and one (fully opaque).
AlphaAsByte
Gets a representation of Alpha, as a byte.
public byte AlphaAsByte { get; }
Property Value
Blue
Gets the blue component in the sRGB color space.
public byte Blue { get; }
Property Value
Green
Gets the green component in the sRGB color space.
public byte Green { get; }
Property Value
Red
Gets the red component in the sRGB color space.
public byte Red { get; }
Property Value
Methods
Equals(Color)
public bool Equals(Color other)
Parameters
otherColor
Returns
Equals(Color)
public bool Equals(Color other)
Parameters
otherColor
Returns
Equals(object)
public override bool Equals(object obj)
Parameters
objobject
Returns
Equals(string)
public bool Equals(string other)
Parameters
otherstring
Returns
FromSystemDrawingColour(Color)
public static Color FromSystemDrawingColour(Color color)
Parameters
colorColor
Returns
Remarks
In some situations, it might be useful to convert to/from the .NET built-in Color type. Use this method and ToSystemDrawingColor() to do so.
GetHashCode()
public override int GetHashCode()
Returns
Parse(string)
Parses a Color from a string representation of a color.
public static Color Parse(string webColorValue)
Parameters
webColorValuestringA string containing a color value which is compatible with the web. See the MDN writeup on web colors for more information.
Returns
- Color
The parsed color.
Remarks
If webColorValue is a valid representation of a web-compatible color value then this method will
return that color as an instance of Color. If not, this method will throw an exception.
If you are not certain that the webColorValue represents a valid colour then use
TryParse(string, out Color) instead.
Exceptions
- ArgumentNullException
If
webColorValueis null.- FormatException
If
webColorValueis not a recognized representation of a color.
ToHexString()
Gets a representation of the current instance in the format #RRGGBB.
public string ToHexString()
Returns
- string
An RGB-format string.
Remarks
The characters RR, GG and BB in the format example correpsond to three hexadecimal
unsigned byte values (00 to FF, corresponding to decimal 0 to 255). These indicate the red, green and
blue components of the color respectively.
Note that this format omits the alpha (transparency) component of the color. Thus, the resulting string will not be equal to the Color value which created it if the alpha component is not equal to 1. The only string-formatting methods which preserve all of the information in the color instance are ToRgbaString() and the default ToString() method, which both return equivalent results.
ToRgbString()
Gets a representation of the current instance in the format rgb(RRR, GGG, BBB).
public string ToRgbString()
Returns
- string
An RGB-format string.
Remarks
The characters RRR, GGG and BBB in the format example correpsond to three decimal
unsigned byte values (0 to 255) which indicate the red, green and blue components of the color respectively.
Note that this format omits the alpha (transparency) component of the color. Thus, the resulting string will not be equal to the Color value which created it if the alpha component is not equal to 1. The only string-formatting methods which preserve all of the information in the color instance are ToRgbaString() and the default ToString() method, which both return equivalent results.
ToRgbaString()
Gets a representation of the current instance in the format rgba(RRR, GGG, BBB, A).
public string ToRgbaString()
Returns
- string
An RGBA-format string.
Remarks
The characters RRR, GGG and BBB in the format example correpsond to three decimal
unsigned byte values (0 to 255) which indicate the red, green and blue components of the color respectively.
The character A corresponds to the alpha (transparency) component of the color. It will be either
the numbers 1 or 0, or it will be a decimal number between one and zero.
Out of the string-formatting methods available on the Color type, only this one and the default ToString() method preserve all information in the color instance. Thus this method may be used alongside Parse(string) or TryParse(string, out Color) to reliably round-trip an RGBA string color representation.
ToString()
Returns a string in the same format as ToRgbaString().
public override string ToString()
Returns
- string
An RGBA-format string.
Remarks
This method and ToRgbaString() are equivalent.
ToSystemDrawingColor()
Converts this color to a Color.
public Color ToSystemDrawingColor()
Returns
Remarks
In some situations, it might be useful to convert to/from the .NET built-in Color type. Use this method and FromSystemDrawingColour(Color) to do so.
TryParse(string, out Color)
Gets a value indicating whether the specified webColorValue is a valid string representation
for a Color. If this returns true then exposes the parsed Color
value.
public static bool TryParse(string webColorValue, out Color color)
Parameters
webColorValuestringA string containing a color value which is compatible with the web. See the MDN writeup on web colors for more information.
colorColorIf this method returns true, then this parameter exposes the parsed color value. If this method returns false then the value of this parameter must be ignored.
Returns
- bool
true if parsing is successful (the
webColorValuecontains a valid & recognized color representation); false if not
Remarks
If this method returns true then the output parameter color will contain
the color parsed from the specified string. If this method returns false then the output parameter
has an undefined value and must not be used.
This method will not raise an exception and it is safe to use with unknown strings, including those which might be null.
Operators
operator ==(Color, Color)
Gets a value indicating whether the two specified Color instances are equal.
public static bool operator ==(Color first, Color second)
Parameters
Returns
Remarks
Two colors are equal if they represent the same sRGB color.
operator ==(Color, Color)
public static bool operator ==(Color color, Color sysColor)
Parameters
Returns
Remarks
Despite the type differences, two colors are equal if they represent the same sRGB color.
operator ==(Color, string)
public static bool operator ==(Color color, string stringColor)
Parameters
Returns
- bool
true if the current color and the string color representation indicate the same sRGB color; false if not.
Remarks
A color is equal to a string if the result of TryParse(string, out Color) is true and if the resulting parsed Color instance is equal to the current color. Note that this means that a color is never equal to a string which is not a valid color representation, or which is null.
operator ==(Color, Color)
public static bool operator ==(Color sysColor, Color color)
Parameters
Returns
Remarks
Despite the type differences, two colors are equal if they represent the same sRGB color.
operator ==(string, Color)
public static bool operator ==(string stringColor, Color color)
Parameters
Returns
- bool
true if the current color and the string color representation indicate the same sRGB color; false if not.
Remarks
A color is equal to a string if the result of TryParse(string, out Color) is true and if the resulting parsed Color instance is equal to the current color. Note that this means that a color is never equal to a string which is not a valid color representation, or which is null.
operator !=(Color, Color)
Gets a value indicating whether the two specified Color instances are not equal.
public static bool operator !=(Color first, Color second)
Parameters
Returns
Remarks
Two colors are equal if they represent the same sRGB color.
operator !=(Color, Color)
public static bool operator !=(Color color, Color sysColor)
Parameters
Returns
Remarks
Despite the type differences, two colors are equal if they represent the same sRGB color.
operator !=(Color, string)
Gets a value indicating whether the specified Color and string representation of a color are not equal.
public static bool operator !=(Color color, string stringColor)
Parameters
Returns
- bool
false if the current color and the string color representation indicate the same sRGB color; true if not.
Remarks
A color is equal to a string if the result of TryParse(string, out Color) is true and if the resulting parsed Color instance is equal to the current color. Note that this means that a color is never equal to a string which is not a valid color representation, or which is null.
operator !=(Color, Color)
public static bool operator !=(Color sysColor, Color color)
Parameters
Returns
Remarks
Despite the type differences, two colors are equal if they represent the same sRGB color.
operator !=(string, Color)
Gets a value indicating whether the specified Color and string representation of a color are not equal.
public static bool operator !=(string stringColor, Color color)
Parameters
Returns
- bool
false if the current color and the string color representation indicate the same sRGB color; true if not.
Remarks
A color is equal to a string if the result of TryParse(string, out Color) is true and if the resulting parsed Color instance is equal to the current color. Note that this means that a color is never equal to a string which is not a valid color representation, or which is null.