home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / Source.bin / DarkenFilter.java < prev    next >
Encoding:
Java Source  |  1998-09-14  |  3.0 KB  |  122 lines

  1. package symantec.itools.awt.image;
  2.  
  3. import java.awt.image.RGBImageFilter;
  4. import java.awt.image.ColorModel;
  5. import java.awt.image.DirectColorModel;
  6. import java.lang.IllegalArgumentException;
  7. import java.util.ResourceBundle;
  8. import java.text.MessageFormat;
  9.  
  10. // Written by Levi Brown and Micheal Hopkins 1.1, July 8, 1997.
  11.  
  12. /**
  13.  * An Image filter to use for darkening an Image a specified percentage.
  14.  * @version 1.1, July 8, 1997
  15.  * @author  Symantec
  16.  */
  17. public class DarkenFilter extends RGBImageFilter
  18. {
  19.     /**
  20.      * Constructs a default DarkenFilter.
  21.      * By default the Image is darkened 50%.
  22.      * @see #DarkenFilter(double)
  23.      * @see #setPercent
  24.      */
  25.     public DarkenFilter()
  26.     {
  27.         this(0.50);
  28.     }
  29.  
  30.     /**
  31.      * Constructs a DarkenFilter.
  32.      * @param percent the percent to darken the image when filtering.
  33.      * @see #DarkenFilter()
  34.      * @see #setPercent
  35.      */
  36.     public DarkenFilter(double percent)
  37.     {        
  38.         try
  39.         {
  40.             errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  41.         }
  42.         catch(Throwable ex)
  43.         {
  44.             errors = new symantec.itools.resources.ErrorsBundle();
  45.         }
  46.  
  47.         canFilterIndexColorModel = true;
  48.         try
  49.         {
  50.             setPercent(percent);
  51.         }
  52.         catch (IllegalArgumentException exc)
  53.         {
  54.             Object[] args = { new Double(percent) };
  55.             System.err.println("DarkenFilter: " + errors.getString("InvalidPercent1"));
  56.             System.err.println("     " + errors.getString("InvalidPercent2"));
  57.             System.err.println("     " + errors.getString("InvalidPercent3"));
  58.             try { setPercent(0.50); } catch (IllegalArgumentException exc2) {}
  59.         }
  60.     }
  61.  
  62.     /**
  63.      * Sets the percentage to fade when filtering.
  64.      * @param percent the percentage to fade.
  65.      * @exception IllegalArgumentException
  66.      * if the specified percentage value is unacceptable
  67.      * @see #getPercent
  68.      */
  69.     public void setPercent(double percent) throws IllegalArgumentException
  70.     {
  71.         symantec.itools.util.GeneralUtils.checkValidPercent(percent);
  72.  
  73.         this.percent = percent;
  74.     }
  75.  
  76.     /**
  77.      * Gets the percentage to fade when filtering.
  78.      * @return the percentage to fade.
  79.      * @see #setPercent
  80.      */
  81.     public double getPercent()
  82.     {
  83.         return percent;
  84.     }
  85.  
  86.     /**
  87.      * Filters an RGB value by the current fade percentage.
  88.      * @param x unused
  89.      * @param y unused
  90.      * @param rgb the rgb value to fade
  91.      * @return the faded rgb value
  92.      */
  93.     public int filterRGB( int x, int y, int rgb )
  94.     {
  95.         DirectColorModel cm = (DirectColorModel)ColorModel.getRGBdefault();
  96.  
  97.         int alpha = cm.getAlpha(rgb);
  98.         int red   = cm.getRed(rgb);
  99.         int green = cm.getGreen(rgb);
  100.         int blue  = cm.getBlue(rgb);
  101.  
  102.         red        = Math.max((int)(red    * (1 - percent)), 0);
  103.         green    = Math.max((int)(green    * (1 - percent)), 0);
  104.         blue    = Math.max((int)(blue    * (1 - percent)), 0);
  105.  
  106.         alpha    = alpha << 24;
  107.         red        = red   << 16;
  108.         green    = green << 8;
  109.  
  110.         return alpha | red | green | blue;
  111.     }
  112.  
  113.     /**
  114.      * The percentage to fade when filtering.
  115.      * @see #getPercent
  116.      * @see #setPercent
  117.      */
  118.     protected double percent;
  119.  
  120.     transient protected ResourceBundle errors;
  121. }
  122.