Oklab Struct

A color in Oklab, the perceptual color space of Björn Ottosson, in which equal distances are close to equal perceived differences: the space CSS Color 4 recommends to mix colors in.

public readonly struct Oklab : IEquatable<Oklab>

Remarks

The matrices are those of CSS Color 4, recomputed with 64-bit precision for the D65 white of CSS: the white has no chroma, exactly. Oklab is defined from CIEXYZ relative to D65; Oklab.FromXyz and Oklab.ToXyz take and give CIEXYZ relative to D50, as every conversion of the library, adapted with the linear Bradford transform. The lightness is in [0, 1]: CSS writes it as a percentage.

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
Oklab(double, double, double)Initializes a new instance of the Oklab struct.

Oklab(double, double, double)

Oklab.cs

Initializes a new instance of the Oklab struct.

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

Parameters

l double
The perceived lightness, from 0 (black) to 1 (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 perceived lightness, from 0 (black) to 1 (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 perceived lightness, from 0 (black) to 1 (the white).

public double L { get; }

Property value

double

Methods

NameDescription
Equals(Oklab)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 Oklab. Alpha is ignored.
FromRgb(Rgba64, RgbColorSpace?)Converts a 16-bit RGB color to Oklab. Alpha is ignored.
FromXyz(CieXyz)Converts a color from CIEXYZ relative to CieXyz.D50.
GetHashCode()Returns the hash code for this instance.
Lerp(Oklab, Oklab, double)Interpolates linearly between two colors, component by component: the mixture CSS Color 4 gives for in oklab (without alpha).
Parse(ReadOnlySpan<char>, double)Parses a color written with the CSS Color 4 oklab() function, such as oklab(0.62796 0.22486 0.12585).
ToCssString(double)Formats this color with the CSS Color 4 oklab() function, every component as a number: oklab(0.62796 0.22486 0.12585).
ToOklch()Converts this color to its cylindrical form, Oklch.
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.
TryParse(ReadOnlySpan<char>, Oklab, double)Tries to parse a color written with the CSS Color 4 oklab() function, as Oklab.Parse does.

Equals overloads

Equals(Oklab)

Oklab.cs

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

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

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

Equals(object?)

Oklab.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?)

Oklab.cs

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

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

Oklab The color.

FromRgb(Rgba64, RgbColorSpace?)

Oklab.cs

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

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

Oklab The color.

FromXyz(CieXyz)

Oklab.cs

Converts a color from CIEXYZ relative to CieXyz.D50.

public static Oklab FromXyz(CieXyz xyz)

Parameters

xyz CieXyz
The color.

Returns

Oklab The color.

GetHashCode()

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

Lerp(Oklab, Oklab, double)

Oklab.cs

Interpolates linearly between two colors, component by component: the mixture CSS Color 4 gives for in oklab (without alpha).

public static Oklab Lerp(Oklab from, Oklab to, double amount)

Parameters

from Oklab
The color at 0.
to Oklab
The color at 1.
amount double
The position between the two colors; values outside [0, 1] extrapolate.

Returns

Oklab The color.

Parse(ReadOnlySpan<char>, double)

Oklab.cs

Parses a color written with the CSS Color 4 oklab() function, such as oklab(0.62796 0.22486 0.12585).

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

Oklab The color.

Exceptions

FormatException
value is not a oklab() color.

Remarks

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

Oklab.cs

Formats this color with the CSS Color 4 oklab() function, every component as a number: oklab(0.62796 0.22486 0.12585).

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

Exceptions

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

ToOklch()

Oklab.cs

Converts this color to its cylindrical form, Oklch.

public Oklch ToOklch()

Returns

Oklch The color.

ToRgba32(RgbColorSpace?)

Oklab.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?)

Oklab.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()

Oklab.cs

Returns the fully qualified type name of this instance.

public override string ToString()

Returns

string The fully qualified type name.

ToXyz()

Oklab.cs

Converts this color to CIEXYZ relative to CieXyz.D50.

public CieXyz ToXyz()

Returns

CieXyz The color.

TryParse(ReadOnlySpan<char>, Oklab, double)

Oklab.cs

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

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

Parameters

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

Operators

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

operator ==(Oklab, Oklab)

Oklab.cs

Compares two colors for equality, component by component.

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

Parameters

left Oklab
The first color.
right Oklab
The second color.

Returns

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

operator !=(Oklab, Oklab)

Oklab.cs

Compares two colors for inequality, component by component.

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

Parameters

left Oklab
The first color.
right Oklab
The second color.

Returns

bool true if a component differs; otherwise false.