Class ColorPaletteHelper

Class ColorPaletteHelper

Namespace: Aspose.Imaging
Assembly: Aspose.Imaging.dll (25.2.0)

Helper class for color palettes manipulation.

public static class ColorPaletteHelper

Inheritance

objectColorPaletteHelper

Inherited Members

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Methods

Create4Bit()

Creates the 4 bit color palette.

public static IColorPalette Create4Bit()

Returns

IColorPalette

The 4 bit color palette.

Create4BitGrayscale(bool)

Creates the 4 bit grayscale palette.

public static IColorPalette Create4BitGrayscale(bool minIsWhite)

Parameters

minIsWhite bool

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

Returns

IColorPalette

The 4 bit grayscale palette.

Create8Bit()

Creates the 8 bit color palette.

public static IColorPalette Create8Bit()

Returns

IColorPalette

The 8 bit color palette.

Create8BitGrayscale(bool)

Creates the 8 bit grayscale palette.

public static IColorPalette Create8BitGrayscale(bool minIsWhite)

Parameters

minIsWhite bool

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

Returns

IColorPalette

The 8 bit grayscale palette.

Examples

The following example creates a palettized grayscale BMP image and then saves it to a file.```csharp [C#]

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

                                                                                                  Aspose.Imaging.ImageOptions.BmpOptions createOptions = new Aspose.Imaging.ImageOptions.BmpOptions();

                                                                                                  // Save to a file
                                                                                                  createOptions.Source = new Aspose.Imaging.Sources.FileCreateSource(dir + "output.palette8bit.bmp", false);

                                                                                                  // Use 8 bits per pixel to reduce the size of the output image.
                                                                                                  createOptions.BitsPerPixel = 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.Palette = Aspose.Imaging.ColorPaletteHelper.Create8BitGrayscale(false);

                                                                                                  // Save without compression.
                                                                                                  // You can also use RLE-8 compression to reduce the size of the output image.
                                                                                                  createOptions.Compression = Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb;

                                                                                                  // Set the horizontal and vertical resolution to 96 dpi.
                                                                                                  createOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96.0, 96.0);

                                                                                                  // Create a BMP image of 100 x 100 px and save it to a file.
                                                                                                  using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Create(createOptions, 100, 100))
                                                                                                  {
                                                                                                      Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(image);

                                                                                                      Aspose.Imaging.Brushes.LinearGradientBrush gradientBrush = new Aspose.Imaging.Brushes.LinearGradientBrush(
                                                                                                          new Aspose.Imaging.Point(0, 0),
                                                                                                          new Aspose.Imaging.Point(image.Width, image.Height),
                                                                                                          Aspose.Imaging.Color.Black,
                                                                                                          Aspose.Imaging.Color.White);

                                                                                                      // Fill the image with a grayscale gradient
                                                                                                      graphics.FillRectangle(gradientBrush, image.Bounds);

                                                                                                      image.Save();
                                                                                                  }

### <a id="Aspose_Imaging_ColorPaletteHelper_CreateGrayscale_System_Int32_"></a> CreateGrayscale\(int\)

Gets the grayscale palette of specified bit count. Allowed bit values are 1, 2, 4, 8.

```csharp
public static IColorPalette CreateGrayscale(int bits)

Parameters

bits int

The bit count.

Returns

IColorPalette

Grayscale palette.

Exceptions

ArgumentOutOfRangeException

bits

CreateMonochrome()

Creates a monochrome color palette containing 2 colors only.

public static IColorPalette CreateMonochrome()

Returns

IColorPalette

Color palette for monochrome images.

GetCloseImagePalette(RasterImage, int)

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.

public static IColorPalette GetCloseImagePalette(RasterImage image, int entriesCount)

Parameters

image RasterImage

The raster image.

entriesCount int

The desired entries count.

Returns

IColorPalette

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

Examples

The following example loads a BMP image and saves it back to BMP using various save options.```csharp [C#]

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

                                                                                                   using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
                                                                                                   {
                                                                                                       Aspose.Imaging.RasterImage rasterImage = (Aspose.Imaging.RasterImage)image;

                                                                                                       // Create BmpOptions
                                                                                                       Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();

                                                                                                       // Use 8 bits per pixel to reduce the size of the output image.
                                                                                                       saveOptions.BitsPerPixel = 8;

                                                                                                       // Set the closest 8-bit color palette which covers the maximal number of image pixels, so that a palettized image
                                                                                                       // is almost visually indistinguishable from a non-palletized one.
                                                                                                       saveOptions.Palette = Aspose.Imaging.ColorPaletteHelper.GetCloseImagePalette(rasterImage, 256);

                                                                                                       // Save without compression.
                                                                                                       // You can also use RLE-8 compression to reduce the size of the output image.
                                                                                                       saveOptions.Compression = Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb;

                                                                                                       // Set the horizontal and vertical resolution to 96 dpi.
                                                                                                       saveOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96.0, 96.0);

                                                                                                       image.Save(dir + "sample.bmpoptions.bmp", saveOptions);
                                                                                                   }

The following example shows how to palletize a BMP image to reduce its output size.```csharp
[C#]

                                                                                              // Create a BMP image 100 x 100 px.
                                                                                              using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100))
                                                                                              {
                                                                                                  // The linear gradient from the left-top to the right-bottom corner of the image.
                                                                                                  Aspose.Imaging.Brushes.LinearGradientBrush brush =
                                                                                                      new Aspose.Imaging.Brushes.LinearGradientBrush(
                                                                                                          new Aspose.Imaging.Point(0, 0),
                                                                                                          new Aspose.Imaging.Point(bmpImage.Width, bmpImage.Height),
                                                                                                          Aspose.Imaging.Color.Red,
                                                                                                          Aspose.Imaging.Color.Green);

                                                                                                  // Fill the entire image with the linear gradient brush.
                                                                                                  Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(bmpImage);
                                                                                                  gr.FillRectangle(brush, bmpImage.Bounds);

                                                                                                  // 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.
                                                                                                  Aspose.Imaging.IColorPalette palette = Aspose.Imaging.ColorPaletteHelper.GetCloseImagePalette(bmpImage, 256);

                                                                                                  // 8-bit palette contains at most 256 colors.
                                                                                                  Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
                                                                                                  saveOptions.Palette = palette;
                                                                                                  saveOptions.BitsPerPixel = 8;

                                                                                                  using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                                                                                                  {
                                                                                                      bmpImage.Save(stream, saveOptions);
                                                                                                      System.Console.WriteLine("The palettized image size is {0} bytes.", stream.Length);
                                                                                                  }

                                                                                                  using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                                                                                                  {
                                                                                                      bmpImage.Save(stream);
                                                                                                      System.Console.WriteLine("The non-palettized image size is {0} bytes.", stream.Length);
                                                                                                  }
                                                                                              }

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

