ImagePalette Class

An immutable palette of 1 to 256 colors with 8-bit samples and straight alpha, for a color reduction (QuantizeOptions.Palette). Build one from the pixels of frames with PaletteBuilder, or list the colors.

Remarks

The colors keep their order, without duplicates (the first occurrence is kept). Every fully transparent color is the same entry, transparent black: the color hidden by a transparent pixel is not part of a palette.

When it maps a color, a reduction compares an opaque pixel with the opaque entries only (a palette without opaque entry excepted), so opaque areas never become translucent; a translucent pixel is compared with every entry. The encoders restrict the entries to what their format stores: see GifEncoder.Quantization, BmpEncoder.Quantization and PngEncoder.Quantization.

Implements

Constructors

NameDescription
ImagePalette(IEnumerable<Rgba32>)Initializes a new instance of the ImagePalette class.
ImagePalette(ReadOnlySpan<Rgba32>)Initializes a new instance of the ImagePalette class.

ImagePalette overloads

ImagePalette(IEnumerable<Rgba32>)

ImagePalette.cs

Initializes a new instance of the ImagePalette class.

public ImagePalette(IEnumerable<Rgba32> colors)
Parameters
colors IEnumerable<Rgba32>
The colors.
Exceptions
ArgumentNullException
colors is null.
ArgumentException
colors has no color, or more than 256 distinct colors.

ImagePalette(ReadOnlySpan<Rgba32>)

ImagePalette.cs

Initializes a new instance of the ImagePalette class.

public ImagePalette(ReadOnlySpan<Rgba32> colors)
Parameters
colors ReadOnlySpan<Rgba32>
The colors.
Exceptions
ArgumentException
colors has no color, or more than 256 distinct colors.

Properties

NameDescription
ColorsGets the colors, in palette order.
CountGets the number of colors.
HasTransparentColorGets a value indicating whether the palette has a fully transparent entry.

Gets the colors, in palette order.

public ReadOnlySpan<Rgba32> Colors { get; }

Property value

ReadOnlySpan<Rgba32>

Gets the number of colors.

public int Count { get; }

Property value

int

HasTransparentColor

ImagePalette.cs

Gets a value indicating whether the palette has a fully transparent entry.

public bool HasTransparentColor { get; }

Property value

bool

Indexers

NameDescription
this[int]Gets a color.

Gets a color.

public Rgba32 this[int index] { get; }

Parameters

index int
The index of the color.

Property value

Rgba32

Exceptions

ArgumentOutOfRangeException
index is negative or not less than ImagePalette.Count.

Methods

NameDescription
GetEnumerator()Returns an enumerator over the colors, in palette order.
System.Collections.IEnumerable.GetEnumerator()

GetEnumerator()

ImagePalette.cs

Returns an enumerator over the colors, in palette order.

public IEnumerator<Rgba32> GetEnumerator()

Returns

IEnumerator<Rgba32> The enumerator.

System.Collections.IEnumerable.GetEnumerator()

ImagePalette.cs
IEnumerator IEnumerable.GetEnumerator()

No documentation is available.

Constants

NameDescription
MaxCountThe largest number of colors of a palette.

MaxCount

The largest number of colors of a palette.

public const int MaxCount = 256