PredefinedColorsQuantizerGrayscale Method

Gets a PredefinedColorsQuantizer instance that can quantize colors to 8-bit grayscale ones of 256 shades.

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 Grayscale(
	Color32 backColor = default,
	byte alphaThreshold = 128
)

Parameters

backColor  Color32  (Optional)
Colors with alpha (transparency) will be blended with this color before quantizing. The Color32.A field of the background color is ignored. This parameter is optional.
Default value: The default value of the Color32 type, which has the same RGB values as Color.Black.
alphaThreshold  Byte  (Optional)
Specifies a threshold value for the Color32.A field, under which a quantized color is considered completely transparent. Though the quantizer returned from this method never returns alpha colors, it can be relevant in some cases, for example when drawing a partially transparent bitmap onto a solid background. The source pixels, whose alpha value is below the alphaThreshold will be skipped, whereas alpha pixels with higher opacity will be blended with the specified backColor. This parameter is optional.
Default value: 128.

Return Value

PredefinedColorsQuantizer
A PredefinedColorsQuantizer instance that can quantize colors to 8-bit grayscale ones.

Remarks

The returned quantizer uses direct mapping to grayscale colors based on human perception, which makes quantizing very fast while it is very accurate at the same time.

The returned PredefinedColorsQuantizer instance can return up to 256 possible shades of gray.

The palette of this quantizer does not contain the transparent color. To make a bitmap data grayscale with transparency you can use the ToGrayscale and MakeGrayscale extension methods.

This quantizer fits well for the Format8bppIndexed and Format8bppGrayScale pixel formats.

Examples

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

    // a.) this solution returns a new bitmap data and does not change the original one:
    return source.Clone(KnownPixelFormat.Format8bppIndexed, 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 8 BPP grayscale palette and black background
Default (black) background

Graayscale color hues with 8 BPP grayscale palette and silver background
Silver background

Shield icon with transparent background
Shield icon with transparency

Shield icon with 8 BPP grayscale palette and black background
Default (black) background

Shield icon with 8 BPP grayscale palette and silver background
Silver background

See Also