ColorPaletteHelper

Inheritance: java.lang.Object

public final class ColorPaletteHelper

Helper class for color palettes manipulation.

Methods

MethodDescription
createMonochrome()Creates a monochrome color palette containing 2 colors only.
create4Bit()Creates the 4 bit color palette.
create4BitGrayscale(boolean minIsWhite)Creates the 4 bit grayscale palette.
create8Bit()Creates the 8 bit color palette.
create8BitGrayscale(boolean minIsWhite)Creates the 8 bit grayscale palette.
getCloseImagePalette(RasterImage image, int entriesCount)Gets color palette from raster image (palletizes image) in case the image does not have one.
getCloseTransparentImagePalette(RasterImage image, int entriesCount)Gets color palette from raster image (palletizes image) in case the image does not have one.
getCloseImagePalette(RasterImage image, int entriesCount, int paletteMiningMethod)Gets color palette from raster image (palletizes image) in case the image does not have one.
getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount)Gets color palette from raster image (palletizes image) in case the image does not have one.
getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette)Gets color palette from raster image (palletizes image) in case the image does not have one.
getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette, Color alphaBlendInColor)Gets color palette from raster image (palletizes image) in case the image does not have one.
getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette, Color alphaBlendInColor, boolean keepTransparency)Gets color palette from raster image (palletizes image) in case the image does not have one.
getUniformColorPalette(RasterImage image)Get uniform 256 color palette.
getDownscalePalette(RasterImage image)Get 256 color palette, composed from upper bits of initial image color values.
hasTransparentColors(IColorPalette palette)Determines whether the specified palette has transparent colors.

createMonochrome()

public static IColorPalette createMonochrome()

Creates a monochrome color palette containing 2 colors only.

Returns: IColorPalette - Color palette for monochrome images.

create4Bit()

public static IColorPalette create4Bit()

Creates the 4 bit color palette.

Returns: IColorPalette - The 4 bit color palette.

create4BitGrayscale(boolean minIsWhite)

public static IColorPalette create4BitGrayscale(boolean minIsWhite)

Creates the 4 bit grayscale palette.

Parameters:

ParameterTypeDescription
minIsWhitebooleanif set to true the palette starts with white color, otherwise it starts with black color.

Returns: IColorPalette - The 4 bit grayscale palette.

create8Bit()

public static IColorPalette create8Bit()

Creates the 8 bit color palette.

Returns: IColorPalette - The 8bit color palette.

create8BitGrayscale(boolean minIsWhite)

public static IColorPalette create8BitGrayscale(boolean minIsWhite)

Creates the 8 bit grayscale palette.

Parameters:

ParameterTypeDescription
minIsWhitebooleanif set to true the palette starts with white color, otherwise it starts with black color.

Returns: IColorPalette - The 8 bit grayscale palette.

Example: The following example creates a palettized grayscale BMP image and then saves it to a file.

String dir = "c:\\temp\\";

com.aspose.imaging.imageoptions.BmpOptions createOptions = new com.aspose.imaging.imageoptions.BmpOptions();

// Save to a file
createOptions.setSource(new com.aspose.imaging.sources.FileCreateSource(dir + "output.palette8bit.bmp", false));

// Use 8 bits per pixel to reduce the size of the output image.
createOptions.setBitsPerPixel(8);

// Set the standard 8-bit grayscale color palette which covers all grayscale colors.
// If the processed image contains only grayscale colors, then its palettized version
// is visually indistinguishable from a non-palletized one.
createOptions.setPalette(com.aspose.imaging.ColorPaletteHelper.create8BitGrayscale(false));

// Save without compression.
// You can also use RLE-8 compression to reduce the size of the output image.
createOptions.setCompression(com.aspose.imaging.fileformats.bmp.BitmapCompression.Rgb);

// Set the horizontal and vertical resolution to 96 dpi.
createOptions.setResolutionSettings(new com.aspose.imaging.ResolutionSetting(96.0, 96.0));

// Create a BMP image of 100 x 100 px and save it to a file.
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(createOptions, 100, 100);
try {
    com.aspose.imaging.Graphics graphics = new com.aspose.imaging.Graphics(image);

    com.aspose.imaging.brushes.LinearGradientBrush gradientBrush = new com.aspose.imaging.brushes.LinearGradientBrush(
            new com.aspose.imaging.Point(0, 0),
            new com.aspose.imaging.Point(image.getWidth(), image.getHeight()),
            com.aspose.imaging.Color.getBlack(),
            com.aspose.imaging.Color.getWhite());

    // Fill the image with a grayscale gradient
    graphics.fillRectangle(gradientBrush, image.getBounds());

    image.save();
} finally {
    image.dispose();
}

getCloseImagePalette(RasterImage image, int entriesCount)

public static IColorPalette getCloseImagePalette(RasterImage image, int entriesCount)

Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
entriesCountintThe desired entries count.

Returns: IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.

Example: The following example shows how to palletize a BMP image to reduce its output size.


