PredefinedColorsQuantizerSystemDefault8BppPalette Method

Gets a PredefinedColorsQuantizer instance that quantizes colors using the system default 8-bit palette. This palette contains the 16 standard basic sRGB colors, the "web-safe" palette of 216 colors as well as 24 transparent entries.

Definition

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

Parameters

backColor  Color  (Optional)
Colors with alpha (transparency), which are considered opaque 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.
alphaThreshold  Byte  (Optional)
If the system default 8-bit palette contains a transparent color on the current operating system, then specifies a threshold value for the Color.A property, under which a quantized color is considered transparent. If 0, then the quantized colors will never be transparent. This parameter is optional.
Default value: 128.

Return Value

PredefinedColorsQuantizer
A PredefinedColorsQuantizer instance that quantizes colors using the system default 8-bit palette.

Remarks

The returned PredefinedColorsQuantizer instance can return up to 256 colors. Actually this amount is somewhat smaller because of some redundant entries in the palette.

This quantizer fits well for the Format8bppIndexed pixel format.

The palette of this quantizer contains transparent entries.

Example

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

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

    // b.) when converting to Format8bppIndexed format without dithering, this produces the same result:
    if (ditherer == null)
        return source.Clone(KnownPixelFormat.Format8bppIndexed, backColor, alphaThreshold);

    // c.) 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 system default 8 BPP palette, black background and default alpha threshold
Default optional parameter values (black background, alpha threshold = 128). The bottom half of the image is transparent.

Color hues with system default 8 BPP palette, silver background and alpha threshold = 1
Silver background, alpha threshold = 1. Only the bottom line is transparent.

Color hues with system default 8 BPP palette, silver background, default alpha threshold and Bayer 8x8 ordered dithering
Silver background, default alpha threshold, Bayer 8x8 dithering. The bottom half of the image is transparent.

Grayscale color shades with different bit depths
Grayscale color shades

Grayscale color shades with system default 8 BPP palette
Default optional parameter values

Grayscale color shades with system default 8 BPP palette using Bayer 8x8 ordered dithering
Bayer 8x8 dithering

Shield icon with transparent background
Shield icon with transparency

Shield icon with system default 8 BPP palette
Default optional parameter values (black background, alpha threshold = 128)

Shield icon with system default 8 BPP palette using Bayer 8x8 ordered dithering
Default background and alpha threshold, Bayer 8x8 dithering

Shield icon with system default 8 BPP palette using silver background, alpha threshold = 1 and Floyd-Steinberg dithering
Silver background, alpha threshold = 1, Floyd-Steinberg dithering

Test image "Girl with a Pearl Earring"
Original test image "Girl with a Pearl Earring"

Test image "Girl with a Pearl Earring" with system default 8 BPP palette, quantized in the sRGB color space
Default optional parameter values

Test image "Girl with a Pearl Earring" with system default 8 BPP palette, quantized in the sRGB color space using Bayer 8x8 ordered dithering
Bayer 8x8 dithering

Test image "Girl with a Pearl Earring" with system default 8 BPP palette, quantized in the sRGB color space using Floyd-Steinberg dithering
Floyd-Steinberg dithering

See Also