Class ColorConvert
Converts between color values (IColorValue<TSelf>), through XYZ D65, and between rows of pixels and color values.
public static class ColorConvert
- Inheritance
-
ColorConvert
- 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
Methods
Convert<TFrom, TTo>(TFrom)
Converts a color value to another model.
public static TTo Convert<TFrom, TTo>(TFrom value) where TFrom : struct, IColorValue<TFrom> where TTo : struct, IColorValue<TTo>
Parameters
valueTFromThe value.
Returns
- TTo
The same color in
TTo.
Type Parameters
TFromThe model converted from.
TToThe model converted to.
FromPixels<TPixel, TColor>(ReadOnlySpan<TPixel>, Span<TColor>, ColorProfile?)
Converts pixels to color values. The pixels are in encoding (sRGB when null), so the
Lab of a Display P3 pixel is one argument away; alpha is ignored. 8-bit sRGB pixels to Lab, Oklab, HSL, HSV and YCbCr take
a fast path.
public static void FromPixels<TPixel, TColor>(ReadOnlySpan<TPixel> pixels, Span<TColor> colors, ColorProfile? encoding = null) where TPixel : unmanaged, IPixelType<TPixel> where TColor : struct, IColorValue<TColor>
Parameters
pixelsReadOnlySpan<TPixel>The pixels.
colorsSpan<TColor>Where to write the values, at least as long as
pixels.encodingColorProfileThe pixels' RGB encoding, or null for sRGB.
Type Parameters
TPixelThe pixel type.
TColorThe color value type.
Exceptions
- ArgumentException
colorsis shorter thanpixels.
ToPixels<TColor, TPixel>(ReadOnlySpan<TColor>, Span<TPixel>, ColorProfile?)
Converts color values to opaque pixels in encoding (sRGB when null). Integer pixel
types clamp colors outside the encoding; float types keep them.
public static void ToPixels<TColor, TPixel>(ReadOnlySpan<TColor> colors, Span<TPixel> pixels, ColorProfile? encoding = null) where TColor : struct, IColorValue<TColor> where TPixel : unmanaged, IPixelType<TPixel>
Parameters
colorsReadOnlySpan<TColor>The values.
pixelsSpan<TPixel>Where to write the pixels, at least as long as
colors.encodingColorProfileThe pixels' RGB encoding, or null for sRGB.
Type Parameters
TColorThe color value type.
TPixelThe pixel type.
Exceptions
- ArgumentException
pixelsis shorter thancolors.