←Select platform

Colors Property

Summary
Gets or sets the number of colors to use in the palette
Syntax
C#
Objective-C
C++/CLI
Java
Python
public int Colors { get; set; } 
@property (nonatomic, assign) NSUInteger colors 
public int getColors() 
public void setColors(int value) 
public: 
property int Colors { 
   int get(); 
   void set (    int ); 
} 
Colors # get and set (ColorResolutionCommand) 

Property Value

An integer value that indicates the number of colors to use in the palette.

Remarks

If you specify a palette in the SetPalette method, pass the number of entries in your palette (including reserved entries). The value of the Colors property is not used.

If you pass null to the SetPalette method or leave it uninitialized and you specify ColorResolutionCommandPaletteFlags.Optimized in the PaletteFlags property, you can use the Colors property to limit the number of colors that the command maps in the optimized palette.

Example
C#
Java
 
import java.io.File; 
import java.io.IOException; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.imageprocessing.*; 
 
 
public void colorResolutionCommandExample() { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   RasterCodecs codecs = new RasterCodecs(); 
   String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "rgsref.cmp"); 
   String destFileName1 = combine(LEAD_VARS_IMAGES_DIR, "rgsref_colorres8.bmp"); 
   String destFileName2 = combine(LEAD_VARS_IMAGES_DIR, "rgsref_colorres4.bmp"); 
 
   // Load the source image from disk 
   RasterImage image = codecs.load(srcFileName); 
 
   // First color-res it to 8 bits per pixel using the Netscape palette into a 
   // seperate image 
   ColorResolutionCommand cmd = new ColorResolutionCommand(); 
   cmd.setMode(ColorResolutionCommandMode.CREATE_DESTINATION_IMAGE); 
   cmd.setBitsPerPixel(8); 
   cmd.setOrder(RasterByteOrder.RGB); 
   cmd.setDitheringMethod(RasterDitheringMethod.NONE); 
   cmd.setPaletteFlags(ColorResolutionCommandPaletteFlags.USE_PALETTE.getValue()); 
   cmd.setPalette(RasterPalette.netscape()); 
   cmd.run(image); 
 
   RasterImage destImage = cmd.getDestinationImage(); 
   assertTrue(destImage.getBitsPerPixel() == 8); 
 
   // Save it to disk 
   codecs.save(destImage, destFileName1, RasterImageFormat.BMP, 8); 
 
   assertTrue("file unsuccessfully saved to " + destFileName1, (new File(destFileName1)).exists()); 
   System.out.printf("File saved successfully to %s%n", destFileName1); 
 
   destImage.dispose(); 
 
   // Now color-res the image in-place to 4-bits per pixel using the Optimized 
   // palette 
   cmd.setMode(ColorResolutionCommandMode.IN_PLACE); 
   cmd.setBitsPerPixel(4); 
   cmd.setOrder(RasterByteOrder.RGB); 
   cmd.setDitheringMethod(RasterDitheringMethod.NONE); 
   cmd.setPaletteFlags(ColorResolutionCommandPaletteFlags.OPTIMIZED.getValue()); 
   cmd.run(image); 
   assertTrue(image.getBitsPerPixel() == 4); 
 
   // Save it to disk 
   codecs.save(image, destFileName2, RasterImageFormat.BMP, 4); 
 
   assertTrue("file unsuccessfully saved to " + destFileName1, (new File(destFileName1)).exists()); 
   System.out.printf("File saved successfully to %s%n", destFileName1); 
 
   // Clean Up 
   destImage.dispose(); 
   image.dispose(); 
   codecs.dispose(); 
} 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
 
 
public void ColorResolutionCommandExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"); 
   string destFileName1 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_colorres8.bmp"); 
   string destFileName2 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_colorres4.bmp"); 
 
   // Load the source image from disk 
   RasterImage image = codecs.Load(srcFileName); 
 
   // First color-res it to 8 bits per pixel  using the Netscape palette into a separate image 
   ColorResolutionCommand cmd = new ColorResolutionCommand(); 
   cmd.Mode = ColorResolutionCommandMode.CreateDestinationImage; 
   cmd.BitsPerPixel = 8; 
   cmd.Order = RasterByteOrder.Rgb; 
   cmd.DitheringMethod = RasterDitheringMethod.None; 
   cmd.PaletteFlags = ColorResolutionCommandPaletteFlags.UsePalette; 
   cmd.SetPalette(RasterPalette.Netscape()); 
   // cmd.InitAlpha is not used, since we're not converting to 32 or 64-bit. 
   cmd.Run(image); 
 
   RasterImage destImage = cmd.DestinationImage; 
   Assert.IsTrue(destImage.BitsPerPixel == 8); 
 
   // Save it to disk 
   codecs.Save(destImage, destFileName1, RasterImageFormat.Bmp, 8); 
   destImage.Dispose(); 
 
   // Now color-res the image in-place to 4-bits per pixel using the Optimized palette 
   cmd.Mode = ColorResolutionCommandMode.InPlace; 
   cmd.BitsPerPixel = 4; 
   cmd.Order = RasterByteOrder.Rgb; 
   cmd.DitheringMethod = RasterDitheringMethod.None; 
   cmd.PaletteFlags = ColorResolutionCommandPaletteFlags.Optimized; 
   cmd.SetPalette(null); 
   cmd.Colors = 15; // limit resulting palette to 15 colors. With 4 bits, it could be up to 16 colors. 
   cmd.Run(image); 
   int actualColors = image.GetPalette().Count(); 
   Assert.IsTrue(image.BitsPerPixel == 4 && actualColors <= 15); 
 
   // Save it to disk 
   codecs.Save(image, destFileName2, RasterImageFormat.Bmp, 4); 
 
   // Clean Up 
   destImage.Dispose(); 
   image.Dispose(); 
   codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
Requirements

Target Platforms

Help Version 23.0.2024.8.28
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.