Table of Contents

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

red byte

The red component in the sRGB color space

green byte

The green component in the sRGB color space

blue byte

The blue component in the sRGB color space

alpha double

The 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

double

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

byte

Blue

Gets the blue component in the sRGB color space.

public byte Blue { get; }

Property Value

byte

Green

Gets the green component in the sRGB color space.

public byte Green { get; }

Property Value

byte

Red

Gets the red component in the sRGB color space.

public byte Red { get; }

Property Value

byte

Methods

Equals(Color)

public bool Equals(Color other)

Parameters

other Color

Returns

bool

Equals(Color)

public bool Equals(Color other)

Parameters

other Color

Returns

bool

Equals(object)

public override bool Equals(object obj)

Parameters

obj object

Returns

bool

Equals(string)

public bool Equals(string other)

Parameters

other string

Returns

bool

FromSystemDrawingColour(Color)

Converts a Color into a Color.

public static Color FromSystemDrawingColour(Color color)

Parameters

color Color

Returns

Color

A Color representation of the specified color.

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

int

Parse(string)

Parses a Color from a string representation of a color.

public static Color Parse(string webColorValue)

Parameters

webColorValue string

A 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 webColorValue is null.

FormatException

If webColorValue is 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

Color

A Color representation of this color.

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

webColorValue string

A string containing a color value which is compatible with the web. See the MDN writeup on web colors for more information.

color Color

If 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 webColorValue contains 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

first Color

The first Color

second Color

The second Color

Returns

bool

true if the two colors represent the same sRGB color; false if not.

Remarks

Two colors are equal if they represent the same sRGB color.

operator ==(Color, Color)

Gets a value indicating whether the specified Color and Color are equal.

public static bool operator ==(Color color, Color sysColor)

Parameters

color Color

The Color

sysColor Color

The Color

Returns

bool

true if the two different colors represent the same sRGB color; false if not.

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 equal.

public static bool operator ==(Color color, string stringColor)

Parameters

color Color

The Color

stringColor string

The string which represents a color

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 specified Color and Color are equal.

public static bool operator ==(Color sysColor, Color color)

Parameters

sysColor Color

The Color

color Color

The Color

Returns

bool

true if the two different colors represent the same sRGB color; false if not.

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 equal.

public static bool operator ==(string stringColor, Color color)

Parameters

stringColor string

The string which represents a color

color Color

The Color

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

first Color

The first Color

second Color

The second Color

Returns

bool

false if the two colors represent the same sRGB color; true if not.

Remarks

Two colors are equal if they represent the same sRGB color.

operator !=(Color, Color)

Gets a value indicating whether the specified Color and Color are not equal.

public static bool operator !=(Color color, Color sysColor)

Parameters

color Color

The Color

sysColor Color

The Color

Returns

bool

false if the two different colors represent the same sRGB color; true if not.

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

color Color

The Color

stringColor string

The string which represents a color

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)

Gets a value indicating whether the specified Color and Color are not equal.

public static bool operator !=(Color sysColor, Color color)

Parameters

sysColor Color

The Color

color Color

The Color

Returns

bool

false if the two different colors represent the same sRGB color; true if not.

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

stringColor string

The string which represents a color

color Color

The Color

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.