ColorModel
A color model determines how colors are represented within AWT. ColorModel is an abstract class that you can subclass to specify your own representation for colors. AWT provides two concrete subclasses of ColorModel that you can use to build your own color model; they are DirectColorModel and IndexColorModel. These two correspond to the two ways computers represent colors internally.
Most modern computer systems use 24 bits to represent each pixel. These 24 bits contain 8 bits for each primary color (red, green, blue); each set of 8 bits represents the intensity of that color for the particular pixel. This arrangement yields the familiar "16 million colors" that you see in advertisements. It corresponds closely to Java's direct color model.
However, 24 bits per pixel, with something like a million pixels on the screen, adds up to a lot of memory. In the dark ages, memory was expensive, and devoting this much memory to a screen buffer cost too much. Therefore, designers used fewer bits--possibly as few as three, but more often eight--for each pixel. Instead of representing the colors directly in these bits, the bits were an index into a color map. Graphics programs would load the color map with the colors they were interested in and then represent each pixel by using the index of the appropriate color in the map. For example, the value 1 might represent fuschia; the value 2 might represent puce. Full information about how to display each color (the red, green, and blue components that make up fuschia or puce) is contained only in the color map. This arrangement corresponds closely to Java's indexed color model.
Because Java is platform-independent, you don't need to worry about how your computer or the user's computer represents colors. Your programs can use an indexed or direct color map as appropriate. Java will do the best it can to render the colors you request. Of course, if you use 5,000 colors on a computer that can only display 256, Java is going to have to make compromises. It will decide which colors to put in the color map and which colors are close enough to the colors in the color map, but that's done behind your back.
Java's default color model uses 8 bits per pixel for red, green, and blue, along with another 8 bits for alpha (transparency) level. However, as I said earlier, you can create your own ColorModel if you want to work in some other scheme. For example, you could create a grayscale color model for black and white pictures, or an HSB (hue, saturation, brightness) color model if you are more comfortable working with this system. Your color model's job will be to take a pixel value in your representation and translate that value into the corresponding alpha, red, green, and blue values. If you are working with a grayscale image, your image producer could deliver grayscale values to the image consumer, plus a ColorModel that tells the consumer how to render these gray values in terms of ARGB components.
ColorModel Methods
Constructors- public ColorModel (int bits)
- There is a single constructor for
ColorModel. It has one parameter,bits, which describes the number of bits required per pixel of an image. Since this is an abstract class, you cannot call this constructor directly. Since each pixel value must be stored within an integer, the maximum value forbitsis 32. If you request more, you get 32.
- public static ColorModel getRGBdefault()
- The
getRGBdefault()method returns the defaultColorModel,which has 8 bits for each of the components alpha, red, green, and blue. The order the pixels are stored in an integer is 0xAARRGGBB, or alpha in highest order byte, down to blue in the lowest.
- public int getPixelSize ()
- The
getPixelSize()method returns the number of bits required for each pixel as described by this color model. That is, it returns the number of bits passed to the constructor. - public abstract int getAlpha (int pixel)
- The
getAlpha()method returns the alpha component ofpixelfor a color model. Its range must be between 0 and 255, inclusive. A value of 0 means the pixel is completely transparent and the background will appear through the pixel. A value of 255 means the pixel is opaque and you cannot see the background behind it. - public abstract int getRed (int pixel)
- The
getRed()method returns the red component ofpixelfor a color model. Its range must be between 0 and 255, inclusive. A value of 0 means the pixel has no red in it. A value of 255 means red is at maximum intensity. - public abstract int getGreen (int pixel)
- The
getGreen()method returns the green component ofpixelfor a color model. Its range must be between 0 and 255, inclusive. A value of 0 means the pixel has no green in it. A value of 255 means green is at maximum intensity. - public abstract int getBlue (int pixel)
- The
getBlue()method returns the blue component ofpixelfor a color model. Its range must be between 0 and 255, inclusive. A value of 0 means the pixel has no blue in it. A value of 255 means blue is at maximum intensity. - public int getRGB(int pixel)
- The
getRGB()method returns the color ofpixelin the default RGB color model. If a subclass has changed the ordering or size of the different color components,getRGB()will return the pixel in the RGB color model (0xAARRGGBB order). In theory, the subclass does not need to override this method, unless it wants to make it final. Making this method final may yield a significant performance improvement. - public void finalize ()
- The garbage collector calls
finalize()when it determines that theColorModelobject is no longer needed.finalize()frees any internal resources that theColorModelobject has used.
DirectColorModel
The DirectColorModel class is a concrete subclass of ColorModel. It specifies a color model in which each pixel contains all the color information (alpha, red, green, and blue values) explicitly. Pixels are represented by 32-bit (int) quantities; the constructor lets you change which bits are allotted to each component.
All of the methods in this class, except constructors, are final, because of assumptions made by the implementation. You can create subclasses of DirectColorModel, but you can't override any of its methods. However, you should not need to develop your own subclass. Just create an instance of DirectColorModel with the appropriate constructor. Any subclassing results in serious performance degradation, because you are going from fast, static final method calls to dynamic method lookups.Constructors
- public DirectColorModel (int bits, int redMask, int greenMask, int blueMask, int alphaMask)
- This constructor creates a
DirectColorModelin whichbitsrepresents the total number of bits used to represent a pixel; it must be less than or equal to 32. TheredMask,greenMask,blueMask, andalphaMaskspecify where in a pixel each color component exists. Each of the bit masks must be contiguous (e.g., red cannot be the first, fourth, and seventh bits of the pixel), must be smaller than 2^bits, and should not exceed 8 bits. (You cannot display more than 8 bits of data for any color component, but the mask can be larger.) Combined, the masks together should bebitsin length. The default RGB color model is:new DirectColorModel (32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000)
The run-time exception
IllegalArgumentExceptionis thrown if any of the following occur:- The bits that are set in a mask are not contiguous.
- Mask bits overlap (i.e., the same bit is set in two or more masks).
- The number of mask bits exceeds
bits.
- public DirectColorModel (int bits, int redMask, int greenMask, int blueMask)
- This constructor for
DirectColorModelcalls the first with an alpha mask of 0, which means that colors in this color model have no transparency component. All colors will be fully opaque with an alpha value of 255. The same restrictions for the red, green, and blue masks apply.
- final public int getAlpha (int pixel)
- The
getAlpha()method returns the alpha component ofpixelfor the color model as a number from 0 to 255, inclusive. A value of 0 means the pixel is completely transparent, and the background will appear through the pixel. A value of 255 means the pixel is opaque, and you cannot see the background behind it. - final public int getRed (int pixel)
- The
getRed()method returns the red component ofpixelfor the color model. Its range is from 0 to 255. A value of 0 means the pixel has no red in it. A value of 255 means red is at maximum intensity. - final public int getGreen (int pixel)
- The
getGreen()method returns the green component ofpixelfor the color model. Its range is from 0 to 255. A value of 0 means the pixel has no green in it. A value of 255 means green is at maximum intensity. - final public int getBlue (int pixel)
- The
getBlue()method returns the blue component ofpixelfor the color model. Its range is from 0 to 255. A value of 0 means the pixel has no blue in it. A value of 255 means blue is at maximum intensity. - final public int getRGB (int pixel)
- The
getRGB()method returns the color ofpixelin the default RGB color model. If a subclass has changed the ordering or size of the different color components,getRGB()will return the pixel in the RGB color model (0xAARRGGBB order). ThegetRGB()method in this subclass is identical to the method inColorModelbut overrides it to make it final.
- final public int getAlphaMask ()
- The
getAlphaMask()method returns thealphaMaskfrom theDirectColorModelconstructor (or 0 if constructor did not havealphaMask). ThealphaMaskspecifies which bits in the pixel represent the alpha transparency component of the color model. - final public int getRedMask ()
- The
getRedMask()method returns theredMaskfrom theDirectColorModelconstructor. TheredMaskspecifies which bits in the pixel represent the red component of the color model. - final public int getGreenMask ()
- The
getGreenMask()method returns thegreenMaskfrom theDirectColorModelconstructor. ThegreenMaskspecifies which bits in the pixel represent the green component of the color model. - final public int getBlueMask ()
- The
getBlueMask()method returns theblueMaskfrom theDirectColorModelconstructor. TheblueMaskspecifies which bits in the pixel represent the blue component of the color model.
IndexColorModel
The IndexColorModel is another concrete subclass of ColorModel. It specifies a ColorModel that uses a color map lookup table (with a maximum size of 256), rather than storing color information in the pixels themselves. Pixels are represented by an index into the color map, which is at most an 8-bit quantity. Each entry in the color map gives the alpha, red, green, and blue components of some color. One entry in the map can be designated "transparent." This is called the "transparent pixel"; the alpha component of this map entry is ignored.
All of the methods in this class, except constructors, are final because of assumptions made by the implementation. You shouldn't need to create subclasses; you can if necessary, but you can't override any of the IndexColorModel methods. Example 12.2 (later in this chapter) uses an IndexColorModel. Constructors
There are two sets of constructors for IndexColorModel. The first two constructors use a single-byte array for the color map. The second group implements the color map with separate byte arrays for each color component.
- public IndexColorModel (int bits, int size, byte colorMap[], int start, boolean hasalpha, int transparent)
- This constructor creates an
IndexColorModel.bitsis the number of bits used to represent each pixel and must not exceed 8.sizeis the number of elements in the map; it must be less than 2^bits.hasalphashould betrueif the color map includes alpha (transparency) components andfalseif it doesn't.transparentis the location of the transparent pixel in the map (i.e., the pixel value that is considered transparent). If there is no transparent pixel, set transparent to -1.The
colorMapdescribes the colors used to paint pixels.startis the index within thecolorMaparray at which the map begins; prior elements of the array are ignored. An entry in the map consists of three or four consecutive bytes, representing the red, green, blue, and (optionally) alpha components. Ifhasalphaisfalse, a map entry consists of three bytes, and no alpha components are present; ifhasalphaistrue, map entries consist of four bytes, and all four components must be present.For example, consider a pixel whose value is
p, and a color map with ahasalphaset tofalse. Therefore, each element in the color map occupies three consecutive array elements. The red component of that pixel will be located atcolorMap[start + 3*p]; the green component will be atcolorMap[start + 3*p + 1]; and so on. The value of size may be smaller than 2^bits, meaning that there may be pixel values with no corresponding entry in the color map. These pixel values (i.e.,size <= p< 2^bits) are painted with the color components set to 0; they are transparent ifhasalphaistrue, opaque otherwise.If
bitsis too large (greater than 8),sizeis too large (greater than 2^bits), or thecolorMaparray is too small to hold the map, the run-time exceptionArrayIndexOutOfBoundsExceptionwill be thrown. - public IndexColorModel (int bits, int size, byte colorMap[], int start, boolean hasalpha)
- This version of the
IndexColorModelconstructor calls the previous constructor with atransparentindex of -1; that is, there is no transparent pixel. Ifbitsis too large (greater than 8), orsizeis too large (greater than 2^bits), or thecolorMaparray is too small to hold the map, the run-time exception,ArrayIndexOutOfBoundsExceptionwill be thrown. - public IndexColorModel (int bits, int size, byte red[], byte green[], byte blue[], int transparent)
- The second set of constructors for
IndexColorModelis similar to the first group, with the exception that these constructors use three or four separate arrays (one per color component) to represent the color map, instead of a single array.The
bitsparameter still represents the number of bits in a pixel.sizerepresents the number of elements in the color map.transparentis the location of the transparent pixel in the map (i.e., the pixel value that is considered transparent). If there is no transparent pixel, settransparentto -1.The
red,green, andbluearrays contain the color map itself. These arrays must have at leastsizeelements. They contain the red, green, and blue components of the colors in the map. For example, if a pixel is at positionp,red[p]contains the pixel's red component;green[p]contains the green component; andblue[p]contains the blue component. The value ofsizemay be smaller than 2^bits, meaning that there may be pixel values with no corresponding entry in the color map. These pixel values (i.e.,size <= p <2^bits) are painted with the color components set to 0.If
bitsis too large (greater than 8),sizeis too large (greater than 2^bits), or the red, green, and blue arrays are too small to hold the map, the run-time exceptionArrayIndexOutOfBoundsExceptionwill be thrown. - public IndexColorModel (int bits, int size, byte red[], byte green[], byte blue[])
- This version of the
IndexColorModelconstructor calls the previous one with atransparentindex of -1; that is, there is no transparent pixel. Ifbitsis too large (greater than 8),sizeis too large (greater than 2^bits), or the red, green, and blue arrays are too small to hold the map, the run-time exceptionArrayIndexOutOfBoundsExceptionwill be thrown. - public IndexColorModel (int bits, int size, byte red[], byte green[], byte blue[], byte alpha[])
- Like the previous constructor, this version creates an
IndexColorModelwith no transparent pixel. It differs from the previous constructor in that it supports transparency; the array alpha contains the map's transparency values. Ifbitsis too large (greater than 8),sizeis too large (greater than 2^bits), or the red, green, blue, and alpha arrays are too small to hold the map, the run-time exceptionArrayIndexOutOfBoundsExceptionwill be thrown.
- final public int getAlpha (int pixel)
- The
getAlpha()method returns the alpha component ofpixelfor a color model, which is a number between 0 and 255, inclusive. A value of 0 means the pixel is completely transparent and the background will appear through the pixel. A value of 255 means the pixel is opaque and you cannot see the background behind it. - final public int getRed (int pixel)
- The
getRed()method returns the red component ofpixelfor a color model, which is a number between 0 and 255, inclusive. A value of 0 means the pixel has no red in it. A value of 255 means red is at maximum intensity. - final public int getGreen (int pixel)
- The
getGreen()method returns the green component ofpixelfor a color model, which is a number between 0 and 255, inclusive. A value of 0 means the pixel has no green in it. A value of 255 means green is at maximum intensity. - final public int getBlue (int pixel)
- The
getBlue()method returns the blue component ofpixelfor a color model, which is a number between 0 and 255, inclusive. A value of 0 means the pixel has no blue in it. A value of 255 means blue is at maximum intensity. - final public int getRGB (int pixel)
- The
getRGB()method returns the color ofpixelin the default RGB color model. If a subclass has changed the ordering or size of the different color components,getRGB()will return the pixel in the RGB color model (0xAARRGGBB order). This version ofgetRGBis identical to the version in theColorModelclass but overrides it to make it final.
- final public int getMapSize()
- The
getMapSize()method returns the size of the color map (i.e., the number of distinct colors). - final public int getTransparentPixel ()
- The
getTransparentPixel()method returns the color map index for the transparent pixel in the color model. If no transparent pixel exists, it returns -1. It is not possible to change the transparent pixel after the color model has been created. - final public void getAlphas (byte alphas[])
- The
getAlphas()method copies the alpha components of theColorModelinto elements 0 throughgetMapSize()-1of thealphasarray. Space must already be allocated in thealphasarray. - final public void getReds (byte reds[])
- The
getReds()method copies the red components of theColorModelinto elements 0 throughgetMapSize()-1of theredsarray. Space must already be allocated in theredsarray. - final public void getGreens (byte greens[])
- The
getGreens()method copies the green components of theColorModelinto elements 0 throughgetMapSize()-1of thegreensarray. Space must already be allocated in thegreensarray. - final public void getBlues (byte blues[])
- The
getBlues()method copies the blue components of theColorModelinto elements 0 throughgetMapSize()-1of thebluesarray. Space must already be allocated in thebluesarray.