// Create a BMP image 100 x 100 px.
com.aspose.imaging.fileformats.bmp.BmpImage bmpImage = new com.aspose.imaging.fileformats.bmp.BmpImage(100, 100);
try {
    // The linear gradient from the left-top to the right-bottom corner of the image.
    com.aspose.imaging.brushes.LinearGradientBrush brush =
            new com.aspose.imaging.brushes.LinearGradientBrush(
                    new com.aspose.imaging.Point(0, 0),
                    new com.aspose.imaging.Point(bmpImage.getWidth(), bmpImage.getHeight()),
                    com.aspose.imaging.Color.getRed(),
                    com.aspose.imaging.Color.getGreen());

    // Fill the entire image with the linear gradient brush.
    com.aspose.imaging.Graphics gr = new com.aspose.imaging.Graphics(bmpImage);
    gr.fillRectangle(brush, bmpImage.getBounds());

    // Get the closest 8-bit color palette which covers as many pixels as possible, so that a palettized image
    // is almost visually indistinguishable from a non-palletized one.
    com.aspose.imaging.IColorPalette palette = com.aspose.imaging.ColorPaletteHelper.getCloseImagePalette(bmpImage, 256);

    // 8-bit palette contains at most 256 colors.
    com.aspose.imaging.imageoptions.BmpOptions saveOptions = new com.aspose.imaging.imageoptions.BmpOptions();
    saveOptions.setPalette(palette);
    saveOptions.setBitsPerPixel(8);

    java.io.ByteArrayOutputStream stream = new java.io.ByteArrayOutputStream();
    try {
        bmpImage.save(stream, saveOptions);
        System.out.println("The palettized image size is " + stream.size() + " bytes.");
    } finally {
        stream.close();
    }

    stream = new java.io.ByteArrayOutputStream();
    try {
        bmpImage.save(stream);
        System.out.println("The non-palettized image size is " + stream.size() + " bytes.");
    } finally {
        stream.close();
    }
} finally {
    bmpImage.dispose();
}

// The output looks like this:
// The palettized image size is 11078 bytes.
// The non-palettized image size is 40054 bytes.

getCloseTransparentImagePalette(RasterImage image, int entriesCount)

public static IColorPalette getCloseTransparentImagePalette(RasterImage image, int entriesCount)

Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
entriesCountintThe desired entries count.

Returns: IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.

getCloseImagePalette(RasterImage image, int entriesCount, int paletteMiningMethod)

public static IColorPalette getCloseImagePalette(RasterImage image, int entriesCount, int paletteMiningMethod)

Gets color palette from raster image (palletizes image) in case the image does not have one. Palette is about to be optimized for better indexed image quality or taken “AS IS” when PaletteMiningMethod.UseCurrentPalette is used.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
entriesCountintThe desired entries count.
paletteMiningMethodintThe palette mining method.

Returns: IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.

Example: The following example shows how to compress a PNG image, using indexed color with best fit palette


// Loads png image        
String sourceFilePath = "OriginalRings.png";
String outputFilePath = "OriginalRingsOutput.png";
try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(sourceFilePath))
{
    com.aspose.imaging.imageoptions.PngOptions options = new com.aspose.imaging.imageoptions.PngOptions();
    options.setProgressive(true);
    // Use indexed color type
    options.setColorType(com.aspose.imaging.fileformats.png.PngColorType.IndexedColor);
    // Use maximal compression
    options.setCompressionLevel(9);
    // Get the closest 8-bit color palette which covers as many pixels as possible, so that a palettized image
    // is almost visually indistinguishable from a non-palletized one.
    options.setPalette(com.aspose.imaging.ColorPaletteHelper.getCloseImagePalette((com.aspose.imaging.RasterImage)image, 
                                256, Aspose.Imaging.PaletteMiningMethod.Histogram));
                     
    image.save(outputFilePath, options);
}
// The output file size should be significantly reduced

getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount)

public static IColorPalette getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount)

Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
destBoundsRectangleThe destination image bounds.
entriesCountintThe desired entries count.

Returns: IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.

getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette)

public static IColorPalette getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette)

Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
destBoundsRectangleThe destination image bounds.
entriesCountintThe desired entries count.
useImagePalettebooleanIf set, it will use its own image palette if available

Returns: IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.

getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette, Color alphaBlendInColor)

public static IColorPalette getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette, Color alphaBlendInColor)

Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
destBoundsRectangleThe destination image bounds.
entriesCountintThe desired entries count.
useImagePalettebooleanIf set, it will use its own image palette if available
alphaBlendInColorColorThe color that should be used as a background color for semi-transparent alpha replacement.

Returns: IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.

getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette, Color alphaBlendInColor, boolean keepTransparency)

public static IColorPalette getCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, boolean useImagePalette, Color alphaBlendInColor, boolean keepTransparency)

Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
destBoundsRectangleThe destination image bounds.
entriesCountintThe desired entries count.
useImagePalettebooleanIf set, it will use its own image palette if available
alphaBlendInColorColorThe color that should be used as a background color for semi-transparent alpha replacement.
keepTransparencybooleanIf set, it will consider alpha channel bits of the image colors.

Returns: IColorPalette - The color palette which starts with the most frequent colors from the image and contains entriesCount entries.

getUniformColorPalette(RasterImage image)

public static ColorPalette getUniformColorPalette(RasterImage image)

Get uniform 256 color palette.

Parameters:

ParameterTypeDescription
imageRasterImageThe image.

Returns: ColorPalette - The ColorPalette.

getDownscalePalette(RasterImage image)

public static ColorPalette getDownscalePalette(RasterImage image)

Get 256 color palette, composed from upper bits of initial image color values.

Parameters:

ParameterTypeDescription
imageRasterImageThe image.

Returns: ColorPalette - The ColorPalette.

hasTransparentColors(IColorPalette palette)

public static boolean hasTransparentColors(IColorPalette palette)

Determines whether the specified palette has transparent colors.

Parameters:

ParameterTypeDescription
paletteIColorPaletteThe palette.

Returns: boolean - true if the specified palette has transparent colors; otherwise, false.