PredefinedColorsQuantizerBlackAndWhite Method

Gets a PredefinedColorsQuantizer instance that quantizes every color to black or white.

Definition

Namespace: KGySoft.Drawing.Imaging
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 8.1.0
C#
public static PredefinedColorsQuantizer BlackAndWhite(
	Color backColor = default,
	byte whiteThreshold = 128
)

Parameters

backColor  Color  (Optional)
Colors with alpha (transparency) will be blended with the specified backColor 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.
whiteThreshold  Byte  (Optional)
Specifies a threshold value for the brightness of the colors, under which a quantized color is considered black. If 0, then the complete result will be white. This parameter is optional.
Default value: 128.

Return Value

PredefinedColorsQuantizer
A PredefinedColorsQuantizer instance that quantizes every color to black or white.

Remarks

If the returned quantizer is combined with an ErrorDiffusionDitherer, then the effect of the whiteThreshold parameter is mostly compensated. Other ditherers preserve the effect of the whiteThreshold parameter.

This quantizer fits well for the Format1bppIndexed pixel format.

Example

The following example demonstrates how to use the quantizer returned by this method:
C#
public static IReadWriteBitmapData ToBlackAndWhite(IReadWriteBitmapData source, Color backColor = default,
    byte whiteThreshold = 128, IDitherer ditherer = null)
{
    IQuantizer quantizer = PredefinedColorsQuantizer.BlackAndWhite(backColor, whiteThreshold);

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

    // b.) alternatively, you can perform the quantization directly on the source bitmap data:
    if (ditherer == null)
        source.Quantize(quantizer);
    else
        source.Dither(quantizer, ditherer);
    return source;
}

The example above may produce the following results:

Original image
Quantized image

Color hues with alpha gradient
Color hues with alpha gradient

Color hues with black and white palette and black background
Default optional parameter values (black background)

Color hues with black and white palette and silver background
Silver background

Color hues with black and white palette, silver background, using Bayer 8x8 ordered dithering
Silver background, Bayer 8x8 dithering

Grayscale color shades with different bit depths
Grayscale color shades

Grayscale color shades with black and white palette
Default optional parameter values

Grayscale color shades with black and white palette, white threshold = 32
White threshold = 32

Grayscale color shades with black and white palette, white threshold = 224
White threshold = 224

Grayscale color shades with black and white palette, using Bayer 8x8 ordered dithering
Default white threshold, Bayer 8x8 dithering

Shield icon with transparent background
Shield icon with transparency

Shield icon with black and white palette and black background
Default optional parameter values (black background)

Shield icon with black and white palette and silver background
Silver background

Shield icon with black and white palette, silver background, using Floyd-Steinberg dithering
Silver background, Floyd-Steinberg dithering

Test image "Cameraman"
Original test image "Cameraman"

Test image "Cameraman" with black and white palette
Default optional parameter values

Test image "Cameraman" with black and white palette, white threshold = 96
White threshold = 96

Test image "Cameraman" with black and white palette, using Bayer 8x8 dithering and white threshold = 96
White threshold = 96, Bayer 8x8 dithering. The ordered dithering preserves the white threshold value.

Test image "Cameraman" with black and white palette, using Floyd-Steinberg dithering and white threshold = 96
White threshold = 96, Floyd-Steinberg dithering. The error diffusion dithering compensates the white threshold value.

See Also