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
| Name | Description |
|---|---|
| Oklch(double, double, double) | Initializes a new instance of the Oklch struct. |
Oklch(double, double, double)
Oklch.csProperties
| Name | Description |
|---|---|
| C | Gets the chroma, the distance from the gray of the same lightness. |
| H | Gets the hue angle, in degrees. |
| L | Gets the perceived lightness, from 0 (black) to 1 (the white). |
Methods
| Name | Description |
|---|---|
| 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. |
FromOklab(Oklab)
Oklch.csFromRgb overloads
FromRgb(Rgba32, RgbColorSpace?)
Oklch.csConverts an 8-bit RGB color to Oklch. Alpha is ignored.
public static Oklch FromRgb(Rgba32 color, RgbColorSpace? colorSpace = null)Parameters
colorRgba32- The color.
colorSpaceRgbColorSpace?- The meaning of the red, green and blue values, or
nullforRgbColorSpace.Srgb.
Returns
Oklch The color.
FromRgb(Rgba64, RgbColorSpace?)
Oklch.csConverts a 16-bit RGB color to Oklch. Alpha is ignored.
public static Oklch FromRgb(Rgba64 color, RgbColorSpace? colorSpace = null)Parameters
colorRgba64- The color.
colorSpaceRgbColorSpace?- The meaning of the red, green and blue values, or
nullforRgbColorSpace.Srgb.
Returns
Oklch The color.
GetHashCode()
Oklch.csLerp(Oklch, Oklch, double, HueInterpolation)
Oklch.csInterpolates 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
fromOklch- The color at 0.
toOklch- The color at 1.
amountdouble- The position between the two colors; values outside [0, 1] extrapolate.
hueInterpolationHueInterpolation- The arc between the two hues.
Returns
Oklch The color, with the hue in [0, 360).
Exceptions
ArgumentOutOfRangeExceptionhueInterpolationis 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.csParses 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
valueReadOnlySpan<char>- The text.
alphadouble- The alpha of the color, in [0, 1]: 1 when the text has none.
Returns
Oklch The color.
Exceptions
FormatExceptionvalueis not aoklch()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.csFormats this color with the CSS Color 4 oklch() function, every component as a number: oklch(0.62796 0.25768 29.23).
Parameters
alphadouble- 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
ArgumentOutOfRangeExceptionalphais not in [0, 1], or a component is not a finite number.
ToOklab()
Oklch.csToRgba32(RgbColorSpace?)
Oklch.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?)
Oklch.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()
Oklch.csTryParse(ReadOnlySpan<char>, Oklch, double)
Oklch.csTries 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
valueReadOnlySpan<char>- The text.
resultOklch- The color, or the default value when the text is not a
oklch()color. alphadouble- 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
| Name | Description |
|---|---|
| operator ==(Oklch, Oklch) | Compares two colors for equality, component by component. |
| operator !=(Oklch, Oklch) | Compares two colors for inequality, component by component. |