Struct OklchColor
An Oklch color: the polar form of OklabColor, CSS Color 4's oklch().
public readonly record struct OklchColor : IColorValue<OklchColor>, IEquatable<OklchColor>
- Implements
- Inherited Members
Examples
// Color values in the models apps ask for, from and to pixels, and between each other.
Rgba8 orange = new(240, 128, 32, 255);
HslColor hsl = HslColor.From(orange); // hue 28°, saturation 0.87, lightness 0.53
LabColor lab = ColorConvert.Convert<HslColor, LabColor>(hsl); // through XYZ
OklchColor oklch = OklchColor.From(orange);
YCbCrColor jpeg = YCbCrColor.From(orange); // BT.601 full range, as JPEG
YCbCrColor video = YCbCrColor.From(orange, YuvMatrix.Bt709, YuvRange.Limited);
// Color difference: CIEDE2000 on Lab, distance on Oklab.
double deltaE = lab.DeltaE2000(LabColor.From(new Rgba8(235, 130, 40, 255)));
// Whole rows at once, from any pixel type and encoding.
Rgba8[] pixels = [orange, new(10, 20, 30, 255)];
OklabColor[] row = new OklabColor[pixels.Length];
ColorConvert.FromPixels<Rgba8, OklabColor>(pixels, row);
ColorConvert.FromPixels<Rgba8, OklabColor>(pixels, row, ColorProfile.DisplayP3); // the same pixels read as Display P3
Constructors
OklchColor(float, float, float)
An Oklch color: the polar form of OklabColor, CSS Color 4's oklch().
public OklchColor(float L, float C, float H)
Parameters
LfloatLightness, 0 to 1.
CfloatChroma: 0 for gray, up to about 0.37 for sRGB colors.
HfloatHue in degrees, 0 to 360; 0 for grays.
Examples
// Color values in the models apps ask for, from and to pixels, and between each other.
Rgba8 orange = new(240, 128, 32, 255);
HslColor hsl = HslColor.From(orange); // hue 28°, saturation 0.87, lightness 0.53
LabColor lab = ColorConvert.Convert<HslColor, LabColor>(hsl); // through XYZ
OklchColor oklch = OklchColor.From(orange);
YCbCrColor jpeg = YCbCrColor.From(orange); // BT.601 full range, as JPEG
YCbCrColor video = YCbCrColor.From(orange, YuvMatrix.Bt709, YuvRange.Limited);
// Color difference: CIEDE2000 on Lab, distance on Oklab.
double deltaE = lab.DeltaE2000(LabColor.From(new Rgba8(235, 130, 40, 255)));
// Whole rows at once, from any pixel type and encoding.
Rgba8[] pixels = [orange, new(10, 20, 30, 255)];
OklabColor[] row = new OklabColor[pixels.Length];
ColorConvert.FromPixels<Rgba8, OklabColor>(pixels, row);
ColorConvert.FromPixels<Rgba8, OklabColor>(pixels, row, ColorProfile.DisplayP3); // the same pixels read as Display P3
Properties
C
Chroma: 0 for gray, up to about 0.37 for sRGB colors.
public float C { get; init; }
Property Value
H
Hue in degrees, 0 to 360; 0 for grays.
public float H { get; init; }
Property Value
L
Lightness, 0 to 1.
public float L { get; init; }
Property Value
Methods
From(Rgba8)
Returns the value of an sRGB pixel; alpha is ignored.
public static OklchColor From(Rgba8 color)
Parameters
colorRgba8The pixel.
Returns
- OklchColor
The value.
From(RgbaF32)
Returns the value of an sRGB pixel, whose channels may be outside 0..1; alpha is ignored.
public static OklchColor From(RgbaF32 color)
Parameters
colorRgbaF32The pixel.
Returns
- OklchColor
The value.
FromOklab(OklabColor)
Returns the polar form of an Oklab color.
public static OklchColor FromOklab(OklabColor lab)
Parameters
labOklabColorThe color.
Returns
- OklchColor
The same color as Oklch.
FromXyz(XyzColor)
Returns the value for an XYZ color (D65, white Y = 1).
public static OklchColor FromXyz(XyzColor xyz)
Parameters
xyzXyzColorThe color.
Returns
- OklchColor
The value; components may fall outside their usual range for colors outside sRGB.
IsCloseTo(OklchColor, float)
Returns whether every component of the two values differs by at most tolerance.
public bool IsCloseTo(OklchColor other, float tolerance = 0.0001)
Parameters
otherOklchColorThe other value.
tolerancefloatThe largest difference allowed in a component.
Returns
ToOklab()
Returns the Cartesian form.
public OklabColor ToOklab()
Returns
- OklabColor
The same color as Oklab.
ToRgba8()
Returns the color as an opaque sRGB pixel, each channel clamped into range.
public Rgba8 ToRgba8()
Returns
- Rgba8
The pixel.
ToRgbaF32()
Returns the color as an opaque sRGB pixel, unclamped, so colors outside sRGB keep channels outside 0..1.
public RgbaF32 ToRgbaF32()
Returns
- RgbaF32
The pixel.
ToXyz()
Returns the color as XYZ (D65, white Y = 1).
public XyzColor ToXyz()
Returns
- XyzColor
The color.