PredefinedColorsQuantizerFromCustomFunction Method (FuncColor32, Color32, PixelFormat)

KGy SOFT Drawing Libraries Help
Gets a PredefinedColorsQuantizer instance that quantizes colors using the custom quantizer function specified in the quantizingFunction parameter.
See the Remarks section for details and some examples.

Namespace:  KGySoft.Drawing.Imaging
Assembly:  KGySoft.Drawing (in KGySoft.Drawing.dll) Version: 5.1.0
Syntax

public static PredefinedColorsQuantizer FromCustomFunction(
	Func<Color32, Color32> quantizingFunction,
	PixelFormat pixelFormatHint = PixelFormat.Format32bppArgb
)

Parameters

quantizingFunction
Type: SystemFuncColor32, 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 (Optional)
Type: System.Drawing.ImagingPixelFormat
The PixelFormat value that the PixelFormatHint property of the returned instance will return. This parameter is optional.
Default value: Format32bppArgb.

Return Value

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

The quantizer returned by this method does not have a palette. If you need to create indexed 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, PixelFormat, Byte) overload instead.

Examples

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

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

    // b.) alternatively, you can perform the quantization directly on the source bitmap:
    source.Quantize(quantizer);
    return source;
}

The example above may produce the following results:

Original imageQuantized 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

Reference