Palette Class

Represents an indexed set of colors and provides efficient color lookup with caching. To create an instance use the static methods or the constructors.

Definition

Namespace: KGySoft.Drawing.Imaging
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 8.1.0
C#
public sealed class Palette : IPalette
Inheritance
Object    Palette
Implements
IPalette

Remarks

The Palette class represents an indexed set of colors that can be accessed by the indexer or the GetEntries method.

The Palette class supports performing quick lookup operations (see GetNearestColor and GetNearestColorIndex methods) to find the closest matching palette entry to any color.

  Note

The palette entries represent colors in the sRGB color space and nearest color lookup is also performed in the sRGB color space by default, but you can create a Palette instance that performs looking up for nearest colors in the linear color space by the Palette(Palette, WorkingColorSpace, Color32, Byte) constructor or the factory methods that have WorkingColorSpace parameter. See the Remarks section of the WorkingColorSpace enumeration for details and image examples about using the different color spaces in various operations.

By default, the lookup is performed by a slightly modified euclidean-like search but if the Palette contains grayscale entries only, then it is optimized for finding the best matching gray shade based on human perception. To override this logic a custom lookup routine can be passed to the constructors.

If the Palette instance is created without a custom lookup logic, then the search results for non-palette-entry colors are cached. The cache is optimized for parallel processing. The theoretical maximum of cache size (apart from the actual palette entries) is 2 x 218 but as soon as that limit is reached the amount of stored elements are halved so the cache is somewhat optimized to store the most recently processed colors.

In order to prevent caching you can pass a custom lookup logic to the constructors. It is expected to be fast (applying some direct mapping to a palette index, for example), or that it uses some custom caching (which should perform well also when queried concurrently).

The palette can have any number of colors but as the typical usage is quantizing colors for indexed bitmaps the typical maximum palette size is 256. Generally, the more colors the palette has the slower are the lookups for non-palette colors that are not cached yet.

Constructors

Palette(Palette, Color32, Byte) Initializes a new instance of the Palette class from another palette using new backColor and alphaThreshold values.
Palette(Color, Color, Byte, FuncColor32, Int32) Initializes a new instance of the Palette class.
See the Remarks section of the Palette class for details.
Obsolete.
Palette(Color32, Color32, Byte, FuncColor32, Int32) Initializes a new instance of the Palette class.
See the Remarks section of the Palette class for details.
Obsolete.
Palette(IEnumerableColor32, Color32, Byte, FuncColor32, IPalette, Int32) Initializes a new instance of the Palette class.
See the Remarks section of the Palette class for details.
Palette(IEnumerableColor, Color, Byte, FuncColor32, IPalette, Int32) Initializes a new instance of the Palette class.
See the Remarks section of the Palette class for details.
Palette(Palette, WorkingColorSpace, Color32, Byte) Initializes a new instance of the Palette class from another palette using new backColor and alphaThreshold values and color space preference.
Palette(IEnumerableColor32, WorkingColorSpace, Color32, Byte, FuncColor32, IPalette, Int32) Initializes a new instance of the Palette class.
See the Remarks section of the Palette class for details.
Palette(IEnumerableColor, WorkingColorSpace, Color, Byte, FuncColor32, IPalette, Int32) Initializes a new instance of the Palette class.
See the Remarks section of the Palette class for details.

Properties

AlphaThreshold If this Palette has a transparent entry, then gets a threshold value for the Color32.A field, under which lookup operations will return the first transparent color (GetNearestColor) or the index of the first transparent color (GetNearestColorIndex) in the palette.
BackColor Gets the background color. Relevant only if this Palette does not contain partially transparent colors. If a lookup operation (GetNearestColor, GetNearestColorIndex) is performed with a color whose Color32.A field is equal to or greater than AlphaThreshold, then the color will be blended with this color before performing the lookup.
Count Gets the number of color entries in the current Palette.
HasAlpha Gets whether the palette contains at least one entry that is not fully opaque.
IsGrayscale Gets whether the palette consists of grayscale entries only.
Item Gets the color entry of this Palette at the specified index.
WorkingColorSpace Gets the preferred color space of this Palette instance for performing blending and measuring distance when looking for a nearest color. You can use the Palette(Palette, WorkingColorSpace, Color32, Byte) constructor to create a clone of this Palette using a different working color space.

Methods

