PredefinedColorsQuantizerBlackAndWhite Method
Namespace: KGySoft.Drawing.ImagingAssembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 10.0.0-rc.1
public static PredefinedColorsQuantizer BlackAndWhite(
Color32 backColor = default,
byte whiteThreshold = 128,
byte alphaThreshold = 128
)
Public Shared Function BlackAndWhite (
Optional backColor As Color32 = Nothing,
Optional whiteThreshold As Byte = 128,
Optional alphaThreshold As Byte = 128
) As PredefinedColorsQuantizer
public:
static PredefinedColorsQuantizer^ BlackAndWhite(
Color32 backColor = Color32(),
unsigned char whiteThreshold = 128,
unsigned char alphaThreshold = 128
)
static member BlackAndWhite :
?backColor : Color32 *
?whiteThreshold : byte *
?alphaThreshold : byte
(* Defaults:
let _backColor = defaultArg backColor new Color32()
let _whiteThreshold = defaultArg whiteThreshold 128
let _alphaThreshold = defaultArg alphaThreshold 128
*)
-> PredefinedColorsQuantizer
- backColor Color32 (Optional)
- Colors with alpha (transparency) 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. - 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. - alphaThreshold Byte (Optional)
- Specifies a threshold value for the Color32.A field, under which a quantized color
is considered completely transparent. Though the quantizer returned from this method never returns alpha colors, it can be relevant in some cases, for example when drawing a partially
transparent bitmap onto a solid background. The source pixels, whose alpha value is below the alphaThreshold will be skipped,
whereas alpha pixels with higher opacity will be blended with the specified backColor. This parameter is optional.
Default value: 128.
PredefinedColorsQuantizerA
PredefinedColorsQuantizer instance that quantizes every color to black or white.
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.
The following example demonstrates how to use the quantizer returned by this method:
public static IReadWriteBitmapData ToBlackAndWhite(IReadWriteBitmapData source, Color32 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 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
|  Default optional parameter values (black background)
 Silver background
 Silver background, Bayer 8x8 dithering
|
 Grayscale color shades
|  Default optional parameter values
 White threshold = 32
 White threshold = 224
 Default white threshold, Bayer 8x8 dithering
|
 Shield icon with transparency
|  Default optional parameter values (black background)
 Silver background
 Silver background, Floyd-Steinberg dithering
|
 Original test image "Cameraman"
|  Default optional parameter values
 White threshold = 96
 White threshold = 96, Bayer 8x8 dithering. The ordered dithering preserves the white threshold value.
 White threshold = 96, Floyd-Steinberg dithering. The error diffusion dithering compensates the white threshold value.
|