PredefinedColorsQuantizerBlackAndWhite Method

KGy SOFT Drawing Libraries Help
Gets a PredefinedColorsQuantizer instance that quantizes every color to black or white.
See the Remarks section for details and some examples.

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

public static PredefinedColorsQuantizer BlackAndWhite(
	Color backColor = null,
	byte whiteThreshold = 128
)

Parameters

backColor (Optional)
Type: System.DrawingColor
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 (Optional)
Type: SystemByte
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

Type: 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 Bitmaps with Format1bppIndexed pixel format.

Note Note
For more information about the possible usable PixelFormats on different platforms see the Remarks section of the ConvertPixelFormat extension method.
Examples

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

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

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

The example above may produce the following results:

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

Reference