CieLab Struct

A color in the CIE 1976 L*a*b* color space (CIELAB), relative to a white, CieXyz.D50 by default.

public readonly struct CieLab : IEquatable<CieLab>

Remarks

The equations are those of CIE 15:2004 with the exact constants (6/29)^3 and (29/3)^3, as CSS Color 4 writes them. The conversions from and to RGB go through CIEXYZ relative to D50, as the lab() colors of CSS and the CIELAB colors of ICC profiles do.

The values are not validated: colors outside the gamut of every display and values that are not numbers are kept as they are.

Implements

Constructors

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

CieLab(double, double, double)

CieLab.cs

Initializes a new instance of the CieLab struct.

public CieLab(double l, double a, double b)

Parameters

l double
The lightness, from 0 (black) to 100 (the white).
a double
The green (negative) to red (positive) axis.
b double
The blue (negative) to yellow (positive) axis.

Properties

NameDescription
AGets the green (negative) to red (positive) axis.
BGets the blue (negative) to yellow (positive) axis.
LGets the lightness, from 0 (black) to 100 (the white).

Gets the green (negative) to red (positive) axis.

public double A { get; }

Property value

double

Gets the blue (negative) to yellow (positive) axis.

public double B { get; }

Property value

double

Gets the lightness, from 0 (black) to 100 (the white).

public double L { get; }

Property value

double

Methods

NameDescription
Equals(CieLab)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, RgbColorSpace?)Converts an 8-bit RGB color to CieLab. Alpha is ignored.
FromRgb(Rgba64, RgbColorSpace?)Converts a 16-bit RGB color to CieLab. Alpha is ignored.
FromXyz(CieXyz)Converts a color from CIEXYZ relative to CieXyz.D50.
FromXyz(CieXyz, CieXyz)Converts a color from CIEXYZ relative to a white.
GetHashCode()Returns the hash code for this instance.
Parse(ReadOnlySpan<char>, double)Parses a color written with the CSS Color 4 lab() function, such as lab(52.2345 40.1645 59.9971).
ToCssString(double)Formats this color with the CSS Color 4 lab() function, every component as a number: lab(52.2345 40.1645 59.9971).
ToLch()Converts this color to its cylindrical form, CIELCh.
ToRgba32(RgbColorSpace?)Converts this color to an opaque 8-bit RGB color, clipped to the gamut of the space component by component.
ToRgba64(RgbColorSpace?)Converts this color to an opaque 16-bit RGB color, clipped to the gamut of the space component by component.
ToString()Returns the fully qualified type name of this instance.
ToXyz()Converts this color to CIEXYZ relative to CieXyz.D50.
ToXyz(CieXyz)Converts this color to CIEXYZ relative to a white.
TryParse(ReadOnlySpan<char>, CieLab, double)Tries to parse a color written with the CSS Color 4 lab() function, as CieLab.Parse does.

Equals overloads

Equals(CieLab)

CieLab.cs

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

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

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

Equals(object?)

CieLab.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, RgbColorSpace?)

CieLab.cs

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

public static CieLab FromRgb(Rgba32 color, RgbColorSpace? colorSpace = null)
Parameters
color Rgba32
The color.
colorSpace RgbColorSpace?
The meaning of the red, green and blue values, or null for RgbColorSpace.Srgb.
Returns

CieLab The color.

FromRgb(Rgba64, RgbColorSpace?)

CieLab.cs

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

public static CieLab FromRgb(Rgba64 color, RgbColorSpace? colorSpace = null)
Parameters
color Rgba64
The color.
colorSpace RgbColorSpace?
The meaning of the red, green and blue values, or null for RgbColorSpace.Srgb.
Returns

CieLab The color.

FromXyz overloads

FromXyz(CieXyz)

CieLab.cs

Converts a color from CIEXYZ relative to CieXyz.D50.

public static CieLab FromXyz(CieXyz xyz)
Parameters
xyz CieXyz
The color.
Returns

CieLab The color.

FromXyz(CieXyz, CieXyz)

CieLab.cs

Converts a color from CIEXYZ relative to a white.

public static CieLab FromXyz(CieXyz xyz, CieXyz whitePoint)
Parameters
xyz CieXyz
The color.
whitePoint CieXyz
The white the color is relative to: it has a lightness of 100 and no chroma.
Returns

CieLab The color.

GetHashCode()

CieLab.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)

CieLab.cs

Parses a color written with the CSS Color 4 lab() function, such as lab(52.2345 40.1645 59.9971).

public static CieLab 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

CieLab The color.

Exceptions

FormatException
value is not a lab() color.

Remarks

The lightness is a number or a percentage of 100, clamped to [0, 100]; a and b are numbers, or percentages of 125. 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)

CieLab.cs

Formats this color with the CSS Color 4 lab() function, every component as a number: lab(52.2345 40.1645 59.9971).

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; CieLab.Parse reads it back.

Exceptions

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

ToLch()

CieLab.cs

Converts this color to its cylindrical form, CIELCh.

public CieLch ToLch()

Returns

CieLch The color.

ToRgba32(RgbColorSpace?)

CieLab.cs

Converts this color to an opaque 8-bit RGB color, clipped to the gamut of the space component by component.

public Rgba32 ToRgba32(RgbColorSpace? colorSpace = null)

Parameters

colorSpace RgbColorSpace?
The meaning of the red, green and blue values, or null for RgbColorSpace.Srgb.

Returns

Rgba32 The color.

Exceptions

UnsupportedImageFeatureException
The space cannot convert colors to RGB values (RgbColorSpace.CanConvertFromXyz).

ToRgba64(RgbColorSpace?)

CieLab.cs

Converts this color to an opaque 16-bit RGB color, clipped to the gamut of the space component by component.

public Rgba64 ToRgba64(RgbColorSpace? colorSpace = null)

Parameters

colorSpace RgbColorSpace?
The meaning of the red, green and blue values, or null for RgbColorSpace.Srgb.

Returns

Rgba64 The color.

Exceptions

UnsupportedImageFeatureException
The space cannot convert colors to RGB values (RgbColorSpace.CanConvertFromXyz).

ToString()

CieLab.cs

Returns the fully qualified type name of this instance.

public override string ToString()

Returns

string The fully qualified type name.

ToXyz overloads

ToXyz()

CieLab.cs

Converts this color to CIEXYZ relative to CieXyz.D50.

public CieXyz ToXyz()
Returns

CieXyz The color.

ToXyz(CieXyz)

CieLab.cs

Converts this color to CIEXYZ relative to a white.

public CieXyz ToXyz(CieXyz whitePoint)
Parameters
whitePoint CieXyz
The white this color is relative to.
Returns

CieXyz The color.

TryParse(ReadOnlySpan<char>, CieLab, double)

CieLab.cs

Tries to parse a color written with the CSS Color 4 lab() function, as CieLab.Parse does.

public static bool TryParse(ReadOnlySpan<char> value, out CieLab result, out double alpha)

Parameters

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

Operators

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

operator ==(CieLab, CieLab)

CieLab.cs

Compares two colors for equality, component by component.

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

Parameters

left CieLab
The first color.
right CieLab
The second color.

Returns

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

operator !=(CieLab, CieLab)

CieLab.cs

Compares two colors for inequality, component by component.

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

Parameters

left CieLab
The first color.
right CieLab
The second color.

Returns

bool true if a component differs; otherwise false.