PredefinedColorsQuantizerFromCustomFunction(FuncColor32, Color32, KnownPixelFormat) Method

Gets a PredefinedColorsQuantizer instance that quantizes colors using the custom quantizer function specified in the quantizingFunction parameter.


Namespace: KGySoft.Drawing.Imaging
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 8.1.0
public static PredefinedColorsQuantizer FromCustomFunction(
	Func<Color32, Color32> quantizingFunction,
	KnownPixelFormat pixelFormatHint = KnownPixelFormat.Format32bppArgb


quantizingFunction  FuncColor32, Color32
A delegate that specifies the custom quantization logic. It must be thread-safe for parallel invoking and it is expected to be fast. The results returned by the delegate are not cached.
pixelFormatHint  KnownPixelFormat  (Optional)
The KnownPixelFormat value that the PixelFormatHint property of the returned instance will return. This parameter is optional.
Default value: Format32bppArgb.

Return Value

A PredefinedColorsQuantizer instance that quantizes colors using the custom quantizer function specified in the quantizingFunction parameter.


The quantizer returned by this method does not have a palette. If you need to create an indexed result using a custom mapping function that uses up to 256 different colors, then create a Palette instance specifying a custom function and call the FromCustomPalette(Palette) method instead.

This overload always calls the quantizingFunction delegate without preprocessing the input colors. In order to pass only opaque colors to the quantizingFunction delegate use the FromCustomFunction(FuncColor32, Color32, Color, KnownPixelFormat, Byte) overload instead.

This overload always creates a quantizer with black BackColor and zero AlphaThreshold. If quantizingFunction can return colors with alpha, then the background color and alpha threshold are relevant only when this quantizer is used together with an IDitherer, which does not support partial transparency. Use the FromCustomFunction(FuncColor32, Color32, Color, Byte, Boolean, KnownPixelFormat) overload to specify the BackColor and AlphaThreshold properties.


The following example demonstrates how to use the quantizer returned by this method:
public static IReadWriteBitmapData ToGrayscalePreserveAlpha(IReadWriteBitmapData source)
    IQuantizer quantizer = PredefinedColorsQuantizer.FromCustomFunction(c => c.ToGray());

    // a.) this solution returns a new bitmap data and does not change the original one:
    return source.Clone(KnownPixelFormat.Format32bppArgb, quantizer);

    // b.) alternatively, you can perform the quantization directly on the source bitmap data:
    return source;

The example above may produce the following results:

Original image
Quantized image

Color hues with alpha gradient
Color hues with alpha gradient

Grayscale color hues with alpha preserved
Alpha has been preserved

Shield icon with transparent background
Shield icon with transparency

Grayscale shield icon with alpha preserved
Alpha has been preserved

See Also