Skip to main content

Color Quantization and Color Palettes

Version: 2.0

Color Quantization and Color Palettes

Color Quantization​

Color Quantization is a process that reduces the number of distinct colors used in an image (based on a specified or automatically-found color palette), without much affect on the quality of the image. A quantized image has fewer colors and smaller file size compared to the original one, while keeping the visual essence of the original true-colored image intact. Color quantization is a lossy process. Dithering can also be applied to quantized images to improve the quality of the final image.

Color Palettes​

When creating an indexed image using DynamicPDF Rasterizer, the number of distinct colors is reduced to 256 or less. This can be done by using one of the predefined color palette (Web or Windows Palette), a custom palette (User Palette) or by using an algorithm to automatically determine the palette (Auto Palette). These four types of palette; Web palette, Windows palette, Auto palette and User palette are outlined below:

Web Palette : Web palette contains only 216 web safe colors .This palette can be used to create images used in web pages. These 216 colors, are recognized by all web browsers and operating systems, which means these colors will look about the same within any browser.

Windows Palette : Windows palette contains 216 web safe colors and 20 windows specific colors. This palette is suitable for creating ordinary images.

User Palette : User palette or custom palette gives complete freedom to users to select the colors and the number of colors present in the image. Output image only contains user specified colors.

Auto Palette : Auto Palette is the default color palette DynamicPDF Rasterizer uses this for creation of indexed images. Auto Palette will use a quantization algorithm to automatically determine the best color palette to use based on existing image specific colors. The auto palette can use either the Octree or Medium Cut algorithms in determining the color palette and has an option to specify the number of colors to be used in the output image. The quality of the output image is better when using auto palette as compared to using other palettes. By default, the Auto Palette uses the Octree quantization algorithm and a maximum palette size of 256.

The below example illustrates rasterizing a PDF document into an indexed PNG image using the Windows Color Palette:

PdfRasterizer rasterizer = new PdfRasterizer("C:\\DocumentA.pdf");
PngIndexedColorFormat pngIndexedColorFormat = new PngIndexedColorFormat(Palette.WindowsPalette);
PngImageFormat pngIndexed = new PngImageFormat(pngIndexedColorFormat);
rasterizer.Draw("C:\\DocumentA_WindowsPalette_Page.png", pngIndexed, ImageSize.Dpi72);
Dim rasterizer As New PdfRasterizer("C:\DocumentA.pdf")
Dim pngIndexedColorFormat As New PngIndexedColorFormat(Palette.WindowsPalette)
Dim pngIndexed As New PngImageFormat(pngIndexedColorFormat)
rasterizer.Draw("C:\DocumentA_WindowsPalette_Page.png", pngIndexed, ImageSize.Dpi72)