< prev index next >

src/java.desktop/share/classes/javax/swing/GrayFilter.java

Print this page




  59      * Constructs a GrayFilter object that filters a color image to a
  60      * grayscale image. Used by buttons to create disabled ("grayed out")
  61      * button images.
  62      *
  63      * @param b  a boolean -- true if the pixels should be brightened
  64      * @param p  an int in the range 0..100 that determines the percentage
  65      *           of gray, where 100 is the darkest gray, and 0 is the lightest
  66      */
  67     public GrayFilter(boolean b, int p) {
  68         brighter = b;
  69         percent = p;
  70 
  71         // canFilterIndexColorModel indicates whether or not it is acceptable
  72         // to apply the color filtering of the filterRGB method to the color
  73         // table entries of an IndexColorModel object in lieu of pixel by pixel
  74         // filtering.
  75         canFilterIndexColorModel = true;
  76     }
  77 
  78     /**
  79      * Overrides <code>RGBImageFilter.filterRGB</code>.
  80      */
  81     public int filterRGB(int x, int y, int rgb) {
  82         // Use NTSC conversion formula.
  83         int gray = (int)((0.30 * ((rgb >> 16) & 0xff) +
  84                          0.59 * ((rgb >> 8) & 0xff) +
  85                          0.11 * (rgb & 0xff)) / 3);
  86 
  87         if (brighter) {
  88             gray = (255 - ((255 - gray) * (100 - percent) / 100));
  89         } else {
  90             gray = (gray * (100 - percent) / 100);
  91         }
  92 
  93         if (gray < 0) gray = 0;
  94         if (gray > 255) gray = 255;
  95         return (rgb & 0xff000000) | (gray << 16) | (gray << 8) | (gray << 0);
  96     }
  97 }


  59      * Constructs a GrayFilter object that filters a color image to a
  60      * grayscale image. Used by buttons to create disabled ("grayed out")
  61      * button images.
  62      *
  63      * @param b  a boolean -- true if the pixels should be brightened
  64      * @param p  an int in the range 0..100 that determines the percentage
  65      *           of gray, where 100 is the darkest gray, and 0 is the lightest
  66      */
  67     public GrayFilter(boolean b, int p) {
  68         brighter = b;
  69         percent = p;
  70 
  71         // canFilterIndexColorModel indicates whether or not it is acceptable
  72         // to apply the color filtering of the filterRGB method to the color
  73         // table entries of an IndexColorModel object in lieu of pixel by pixel
  74         // filtering.
  75         canFilterIndexColorModel = true;
  76     }
  77 
  78     /**
  79      * Overrides {@code RGBImageFilter.filterRGB}.
  80      */
  81     public int filterRGB(int x, int y, int rgb) {
  82         // Use NTSC conversion formula.
  83         int gray = (int)((0.30 * ((rgb >> 16) & 0xff) +
  84                          0.59 * ((rgb >> 8) & 0xff) +
  85                          0.11 * (rgb & 0xff)) / 3);
  86 
  87         if (brighter) {
  88             gray = (255 - ((255 - gray) * (100 - percent) / 100));
  89         } else {
  90             gray = (gray * (100 - percent) / 100);
  91         }
  92 
  93         if (gray < 0) gray = 0;
  94         if (gray > 255) gray = 255;
  95         return (rgb & 0xff000000) | (gray << 16) | (gray << 8) | (gray << 0);
  96     }
  97 }
< prev index next >