PredefinedColorsQuantizerGrayscale Method

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

Definition

Namespace: KGySoft.Drawing.Imaging
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 8.1.0
C#
public static PredefinedColorsQuantizer Grayscale(
	Color backColor = default
)

Parameters

backColor  Color  (Optional)
Colors with alpha (transparency) will be blended with this color before quantization. The Color.A property of the background color is ignored. This parameter is optional.
Default value: Empty, which has the same RGB values as Black.

Return Value

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

Remarks

The returned quantizer uses direct mapping to grayscale colors based on human perception, which makes quantization 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 pixel format.

Example

The following example demonstrates how to use the quantizer returned by this method:
C#
public static IReadWriteBitmapData ToGrayscale(IReadWriteBitmapData source, Color 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 quantization 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