Table of Contents

Struct HslColor

Namespace
Avalonia.Media
Assembly
Avalonia.Base.dll

Defines a color using the hue/saturation/lightness (HSL) model. This uses a cylindrical-coordinate representation of a color.

public readonly struct HslColor : IEquatable<HslColor>
Implements
Inherited Members

Constructors

HslColor(Color)

Initializes a new instance of the HslColor struct.

public HslColor(Color color)

Parameters

color Color

The RGB color to convert to HSL.

HslColor(double, double, double, double)

Initializes a new instance of the HslColor struct.

public HslColor(double alpha, double hue, double saturation, double lightness)

Parameters

alpha double

The Alpha (transparency) component in the range from 0..1.

hue double

The Hue component in the range from 0..360. Note that 360 is equivalent to 0 and will be adjusted automatically.

saturation double

The Saturation component in the range from 0..1.

lightness double

The Lightness component in the range from 0..1.

Properties

A

Gets the Alpha (transparency) component in the range from 0..1 (percentage).

public double A { get; }

Property Value

double

Remarks

  • 0 is fully transparent.
  • 1 is fully opaque.

H

Gets the Hue component in the range from 0..360 (degrees). This is the color's location, in degrees, on a color wheel/circle from 0 to 360. Note that 360 is equivalent to 0 and will be adjusted automatically.

public double H { get; }

Property Value

double

Remarks

  • 0/360 degrees is Red.
  • 60 degrees is Yellow.
  • 120 degrees is Green.
  • 180 degrees is Cyan.
  • 240 degrees is Blue.
  • 300 degrees is Magenta.

L

Gets the Lightness component in the range from 0..1 (percentage).

public double L { get; }

Property Value

double

Remarks

  • 0 is fully black.
  • 1 is fully white.

S

Gets the Saturation component in the range from 0..1 (percentage).

public double S { get; }

Property Value

double

Remarks

  • 0 is fully white (or a shade of gray) and shows no color.
  • 1 is the full color.

Methods

Equals(HslColor)

public bool Equals(HslColor other)

Parameters

other HslColor

Returns

bool

Equals(object?)

public override bool Equals(object? obj)

Parameters

obj object

Returns

bool

FromAhsl(double, double, double, double)

Creates a new HslColor from individual color component values.

public static HslColor FromAhsl(double a, double h, double s, double l)

Parameters

a double

The Alpha (transparency) component in the range from 0..1.

h double

The Hue component in the range from 0..360.

s double

The Saturation component in the range from 0..1.

l double

The Lightness component in the range from 0..1.

Returns

HslColor

A new HslColor built from the individual color component values.

Remarks

This exists for symmetry with the Color struct; however, the appropriate constructor should commonly be used instead.

FromHsl(double, double, double)

Creates a new HslColor from individual color component values.

public static HslColor FromHsl(double h, double s, double l)

Parameters

h double

The Hue component in the range from 0..360.

s double

The Saturation component in the range from 0..1.

l double

The Lightness component in the range from 0..1.

Returns

HslColor

A new HslColor built from the individual color component values.

Remarks

This exists for symmetry with the Color struct; however, the appropriate constructor should commonly be used instead.

GetHashCode()

Gets a hashcode for this object. Hashcode is not guaranteed to be unique.

public override int GetHashCode()

Returns

int

The hashcode for this object.

Parse(string)

Parses an HSL color string.

public static HslColor Parse(string s)

Parameters

s string

The HSL color string to parse.

Returns

HslColor

The parsed HslColor.

ToHsv()

Returns the HSV color model equivalent of this HSL color.

public HsvColor ToHsv()

Returns

HsvColor

The HSV equivalent color.

ToHsv(double, double, double, double)

Converts the given HSLA color component values to their HSV color equivalent.

public static HsvColor ToHsv(double hue, double saturation, double lightness, double alpha = 1)

Parameters

hue double

The Hue component in the HSL color model in the range from 0..360.

saturation double

The Saturation component in the HSL color model in the range from 0..1.

lightness double

The Lightness component in the HSL color model in the range from 0..1.

alpha double

The Alpha component in the range from 0..1.

Returns

HsvColor

A new HsvColor equivalent to the given HSLA values.

ToRgb()

Returns the RGB color model equivalent of this HSL color.

public Color ToRgb()

Returns

Color

The RGB equivalent color.

ToRgb(double, double, double, double)

Converts the given HSLA color component values to their RGB color equivalent.

public static Color ToRgb(double hue, double saturation, double lightness, double alpha = 1)

Parameters

hue double

The Hue component in the HSL color model in the range from 0..360.

saturation double

The Saturation component in the HSL color model in the range from 0..1.

lightness double

The Lightness component in the HSL color model in the range from 0..1.

alpha double

The Alpha component in the range from 0..1.

Returns

Color

A new RGB Color equivalent to the given HSLA values.

ToString()

public override string ToString()

Returns

string

TryParse(string?, out HslColor)

Parses an HSL color string.

public static bool TryParse(string? s, out HslColor hslColor)

Parameters

s string

The HSL color string to parse.

hslColor HslColor

The parsed HslColor.

Returns

bool

True if parsing was successful; otherwise, false.

Operators

operator ==(HslColor, HslColor)

Indicates whether the values of two specified HslColor objects are equal.

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

Parameters

left HslColor

The first object to compare.

right HslColor

The second object to compare.

Returns

bool

True if left and right are equal; otherwise, false.

explicit operator Color(HslColor)

Explicit conversion from an HslColor to a Color.

public static explicit operator Color(HslColor hslColor)

Parameters

hslColor HslColor

The HslColor to convert.

Returns

Color

operator !=(HslColor, HslColor)

Indicates whether the values of two specified HslColor objects are not equal.

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

Parameters

left HslColor

The first object to compare.

right HslColor

The second object to compare.

Returns

bool

True if left and right are not equal; otherwise, false.