home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / ColorModel.java < prev    next >
Text File  |  1996-05-03  |  4KB  |  121 lines

  1. /*
  2.  * @(#)ColorModel.java    1.11 96/02/23 Jim Graham
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.awt.image;
  21.  
  22. /**
  23.  * A class that encapsulates the methods for translating from pixel values
  24.  * to alpha, red, green, and blue color components for an image.  This
  25.  * class is abstract.
  26.  *
  27.  * @see IndexColorModel
  28.  * @see DirectColorModel
  29.  *
  30.  * @version    1.11 23 Feb 1996
  31.  * @author     Jim Graham
  32.  */
  33. public abstract class ColorModel {
  34.     private int pData;        // Placeholder for data for native functions
  35.  
  36.     protected int pixel_bits;
  37.  
  38.     private static ColorModel RGBdefault;
  39.  
  40.     /**
  41.      * Return a ColorModel which describes the default format for
  42.      * integer RGB values used throughout the AWT image interfaces.
  43.      * The format for the RGB values is an integer with 8 bits
  44.      * each of alpha, red, green, and blue color components ordered
  45.      * correspondingly from the most significant byte to the least
  46.      * significant byte, as in:  0xAARRGGBB
  47.      */
  48.     public static ColorModel getRGBdefault() {
  49.     if (RGBdefault == null) {
  50.         RGBdefault = new DirectColorModel(32,
  51.                           0x00ff0000,    // Red
  52.                           0x0000ff00,    // Green
  53.                           0x000000ff,    // Blue
  54.                           0xff000000    // Alpha
  55.                           );
  56.     }
  57.     return RGBdefault;
  58.     }
  59.  
  60.     /**
  61.      * Constructs a ColorModel which describes a pixel of the specified
  62.      * number of bits.
  63.      */
  64.     public ColorModel(int bits) {
  65.     pixel_bits = bits;
  66.     }
  67.  
  68.     /**
  69.      * Returns the number of bits per pixel described by this ColorModel.
  70.      */
  71.     public int getPixelSize() {
  72.     return pixel_bits;
  73.     }
  74.  
  75.     /**
  76.      * The subclass must provide a function which provides the red
  77.      * color compoment for the specified pixel.
  78.      * @return        The red color component ranging from 0 to 255
  79.      */
  80.     public abstract int getRed(int pixel);
  81.  
  82.     /**
  83.      * The subclass must provide a function which provides the green
  84.      * color compoment for the specified pixel.
  85.      * @return        The green color component ranging from 0 to 255
  86.      */
  87.     public abstract int getGreen(int pixel);
  88.  
  89.     /**
  90.      * The subclass must provide a function which provides the blue
  91.      * color compoment for the specified pixel.
  92.      * @return        The blue color component ranging from 0 to 255
  93.      */
  94.     public abstract int getBlue(int pixel);
  95.  
  96.     /**
  97.      * The subclass must provide a function which provides the alpha
  98.      * color compoment for the specified pixel.
  99.      * @return        The alpha transparency value ranging from 0 to 255
  100.      */
  101.     public abstract int getAlpha(int pixel);
  102.  
  103.     /**
  104.      * Returns the color of the pixel in the default RGB color model.
  105.      * @see ColorModel#getRGBdefault
  106.      */
  107.     public int getRGB(int pixel) {
  108.     return (getAlpha(pixel) << 24)
  109.         | (getRed(pixel) << 16)
  110.         | (getGreen(pixel) << 8)
  111.         | (getBlue(pixel) << 0);
  112.     }
  113.  
  114.     /* Throw away the compiled data stored in pData */
  115.     private native void deletepData();
  116.  
  117.     public void finalize() {
  118.     deletepData();
  119.     }
  120. }
  121.