Table of Contents

Method ExtractColorPalette

Namespace
LMKit.Media.Image
Assembly
LM-Kit.NET.dll

ExtractColorPalette(ColorPaletteOptions)

Finds the colors the image is made of - its palette, largest share first, each color named with its basic color term - so a library can answer "the blue ones", a caption can say "a red car on gray asphalt", and a design tool can pick a matching accent. The source is never modified.

public ImageBuffer.ColorPalette ExtractColorPalette(ImageBuffer.ColorPaletteOptions options = null)

Parameters

options ImageBuffer.ColorPaletteOptions

The settings to apply, or null for the defaults.

Returns

ImageBuffer.ColorPalette

The palette. Empty when the image has no opaque pixel to read.

Remarks

The engine is deterministic and bounded whatever the source size. The image is box-averaged down to at most MaxSamplePixels samples (every source pixel still weighs in; transparent pixels of an RGBA image are left out), converted to CIELAB, the perceptually uniform space in which Euclidean distance approximates what the eye sees, and binned into a fixed L*a*b* histogram so the clustering runs over a few thousand weighted cells instead of millions of pixels. Weighted k-means then groups the cells, seeded deterministically in the k-means++ manner (the heaviest cell first, then the cell whose weight times its squared distance to the nearest seed is largest) and refined by Lloyd iterations to convergence. Clusters closer than MergeDistance under CIEDE2000 are one color and merge; clusters under MinShare are dropped. Each surviving color is the weighted mean of its cluster, converted back to sRGB and named by ColorNaming.

The same image always produces the same palette; a change of one option changes the palette predictably. Typical cost is a few milliseconds for a photograph once decoded, dominated by the single pass over the source pixels.

GRAY8, RGB24 and RGBA32 sources are read directly; a BINARY1 source is analysed as grayscale.

Exceptions

ObjectDisposedException

Thrown if this instance has been disposed.

NotSupportedException

Thrown for a pixel format the analysis cannot read.

Share