PredefinedColorsQuantizerArgb1555 Method

Gets a PredefinedColorsQuantizer instance that can quantize colors to 16-bit ones where alpha, red, green and blue components are encoded in 1, 5, 5 and 5 bits, respectively.

See the online help for an example with images.

Definition

Namespace: KGySoft.Drawing.Imaging
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 10.0.0-rc.1
C#
public static PredefinedColorsQuantizer Argb1555(
	Color32 backColor = default,
	byte alphaThreshold = 128
)

Parameters

backColor  Color32  (Optional)
Colors with alpha (transparency), whose Color32.A field is equal to or greater than alphaThreshold 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 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 can quantize colors to 16-bit ones where each color component is encoded in 5 bits.

Remarks

The returned PredefinedColorsQuantizer instance can return up to 32,768 colors, and a transparent color.

This quantizer fits well for the Format16bppArgb1555 pixel format.

Examples

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

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

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

    // c.) alternatively, you can perform the quantizing 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 ARGB1555 pixel format, 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 ARGB1555 pixel format, silver background and alpha threshold = 1
Silver background, alpha threshold = 1. Only the bottom line is transparent.

Color hues with ARGB1555 pixel format, 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.

Shield icon with transparent background
Shield icon with transparency

Shield icon with ARGB1555 pixel format, black background and default alpha threshold
Default optional parameter values (black background, alpha threshold = 128)

Shield icon with ARGB1555 pixel format, silver background and alpha threshold = 1
Silver background, alpha threshold = 1

Shield icon with ARGB1555 pixel format, silver background, default alpha threshold and Floyd-Steinberg dithering
Silver background, default alpha threshold, Floyd-Steinberg dithering

See Also