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 / FadeFilter.java < prev    next >
Encoding:
Java Source  |  1998-09-14  |  5.5 KB  |  219 lines

  1. package symantec.itools.awt.image;
  2.  
  3. import java.awt.Color;
  4. import java.awt.image.RGBImageFilter;
  5. import java.awt.image.ColorModel;
  6. import java.awt.image.DirectColorModel;
  7. import java.lang.IllegalArgumentException;
  8. import java.util.ResourceBundle;
  9. import java.text.MessageFormat;
  10.  
  11. // Written by Levi Brown and Micheal Hopkins 1.1, July 8, 1997.
  12.  
  13. /**
  14.  * An Image filter to use for fading an Image towards a specified Color by a specified percent.
  15.  * @version 1.1, July 8, 1997
  16.  * @author  Symantec
  17.  */
  18. public class FadeFilter extends RGBImageFilter
  19. {
  20.     /**
  21.      * Constructs a default FadeFilter.
  22.      * By default the Image is faded 50% towards Color.lightGray.
  23.      * @see #FadeFilter(double)
  24.      * @see #FadeFilter(java.awt.Color)
  25.      * @see #FadeFilter(double, java.awt.Color)
  26.      * @see #setPercent
  27.      * @see #setFadeToColor
  28.      */
  29.     public FadeFilter()
  30.     {
  31.         this(0.50, Color.lightGray);
  32.     }
  33.  
  34.     /**
  35.      * Constructs a FadeFilter.
  36.      * By default the Image is faded towards Color.lightGray.
  37.      * @param percent the percent to fade towards the specified color
  38.      * @see #FadeFilter()
  39.      * @see #FadeFilter(java.awt.Color)
  40.      * @see #FadeFilter(double, java.awt.Color)
  41.      * @see #setFadeToColor
  42.      */
  43.     public FadeFilter(double percent)
  44.     {
  45.         this(percent, Color.lightGray);
  46.     }
  47.  
  48.     /**
  49.      * Constructs a FadeFilter.
  50.      * By default the Image is faded 50%.
  51.      * @param fadeToColor the color to fade to
  52.      * @see #FadeFilter()
  53.      * @see #FadeFilter(double)
  54.      * @see #FadeFilter(double, java.awt.Color)
  55.      * @see #setPercent
  56.      */
  57.     public FadeFilter(Color fadeToColor)
  58.     {
  59.         this(0.50, fadeToColor);
  60.     }
  61.  
  62.     /**
  63.      * Constructs a FadeFilter.
  64.      * @param percent the percent to fade towards the specified color
  65.      * @param fadeToColor the color to fade to
  66.      * @see #FadeFilter()
  67.      * @see #FadeFilter(double)
  68.      * @see #FadeFilter(java.awt.Color)
  69.      */
  70.     public FadeFilter(double percent, Color fadeToColor)
  71.     {        
  72.         try
  73.         {
  74.             errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  75.         }
  76.         catch(Throwable ex)
  77.         {
  78.             errors = new symantec.itools.resources.ErrorsBundle();
  79.         }
  80.         
  81.         canFilterIndexColorModel = true;
  82.         try
  83.         {
  84.             setPercent(percent);
  85.         }
  86.         catch (IllegalArgumentException exc)
  87.         {
  88.             Object[] args = { new Double(percent) };
  89.             System.err.println("FadeFilter: " + errors.getString("InvalidPercent1"));
  90.             System.err.println("     " + errors.getString("InvalidPercent2"));
  91.             System.err.println("     " + errors.getString("InvalidPercent3"));
  92.             try { setPercent(0.50); } catch (IllegalArgumentException exc2) {}
  93.         }
  94.  
  95.         try
  96.         {
  97.             setFadeToColor(fadeToColor);
  98.         }
  99.         catch (NullPointerException exc)
  100.         {
  101.             System.err.println("FadeFilter: Null parameter value passed to constructor:");
  102.             System.err.println("     fadeToColor is null");
  103.             System.err.println("     Defaulting to Color.lightGray.");
  104.             setFadeToColor(Color.lightGray);
  105.         }
  106.     }
  107.  
  108.  
  109.     /**
  110.      * Sets the color to fade to when filtering.
  111.      * @param fadeTo the color to fade to
  112.      * @see #getFadeToColor
  113.      */
  114.     public void setFadeToColor(Color fadeTo)
  115.     {
  116.         to_r = fadeTo.getRed();
  117.         to_g = fadeTo.getGreen();
  118.         to_b = fadeTo.getBlue();
  119.     }
  120.  
  121.     /**
  122.      * Gets the color used to fade to when filtering.
  123.      * @return the color used to fade to
  124.      * @see #setFadeToColor
  125.      */
  126.     public Color getFadeToColor()
  127.     {
  128.         return new Color(to_r,    to_g, to_b);
  129.     }
  130.  
  131.     /**
  132.      * Sets the percentage to fade when filtering.
  133.      * @param percent the percentage to fade
  134.      * @exception IllegalArgumentException
  135.      * if the specified percentage value is unacceptable
  136.      * @see #getPercent
  137.      */
  138.     public void setPercent(double percent) throws IllegalArgumentException
  139.     {
  140.         symantec.itools.util.GeneralUtils.checkValidPercent(percent);
  141.  
  142.         this.percent = percent;
  143.     }
  144.  
  145.     /**
  146.      * Gets the percentage to fade when filtering.
  147.      * @return the percentage to fade
  148.      * @see #setPercent
  149.      */
  150.     public double getPercent()
  151.     {
  152.         return percent;
  153.     }
  154.  
  155.     /**
  156.      * Filters an RGB value using the current fade settings.
  157.      * @param x unused
  158.      * @param y unused
  159.      * @param rgb the rgb value to fade
  160.      * @return the faded rgb value
  161.      */
  162.     public int filterRGB( int x, int y, int rgb )
  163.     {
  164.         DirectColorModel cm = (DirectColorModel)ColorModel.getRGBdefault();
  165.  
  166.         int alpha    = cm.getAlpha(rgb);
  167.         int from_r    = cm.getRed(rgb);
  168.         int from_g    = cm.getGreen(rgb);
  169.         int from_b    = cm.getBlue(rgb);
  170.         int r, g, b;
  171.  
  172.         if (from_r > to_r)
  173.             r = to_r + (int)((from_r - to_r)* (1 - percent));
  174.         else
  175.             r = to_r - (int)((to_r - from_r)* (1 - percent));
  176.         if (from_g > to_r)
  177.             g = to_g + (int)((from_g - to_g)* (1 - percent));
  178.         else
  179.             g = to_g - (int)((to_g - from_g)* (1 - percent));
  180.         if (from_b > to_b)
  181.             b = to_b + (int)((from_b - to_b)* (1 - percent));
  182.         else
  183.             b = to_b - (int)((to_b - from_b)* (1 - percent));
  184.  
  185.         alpha    = alpha << 24;
  186.         r        = r        << 16;
  187.         g        = g        << 8;
  188.  
  189.         return alpha | r | g | b;
  190.     }
  191.  
  192.     /**
  193.      * The percentage to fade when filtering.
  194.      * @see #getPercent
  195.      * @see #setPercent
  196.      */
  197.     protected double percent;
  198.     /**
  199.      * The red value of the color to fade to when filtering.
  200.      * @see #getFadeToColor
  201.      * @see #setFadeToColor
  202.      */
  203.     protected int to_r;
  204.     /**
  205.      * The green value of the color to fade to when filtering.
  206.      * @see #getFadeToColor
  207.      * @see #setFadeToColor
  208.      */
  209.     protected int to_g;
  210.     /**
  211.      * The blue value of the color to fade to when filtering.
  212.      * @see #getFadeToColor
  213.      * @see #setFadeToColor
  214.      */
  215.     protected int to_b;
  216.  
  217.     transient protected ResourceBundle errors;
  218. }
  219.