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
| Name | Description |
|---|---|
| Oklab(double, double, double) | Initializes a new instance of the Oklab struct. |
Oklab(double, double, double)
Oklab.csProperties
| Name | Description |
|---|---|
| A | Gets the green (negative) to red (positive) axis. |
| B | Gets the blue (negative) to yellow (positive) axis. |
| L | Gets the perceived lightness, from 0 (black) to 1 (the white). |
Methods
| Name | Description |
|---|---|
| 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. |
FromRgb overloads
FromRgb(Rgba32, RgbColorSpace?)
Oklab.csConverts an 8-bit RGB color to Oklab. Alpha is ignored.
public static Oklab FromRgb(Rgba32 color, RgbColorSpace? colorSpace = null)Parameters
colorRgba32- The color.
colorSpaceRgbColorSpace?- The meaning of the red, green and blue values, or
nullforRgbColorSpace.Srgb.
Returns
Oklab The color.
FromRgb(Rgba64, RgbColorSpace?)
Oklab.csConverts a 16-bit RGB color to Oklab. Alpha is ignored.
public static Oklab FromRgb(Rgba64 color, RgbColorSpace? colorSpace = null)Parameters
colorRgba64- The color.
colorSpaceRgbColorSpace?- The meaning of the red, green and blue values, or
nullforRgbColorSpace.Srgb.
Returns
Oklab The color.
FromXyz(CieXyz)
Oklab.csConverts a color from CIEXYZ relative to CieXyz.D50.
GetHashCode()
Oklab.csLerp(Oklab, Oklab, double)
Oklab.csInterpolates linearly between two colors, component by component: the mixture CSS Color 4 gives for in oklab (without alpha).
Parse(ReadOnlySpan<char>, double)
Oklab.csParses 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
valueReadOnlySpan<char>- The text.
alphadouble- The alpha of the color, in [0, 1]: 1 when the text has none.
Returns
Oklab The color.
Exceptions
FormatExceptionvalueis not aoklab()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.csFormats this color with the CSS Color 4 oklab() function, every component as a number: oklab(0.62796 0.22486 0.12585).
Parameters
alphadouble- 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
ArgumentOutOfRangeExceptionalphais not in [0, 1], or a component is not a finite number.
ToOklch()
Oklab.csToRgba32(RgbColorSpace?)
Oklab.csConverts 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
colorSpaceRgbColorSpace?- The meaning of the red, green and blue values, or
nullforRgbColorSpace.Srgb.
Returns
Rgba32 The color.
Exceptions
UnsupportedImageFeatureException- The space cannot convert colors to RGB values (
RgbColorSpace.CanConvertFromXyz).
ToRgba64(RgbColorSpace?)
Oklab.csConverts 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
colorSpaceRgbColorSpace?- The meaning of the red, green and blue values, or
nullforRgbColorSpace.Srgb.
Returns
Rgba64 The color.
Exceptions
UnsupportedImageFeatureException- The space cannot convert colors to RGB values (
RgbColorSpace.CanConvertFromXyz).
ToString()
Oklab.csToXyz()
Oklab.csTryParse(ReadOnlySpan<char>, Oklab, double)
Oklab.csTries 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
valueReadOnlySpan<char>- The text.
resultOklab- The color, or the default value when the text is not a
oklab()color. alphadouble- 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
| Name | Description |
|---|---|
| operator ==(Oklab, Oklab) | Compares two colors for equality, component by component. |
| operator !=(Oklab, Oklab) | Compares two colors for inequality, component by component. |