PredefinedColorsQuantizerFromCustomFunction(FuncColor32, Color32, Color32, KnownPixelFormat, Byte) Method

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

See the online help for an example with images.

Definition

Namespace: KGySoft.Drawing.Imaging
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 9.0.0
C#
public static PredefinedColorsQuantizer FromCustomFunction(
	Func<Color32, Color32> quantizingFunction,
	Color32 backColor,
	KnownPixelFormat pixelFormatHint = KnownPixelFormat.Format24bppRgb,
	byte alphaThreshold = 0
)

Parameters

quantizingFunction  FuncColor32, Color32
A delegate that specifies the custom quantizing 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.
backColor  Color32
Colors with alpha (transparency), which are considered opaque will be blended with this color before invoking the quantizingFunction delegate. The Color32.A field of the background color is ignored.
pixelFormatHint  KnownPixelFormat  (Optional)
The KnownPixelFormat value that the PixelFormatHint property of the returned instance will return. This parameter is optional.
Default value: Format24bppRgb, which is valid only if alphaThreshold has the default zero value.
alphaThreshold  Byte  (Optional)
Specifies a threshold value for the Color32.A field, under which a quantized color is considered transparent. If 0, then even the completely transparent colors will be blended with backColor before invoking the quantizingFunction delegate. This parameter is optional.
Default value: 0.

Return Value

PredefinedColorsQuantizer
A PredefinedColorsQuantizer instance that can quantize 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 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 never calls the quantizingFunction delegate with a color with alpha. Depending on alphaThreshold either a completely transparent color will be returned or the color will be blended with backColor before invoking the delegate. In order to allow invoking quantizingFunction with alpha colors use the FromCustomFunction(FuncColor32, Color32, KnownPixelFormat) or FromCustomFunction(FuncColor32, Color32, Color32, Byte, Boolean, KnownPixelFormat) overloads instead.

Examples

The following example demonstrates how to use the quantizer returned by this method:
C#
public static IReadWriteBitmapData ToGrayscaleOpaque(IReadWriteBitmapData source, Color32 backColor = default)
{
    IQuantizer quantizer = PredefinedColorsQuantizer.FromCustomFunction(c => c.ToGray(), backColor);

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

    // b.) alternatively, you can perform the quantizing directly on the source bitmap data:
    source.Quantize(quantizer);
    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 black background
Default (black) background

Graayscale color hues with silver background
Silver background

Shield icon with transparent background
Shield icon with transparency

Grayscale shield icon with black background
Default (black) background

Grayscale shield icon with silver background
Silver background

See Also