GetCloseImagePalette(RasterImage, 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.

public static IColorPalette GetCloseImagePalette(RasterImage image, int entriesCount, PaletteMiningMethod paletteMiningMethod)

Parameters

image RasterImage

The raster image.

entriesCount int

The desired entries count.

paletteMiningMethod PaletteMiningMethod

The palette mining method.

Returns

IColorPalette

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

Examples

The following example shows how to compress a PNG image, using indexed color with best fit palette```csharp [C#]

                                                                                                         // Loads png image        
                                                                                                             string  sourceFilePath="OriginalRings.png";
                                                                                                             string  outputFilePath="OriginalRingsOutput.png";
                                                                                                             using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(sourceFilePath))
                                                                                                         {
                                                                                                             image.Save(outputFilePath, new Aspose.Imaging.ImageOptions.PngOptions()
                                                                                                             {
                                                                                                                  Progressive = true,
                                                                                                                      // Use indexed color type
                                                                                                                  ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.IndexedColor,
                                                                                                                      // Use maximal compression
                                                                                                                  CompressionLevel = 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.
                                                                                                                  Palette = Aspose.Imaging.ColorPaletteHelper.GetCloseImagePalette((Aspose.Imaging.RasterImage)image, 256, Aspose.Imaging.PaletteMiningMethod.Histogram)
                                                                                                             });
                                                                                                         }
                                                                                                             // The output file size should be significantly reduced

### <a id="Aspose_Imaging_ColorPaletteHelper_GetCloseImagePalette_Aspose_Imaging_RasterImage_Aspose_Imaging_Rectangle_System_Int32_"></a> GetCloseImagePalette\(RasterImage, Rectangle, int\)

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.

```csharp
public static IColorPalette GetCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount)

Parameters

image RasterImage

The raster image.

destBounds Rectangle

The destination image bounds.

entriesCount int

The desired entries count.

Returns

IColorPalette

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

GetCloseImagePalette(RasterImage, Rectangle, int, bool)

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.

public static IColorPalette GetCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, bool useImagePalette)

Parameters

image RasterImage

The raster image.

destBounds Rectangle

The destination image bounds.

entriesCount int

The desired entries count.

useImagePalette bool

If 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, Rectangle, int, bool, Color)

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.

public static IColorPalette GetCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, bool useImagePalette, Color alphaBlendInColor)

Parameters

image RasterImage

The raster image.

destBounds Rectangle

The destination image bounds.

entriesCount int

The desired entries count.

useImagePalette bool

If set, it will use its own image palette if available

alphaBlendInColor Color

The 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, Rectangle, int, bool, Color, bool)

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.

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

Parameters

image RasterImage

The raster image.

destBounds Rectangle

The destination image bounds.

entriesCount int

The desired entries count.

useImagePalette bool

If set, it will use its own image palette if available

alphaBlendInColor Color

The color that should be used as a background color for semi-transparent alpha replacement.

keepTransparency bool

If 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.

GetCloseTransparentImagePalette(RasterImage, int)

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.

public static IColorPalette GetCloseTransparentImagePalette(RasterImage image, int entriesCount)

Parameters

image RasterImage

The raster image.

entriesCount int

The desired entries count.

Returns

IColorPalette

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

GetDownscalePalette(RasterImage)

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

public static ColorPalette GetDownscalePalette(RasterImage image)

Parameters

image RasterImage

The image.

Returns

ColorPalette

The Aspose.Imaging.ColorPalette.

GetUniformColorPalette(RasterImage)

Get uniform 256 color palette.

public static ColorPalette GetUniformColorPalette(RasterImage image)

Parameters

image RasterImage

The image.

Returns

ColorPalette

The Aspose.Imaging.ColorPalette.

HasTransparentColors(IColorPalette)

Determines whether the specified palette has transparent colors.

public static bool HasTransparentColors(IColorPalette palette)

Parameters

palette IColorPalette

The palette.

Returns

bool

true if the specified palette has transparent colors; otherwise, false.

Exceptions

ArgumentNullException

palette is null.