Struct OklabColor
An Oklab color (Björn Ottosson, 2020): a perceptual space built on linear sRGB, where equal distances look about equally different and hues stay put when lightness or chroma changes. CSS Color 4's oklab().
public readonly record struct OklabColor : IColorValue<OklabColor>, IEquatable<OklabColor>
- 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
OklabColor(float, float, float)
An Oklab color (Björn Ottosson, 2020): a perceptual space built on linear sRGB, where equal distances look about equally different and hues stay put when lightness or chroma changes. CSS Color 4's oklab().
public OklabColor(float L, float A, float B)
Parameters
LfloatLightness, 0 for black to 1 for white.
AfloatGreen (negative) to red (positive), about −0.4 to 0.4.
BfloatBlue (negative) to yellow (positive), about −0.4 to 0.4.
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
A
Green (negative) to red (positive), about −0.4 to 0.4.
public float A { get; init; }
Property Value
B
Blue (negative) to yellow (positive), about −0.4 to 0.4.
public float B { get; init; }
Property Value
L
Lightness, 0 for black to 1 for white.
public float L { get; init; }
Property Value
Methods
Distance(OklabColor)
Returns the Euclidean distance in Oklab, its color difference: about 0.02 is just noticeable.
public double Distance(OklabColor other)
Parameters
otherOklabColorThe other color.
Returns
- double
The distance.
From(Rgba8)
Returns the value of an sRGB pixel; alpha is ignored.
public static OklabColor From(Rgba8 color)
Parameters
colorRgba8The pixel.
Returns
- OklabColor
The value.
From(RgbaF32)
Returns the value of an sRGB pixel, whose channels may be outside 0..1; alpha is ignored.
public static OklabColor From(RgbaF32 color)
Parameters
colorRgbaF32The pixel.
Returns
- OklabColor
The value.
FromXyz(XyzColor)
Returns the value for an XYZ color (D65, white Y = 1).
public static OklabColor FromXyz(XyzColor xyz)
Parameters
xyzXyzColorThe color.
Returns
- OklabColor
The value; components may fall outside their usual range for colors outside sRGB.
IsCloseTo(OklabColor, float)
Returns whether every component of the two values differs by at most tolerance.
public bool IsCloseTo(OklabColor other, float tolerance = 0.0001)
Parameters
otherOklabColorThe other value.
tolerancefloatThe largest difference allowed in a component.
Returns
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.