PredefinedColorsQuantizerRgb332 Method

KGy SOFT Drawing Libraries Help
Gets a PredefinedColorsQuantizer instance that quantizes colors to 8-bit ones where red, green and blue components are encoded in 3, 3 and 2 bits, respectively.
See the Remarks section for details and some examples.

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

public static PredefinedColorsQuantizer Rgb332(
	Color backColor = null,
	bool directMapping = false
)

Parameters

backColor (Optional)
Type: System.DrawingColor
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.
directMapping (Optional)
Type: SystemBoolean
 to map any color directly to an index instead of searching for a nearest color, which is very fast but without dithering may end up in a noticeably poorer result and higher contrast;  to perform a lookup to determine nearest colors, which may be slower but more accurate. This parameter is optional.
Default value: .

Return Value

Type: PredefinedColorsQuantizer
A PredefinedColorsQuantizer instance that quantizes colors to 8-bit ones where red, green and blue components are encoded in 3, 3 and 2 bits, respectively.
Remarks

If directMapping is , then the result of the quantization may have a higher contrast than without direct color mapping, though this can be compensated if the returned quantizer is combined with an ErrorDiffusionDitherer. Other ditherers preserve the effect of the directMapping parameter.

The returned PredefinedColorsQuantizer instance can return up to 256 colors.

This quantizer fits well for Bitmaps with Format8bppIndexed pixel format.

The palette of this quantizer does not contain the transparent color.

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 ToRgb332(Bitmap source, Color backColor = default, bool directMapping = false, IDitherer ditherer = null)
{
    IQuantizer quantizer = PredefinedColorsQuantizer.Rgb332(backColor, directMapping);

    // a.) this solution returns a new bitmap and does not change the original one:
    return source.ConvertPixelFormat(PixelFormat.Format8bppIndexed, 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 RGB332 palette and black background using nearest color lookup
Default optional parameter values (black background, nearest color lookup)

Color hues with RGB332 palette and silver background using nearest color lookup
Silver background, nearest color lookup

Color hues with RGB332 palette and silver background using direct color mapping
Silver background, direct color mapping

Color hues with RGB332 palette, silver background, using direct color mapping and Bayer 8x8 ordered dithering
Silver background, direct color mapping, Bayer 8x8 dithering

Grayscale color shades with different bit depths
Grayscale color shades

Grayscale color shades with RGB332 palette using nearest color lookup
Nearest color lookup

Grayscale color shades with RGB332 palette using direct color mapping
Direct color mapping

Grayscale color shades with RGB332 palette, using nearest color lookup and Bayer 8x8 ordered dithering
Nearest color lookup, Bayer 8x8 dithering

Grayscale color shades with RGB332 palette, using direct color mapping and Bayer 8x8 ordered dithering
Direct color mapping, Bayer 8x8 dithering

Shield icon with transparent background
Shield icon with transparency

Shield icon with RGB332 palette and black background using nearest color lookup
Default optional parameter values (nearest color lookup)

Shield icon with RGB332 palette and silver background using nearest color lookup
Silver background, nearest color lookup

Shield icon with RGB332 palette and silver background using direct color mapping
Silver background, direct color mapping

Shield icon with RGB332 palette, silver background, using direct color mapping and Floyd-Steinberg dithering
Silver background, direct color mapping, Floyd-Steinberg dithering

Test image "Lena"
Original test image "Lena"

Test image "Lena" with RGB332 palette using nearest color lookup
Nearest color lookup

Test image "Lena" with RGB332 palette using direct color mapping
Direct color mapping

Test image "Lena" with RGB332 palette using direct color mapping and Floyd-Steinberg dithering
Direct color mapping, Floyd-Steinberg dithering

See Also

Reference