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

NameDescription
Hsl(double, double, double)Initializes a new instance of the Hsl struct.

Hsl(double, double, double)

Hsl.cs

Initializes a new instance of the Hsl struct.

public Hsl(double h, double s, double l)

Parameters

h double
The hue angle, in degrees: 0 is red, 120 green, 240 blue.
s double
The saturation, from 0 (gray) to 1.
l double
The lightness, from 0 (black) to 1 (white); 0.5 is the pure hue.

Properties

NameDescription
HGets the hue angle, in degrees: 0 is red, 120 green, 240 blue.
LGets the lightness, from 0 (black) to 1 (white); 0.5 is the pure hue.
SGets the saturation, from 0 (gray) to 1.

Gets the hue angle, in degrees: 0 is red, 120 green, 240 blue.

public double H { get; }

Property value

double

Gets the lightness, from 0 (black) to 1 (white); 0.5 is the pure hue.

public double L { get; }

Property value

double

Gets the saturation, from 0 (gray) to 1.

public double S { get; }

Property value

double

Methods

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

Equals overloads

Equals(Hsl)

Hsl.cs

Indicates whether the current object is equal to another object of the same type.

public bool Equals(Hsl other)
Parameters
other Hsl
An object to compare with this object.
Returns

bool true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Hsl.cs

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)
Parameters
obj object?
The object to compare with the current instance.
Returns

bool true if obj and this instance are the same type and represent the same value; otherwise, false.

FromRgb overloads

FromRgb(Rgba32)

Hsl.cs

Converts an 8-bit RGB color to HSL. Alpha is ignored.

public static Hsl FromRgb(Rgba32 color)
Parameters
color Rgba32
The color.
Returns

Hsl The color.

FromRgb(Rgba64)

Hsl.cs

Converts a 16-bit RGB color to HSL. Alpha is ignored.

public static Hsl FromRgb(Rgba64 color)
Parameters
color Rgba64
The color.
Returns

Hsl The color.

GetHashCode()

Hsl.cs

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int A 32-bit signed integer that is the hash code for this instance.

Parse(ReadOnlySpan<char>, double)

Hsl.cs

Parses 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

value ReadOnlySpan<char>
The text.
alpha double
The alpha of the color, in [0, 1]: 1 when the text has none.

Returns

Hsl The color.

Exceptions

FormatException
value is not a hsl() 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.cs

Formats this color with the CSS Color 4 hsl() function, the components after the hue as percentages: hsl(120 50% 25%).

public string ToCssString(double alpha = 1d)

Parameters

alpha double
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

ArgumentOutOfRangeException
alpha is not in [0, 1], or a component is not a finite number.

ToRgba32()

Hsl.cs

Converts this color to an opaque 8-bit RGB color, each component clipped to [0, 1].

public Rgba32 ToRgba32()

Returns

Rgba32 The color.

ToRgba64()

Hsl.cs

Converts this color to an opaque 16-bit RGB color, each component clipped to [0, 1].

public Rgba64 ToRgba64()

Returns

Rgba64 The color.

ToString()

Hsl.cs

Returns the fully qualified type name of this instance.

public override string ToString()

Returns

string The fully qualified type name.

TryParse(ReadOnlySpan<char>, Hsl, double)

Hsl.cs

Tries 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

value ReadOnlySpan<char>
The text.
result Hsl
The color, or the default value when the text is not a hsl() color.
alpha double
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

NameDescription
operator ==(Hsl, Hsl)Compares two colors for equality, component by component.
operator !=(Hsl, Hsl)Compares two colors for inequality, component by component.

operator ==(Hsl, Hsl)

Hsl.cs

Compares two colors for equality, component by component.

public static bool operator ==(Hsl left, Hsl right)

Parameters

left Hsl
The first color.
right Hsl
The second color.

Returns

bool true if all the components are equal; otherwise false.

operator !=(Hsl, Hsl)

Hsl.cs

Compares two colors for inequality, component by component.

public static bool operator !=(Hsl left, Hsl right)

Parameters

left Hsl
The first color.
right Hsl
The second color.

Returns

bool true if a component differs; otherwise false.