BlackAndWhite(Color32, Byte) Gets a Palette instance that uses the black and white colors.
See the Remarks section of the PredefinedColorsQuantizer.BlackAndWhite method for details and some examples.
BlackAndWhite(WorkingColorSpace, Color32, Byte) Gets a Palette instance that uses the black and white colors.
See the Remarks section of the PredefinedColorsQuantizer.BlackAndWhite method for details and some examples.
GetColor Gets the color entry of this Palette at the specified index.
GetEntries Gets a read-only wrapper of the entries of this Palette instance.
GetNearestColor Gets a Color32 entry of this Palette that is the nearest color to the specified Color32 instance.
GetNearestColorIndex Gets the index of a Palette entry that is the nearest color to the specified Color32 instance.
Grayscale16(Color32, Boolean) Gets a Palette instance that uses a 4-bit grayscale palette of 16 shades.
See the Remarks section of the PredefinedColorsQuantizer.Grayscale16 method for details and some examples.
Grayscale16(WorkingColorSpace, Color32, Boolean) Gets a Palette instance that uses a 4-bit grayscale palette of 16 shades.
See the Remarks section of the PredefinedColorsQuantizer.Grayscale16 method for details and some examples.
Grayscale256(Color32) Gets a Palette instance that uses a 8-bit grayscale palette of 256 shades.
See the Remarks section of the PredefinedColorsQuantizer.Grayscale method for details and some examples.
Grayscale256(WorkingColorSpace, Color32) Gets a Palette instance that uses a 8-bit grayscale palette of 256 shades.
See the Remarks section of the PredefinedColorsQuantizer.Grayscale method for details and some examples.
Grayscale4(Color32, Boolean) Gets a Palette instance that uses a grayscale palette of 4 shades.
See the Remarks section of the PredefinedColorsQuantizer.Grayscale4 method for details and some examples.
Grayscale4(WorkingColorSpace, Color32, Boolean) Gets a Palette instance that uses a grayscale palette of 4 shades.
See the Remarks section of the PredefinedColorsQuantizer.Grayscale4 method for details and some examples.
Rgb332(Color32, Boolean) Gets a Palette instance that uses a 8-bit palette where red, green and blue components are encoded in 3, 3 and 2 bits, respectively.
See the Remarks section of the PredefinedColorsQuantizer.Rgb332 method for details and some examples.
Rgb332(WorkingColorSpace, Color32, Boolean) Gets a Palette instance that uses a 8-bit palette where red, green and blue components are encoded in 3, 3 and 2 bits, respectively.
See the Remarks section of the PredefinedColorsQuantizer.Rgb332 method for details and some examples.
SystemDefault1BppPalette(Color32) Gets a Palette instance that uses the system default 1-bit palette. This palette consists of the black and white colors.
See the Remarks section of the PredefinedColorsQuantizer.SystemDefault1BppPalette method for details.
SystemDefault1BppPalette(WorkingColorSpace, Color32) Gets a Palette instance that uses the system default 1-bit palette. This palette consists of the black and white colors.
See the Remarks section of the PredefinedColorsQuantizer.SystemDefault1BppPalette method for details.
SystemDefault4BppPalette(Color32) Gets a Palette instance that uses the system default 4-bit palette. This palette consists of the 16 standard basic sRGB colors.
See the Remarks section of the PredefinedColorsQuantizer.SystemDefault4BppPalette method for details and some examples.
SystemDefault4BppPalette(WorkingColorSpace, Color32) Gets a Palette instance that uses the system default 4-bit palette. This palette consists of the 16 standard basic sRGB colors.
See the Remarks section of the PredefinedColorsQuantizer.SystemDefault4BppPalette method for details and some examples.
SystemDefault8BppPalette(Color32, Byte) Gets a Palette instance that uses the system default 8-bit palette. This palette contains the 16 standard basic sRGB colors, the "web-safe" palette of 216 colors as well as 24 transparent entries.
See the Remarks section of the PredefinedColorsQuantizer.SystemDefault8BppPalette method for details and some examples.
SystemDefault8BppPalette(WorkingColorSpace, Color32, Byte) Gets a Palette instance that uses the system default 8-bit palette. This palette contains the 16 standard basic sRGB colors, the "web-safe" palette of 216 colors as well as 24 transparent entries.
See the Remarks section of the PredefinedColorsQuantizer.SystemDefault8BppPalette method for details and some examples.

Thread Safety

If there is no custom lookup logic passed to the constructors, then members of this type are guaranteed to be safe for multi-threaded operations. If this type is initialized with a custom lookup logic, then thread-safety depends on the custom lookup implementation.

See Also