Table of Contents

Class ColorConvert

Namespace
Egami.Colors
Assembly
Egami.dll

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

value TFrom

The value.

Returns

TTo

The same color in TTo.

Type Parameters

TFrom

The model converted from.

TTo

The 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

pixels ReadOnlySpan<TPixel>

The pixels.

colors Span<TColor>

Where to write the values, at least as long as pixels.

encoding ColorProfile

The pixels' RGB encoding, or null for sRGB.

Type Parameters

TPixel

The pixel type.

TColor

The color value type.

Exceptions

ArgumentException

colors is shorter than pixels.

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

colors ReadOnlySpan<TColor>

The values.

pixels Span<TPixel>

Where to write the pixels, at least as long as colors.

encoding ColorProfile

The pixels' RGB encoding, or null for sRGB.

Type Parameters

TColor

The color value type.

TPixel

The pixel type.

Exceptions

ArgumentException

pixels is shorter than colors.