Hsl Struct
A color given by its hue, saturation and lightness (HSL), a cylindrical form of the encoded RGB values of a device.
public readonly struct Hsl : IEquatable<Hsl>Remarks
HSL describes RGB values, not colors: it has no color space of its own, and its conversions read and write the stored RGB values as they are, whatever their color space. The equations are those of CSS Color 4 (hslToRgb and rgbToHsl). The conversions from RGB give the hue in [0, 360); a gray has no hue: it is 0.
The values are not validated: the conversion to RGB clips each component to [0, 1].
- Implements
Constructors
| Name | Description |
|---|---|
| Hsl(double, double, double) | Initializes a new instance of the Hsl struct. |
Hsl(double, double, double)
Hsl.csProperties
| Name | Description |
|---|---|
| H | Gets the hue angle, in degrees: 0 is red, 120 green, 240 blue. |
| L | Gets the lightness, from 0 (black) to 1 (white); 0.5 is the pure hue. |
| S | Gets the saturation, from 0 (gray) to 1. |
Methods
| Name | Description |
|---|---|
| Equals(Hsl) | Indicates whether the current object is equal to another object of the same type. |
| Equals(object?) | Indicates whether this instance and a specified object are equal. |
| FromRgb(Rgba32) | Converts an 8-bit RGB color to HSL. Alpha is ignored. |
| FromRgb(Rgba64) | Converts a 16-bit RGB color to HSL. Alpha is ignored. |
| GetHashCode() | Returns the hash code for this instance. |
| Parse(ReadOnlySpan<char>, double) | Parses a color written with the CSS Color 4 hsl() function, such as hsl(120 50% 25%). |
| ToCssString(double) | Formats this color with the CSS Color 4 hsl() function, the components after the hue as percentages: hsl(120 50% 25%). |
| ToRgba32() | Converts this color to an opaque 8-bit RGB color, each component clipped to [0, 1]. |
| ToRgba64() | Converts this color to an opaque 16-bit RGB color, each component clipped to [0, 1]. |
| ToString() | Returns the fully qualified type name of this instance. |
| TryParse(ReadOnlySpan<char>, Hsl, double) | Tries to parse a color written with the CSS Color 4 hsl() function, as Hsl.Parse does. |
GetHashCode()
Hsl.csParse(ReadOnlySpan<char>, double)
Hsl.csParses a color written with the CSS Color 4 hsl() function, such as hsl(120 50% 25%).
public static Hsl Parse(ReadOnlySpan<char> value, out double alpha)Parameters
valueReadOnlySpan<char>- The text.
alphadouble- The alpha of the color, in [0, 1]: 1 when the text has none.
Returns
Hsl The color.
Exceptions
FormatExceptionvalueis not ahsl()color.
Remarks
The modern syntax (hsl(120 50% 25%), with an optional / alpha) and the legacy syntax (hsl(120, 50%, 25%) and hsla()) are accepted. The hue is a number of degrees or an angle (deg, grad, rad, turn), brought to [0, 360); the saturation and the lightness are percentages, or numbers of the same value in the modern syntax, and a negative saturation is clamped to 0. Each component may be none, read as 0. Function names, units and keywords are ASCII case-insensitive, and white space may surround the text; calc() and relative colors are not supported.
ToCssString(double)
Hsl.csFormats this color with the CSS Color 4 hsl() function, the components after the hue as percentages: hsl(120 50% 25%).
Parameters
alphadouble- The alpha, in [0, 1]. It is written after a
/when it is not 1.
Returns
string The text; Hsl.Parse reads it back.
Exceptions
ArgumentOutOfRangeExceptionalphais not in [0, 1], or a component is not a finite number.
ToRgba32()
Hsl.csToRgba64()
Hsl.csToString()
Hsl.csTryParse(ReadOnlySpan<char>, Hsl, double)
Hsl.csTries to parse a color written with the CSS Color 4 hsl() function, as Hsl.Parse does.
public static bool TryParse(ReadOnlySpan<char> value, out Hsl result, out double alpha)Parameters
valueReadOnlySpan<char>- The text.
resultHsl- The color, or the default value when the text is not a
hsl()color. alphadouble- The alpha of the color, in [0, 1]: 1 when the text has none.
Returns
bool true if the text is a hsl() color.
Operators
| Name | Description |
|---|---|
| operator ==(Hsl, Hsl) | Compares two colors for equality, component by component. |
| operator !=(Hsl, Hsl) | Compares two colors for inequality, component by component. |