Table of Contents

Class Palette

Namespace
Egami.Quantization
Assembly
Egami.dll

An ordered list of up to 256 colors to map images onto: a brand palette, a display's inks, or one built from an image with Wu's quantizer. Use it as FixedPalette for the GIF, PNG, BMP and TGA writers, with Remap(Palette, DitherMode, float), or to find the nearest color yourself.

public sealed class Palette
Inheritance
Palette
Inherited Members

Examples

// Map onto a fixed palette: here an e-ink display's six inks.
Palette inks = Palette.FromColors([Rgba8.Black, Rgba8.White, new(255, 0, 0, 255), new(255, 255, 0, 255), new(0, 0, 255, 255), new(0, 255, 0, 255)]);
photo.Apply(ops => ops.Remap(inks, DitherMode.FloydSteinberg), Ct);

Properties

Colors

Gets the colors, in order.

public IReadOnlyList<Rgba8> Colors { get; }

Property Value

IReadOnlyList<Rgba8>

Count

Gets the number of colors, 1 to 256.

public int Count { get; }

Property Value

int

TransparentIndex

Gets the index of the entry fully transparent pixels take, or null when there is none. That entry is never the nearest color of a visible pixel.

public int? TransparentIndex { get; }

Property Value

int?

Methods

Build(Raster, PaletteOptions?)

Builds a palette for every frame of an image together: its own colors when they fit, Wu's quantizer otherwise, polished by RefineIterations. Alpha is kept in the colors, as for a palette PNG.

public static Palette Build(Raster image, PaletteOptions? options = null)

Parameters

image Raster

The image.

options PaletteOptions

The color limit and refinement, or null for Default; a fixed palette in them is returned as it is.

Returns

Palette

The palette.

Examples

// Build one palette from an image, polished by k-means, and use it for other images and for every GIF frame.
Palette shared = Palette.Build(first, new PaletteOptions { MaxColors = 64, RefineIterations = 6 });
second.Apply(ops => ops.Remap(shared), Ct);
second.Save("egret.gif", new GifWriterOptions { Palette = new PaletteOptions { FixedPalette = shared, Dither = DitherMode.Bayer4x4 } });

Exceptions

ArgumentNullException

image is null.

ObjectDisposedException

The image has been disposed.

FromColors(ReadOnlySpan<Rgba8>, int?)

Creates a palette of the given colors, in that order.

public static Palette FromColors(ReadOnlySpan<Rgba8> colors, int? transparentIndex = null)

Parameters

colors ReadOnlySpan<Rgba8>

1 to 256 colors; they may repeat.

transparentIndex int?

The entry fully transparent pixels take, or null (the default) for none.

Returns

Palette

The palette.

Examples

// Map onto a fixed palette: here an e-ink display's six inks.
Palette inks = Palette.FromColors([Rgba8.Black, Rgba8.White, new(255, 0, 0, 255), new(255, 255, 0, 255), new(0, 0, 255, 255), new(0, 255, 0, 255)]);
photo.Apply(ops => ops.Remap(inks, DitherMode.FloydSteinberg), Ct);

Exceptions

ArgumentException

colors is empty or has more than 256 colors.

ArgumentOutOfRangeException

transparentIndex is not an index of colors.

FromRaster(Raster, int)

Builds a palette of at most maxColors colors for an image, as Build(Raster, PaletteOptions?) with default options.

public static Palette FromRaster(Raster image, int maxColors = 256)

Parameters

image Raster

The image.

maxColors int

The most colors, 2 to 256. Defaults to 256.

Returns

Palette

The palette.

Exceptions

ArgumentNullException

image is null.

ArgumentOutOfRangeException

maxColors is outside 2 to 256.

ObjectDisposedException

The image has been disposed.

Nearest(Rgba8)

Returns the palette color nearest to color; see NearestIndex(Rgba8).

public Rgba8 Nearest(Rgba8 color)

Parameters

color Rgba8

The color.

Returns

Rgba8

The nearest color.

NearestIndex(Rgba8)

Returns the index of the color nearest to color, by squared distance in encoded RGB (plus alpha when the palette has translucent colors); a fully transparent color takes TransparentIndex when there is one.

public int NearestIndex(Rgba8 color)

Parameters

color Rgba8

The color.

Returns

int

The index; the first one on a tie.