Oklch Struct

A color in the cylindrical form of Oklab: its lightness, chroma and hue angle.

public readonly struct Oklch : IEquatable<Oklch>

Remarks

The hue is in degrees, counterclockwise from the positive a axis; the conversions from Oklab give it in [0, 360). The hue of a gray has no meaning: it is the angle of its tiny a and b, or 0 when they are exactly zero.

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

Oklch(double, double, double)

Oklch.cs

Initializes a new instance of the Oklch struct.

public Oklch(double l, double c, double h)

Parameters

l double
The perceived lightness, from 0 (black) to 1 (the white).
c double
The chroma, the distance from the gray of the same lightness.
h double
The hue angle, in degrees.

Properties

NameDescription
CGets the chroma, the distance from the gray of the same lightness.
HGets the hue angle, in degrees.
LGets the perceived lightness, from 0 (black) to 1 (the white).

Gets the chroma, the distance from the gray of the same lightness.

public double C { get; }

Property value

double

Gets the hue angle, in degrees.

public double H { 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(Oklch)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.
FromOklab(Oklab)Converts a color from Oklab.
FromRgb(Rgba32, RgbColorSpace?)Converts an 8-bit RGB color to Oklch. Alpha is ignored.
FromRgb(Rgba64, RgbColorSpace?)Converts a 16-bit RGB color to Oklch. Alpha is ignored.
GetHashCode()Returns the hash code for this instance.
Lerp(Oklch, Oklch, double, HueInterpolation)Interpolates between two colors as CSS Color 4 does for in oklch (without alpha): the lightness and the chroma linearly, the hue along the arc that hueInterpolation selects.
Parse(ReadOnlySpan<char>, double)Parses a color written with the CSS Color 4 oklch() function, such as oklch(0.62796 0.25768 29.23).
ToCssString(double)Formats this color with the CSS Color 4 oklch() function, every component as a number: oklch(0.62796 0.25768 29.23).
ToOklab()Converts this color to Oklab.
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.
TryParse(ReadOnlySpan<char>, Oklch, double)Tries to parse a color written with the CSS Color 4 oklch() function, as Oklch.Parse does.

Equals overloads

Equals(Oklch)

Oklch.cs

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

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

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

Equals(object?)

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

FromOklab(Oklab)

Oklch.cs

Converts a color from Oklab.

public static Oklch FromOklab(Oklab oklab)

Parameters

oklab Oklab
The color.

Returns

Oklch The color, with the hue in [0, 360).

FromRgb overloads

FromRgb(Rgba32, RgbColorSpace?)

Oklch.cs

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

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

Oklch The color.

FromRgb(Rgba64, RgbColorSpace?)

Oklch.cs

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

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

Oklch The color.

GetHashCode()

Oklch.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(Oklch, Oklch, double, HueInterpolation)

Oklch.cs

Interpolates between two colors as CSS Color 4 does for in oklch (without alpha): the lightness and the chroma linearly, the hue along the arc that hueInterpolation selects.

public static Oklch Lerp(Oklch from, Oklch to, double amount, HueInterpolation hueInterpolation = HueInterpolation.Shorter)

Parameters

from Oklch
The color at 0.
to Oklch
The color at 1.
amount double
The position between the two colors; values outside [0, 1] extrapolate.
hueInterpolation HueInterpolation
The arc between the two hues.

Returns

Oklch The color, with the hue in [0, 360).

Exceptions

ArgumentOutOfRangeException
hueInterpolation is not a defined value.

Remarks

The hue of a color whose chroma is at most 0.000004 is powerless (a gray): the other hue is used for both, so that a gradient from a gray to a color keeps the hue of the color. Hues that are not finite are treated the same way.

Parse(ReadOnlySpan<char>, double)

Oklch.cs

Parses a color written with the CSS Color 4 oklch() function, such as oklch(0.62796 0.25768 29.23).

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

Oklch The color.

Exceptions

FormatException
value is not a oklch() color.

Remarks

The lightness is a number or a percentage of 1, clamped to [0, 1]; the chroma is a number or a percentage of 0.4, clamped to 0 and above; the hue is a number of degrees or an angle (deg, grad, rad, turn), brought to [0, 360). 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)

Oklch.cs

Formats this color with the CSS Color 4 oklch() function, every component as a number: oklch(0.62796 0.25768 29.23).

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

Exceptions

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

ToOklab()

Oklch.cs

Converts this color to Oklab.

public Oklab ToOklab()

Returns

Oklab The color.

ToRgba32(RgbColorSpace?)

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

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

Oklch.cs

Returns the fully qualified type name of this instance.

public override string ToString()

Returns

string The fully qualified type name.

TryParse(ReadOnlySpan<char>, Oklch, double)

Oklch.cs

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

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

Parameters

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

Operators

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

operator ==(Oklch, Oklch)

Oklch.cs

Compares two colors for equality, component by component.

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

Parameters

left Oklch
The first color.
right Oklch
The second color.

Returns

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

operator !=(Oklch, Oklch)

Oklch.cs

Compares two colors for inequality, component by component.

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

Parameters

left Oklch
The first color.
right Oklch
The second color.

Returns

bool true if a component differs; otherwise false.