01 /*
02 * Copyright 1997-1998 Sun Microsystems, Inc. All Rights Reserved.
03 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04 *
05 * This code is free software; you can redistribute it and/or modify it
06 * under the terms of the GNU General Public License version 2 only, as
07 * published by the Free Software Foundation. Sun designates this
08 * particular file as subject to the "Classpath" exception as provided
09 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25 package javax.swing;
26
27 import java.awt.*;
28 import java.awt.image.*;
29
30 /**
31 * An image filter that "disables" an image by turning
32 * it into a grayscale image, and brightening the pixels
33 * in the image. Used by buttons to create an image for
34 * a disabled button.
35 *
36 * @author Jeff Dinkins
37 * @author Tom Ball
38 * @author Jim Graham
39 * @version 1.22 05/05/07
40 */
41 public class GrayFilter extends RGBImageFilter {
42 private boolean brighter;
43 private int percent;
44
45 /**
46 * Creates a disabled image
47 */
48 public static Image createDisabledImage(Image i) {
49 GrayFilter filter = new GrayFilter(true, 50);
50 ImageProducer prod = new FilteredImageSource(i.getSource(),
51 filter);
52 Image grayImage = Toolkit.getDefaultToolkit().createImage(prod);
53 return grayImage;
54 }
55
56 /**
57 * Constructs a GrayFilter object that filters a color image to a
58 * grayscale image. Used by buttons to create disabled ("grayed out")
59 * button images.
60 *
61 * @param b a boolean -- true if the pixels should be brightened
62 * @param p an int in the range 0..100 that determines the percentage
63 * of gray, where 100 is the darkest gray, and 0 is the lightest
64 */
65 public GrayFilter(boolean b, int p) {
66 brighter = b;
67 percent = p;
68
69 // canFilterIndexColorModel indicates whether or not it is acceptable
70 // to apply the color filtering of the filterRGB method to the color
71 // table entries of an IndexColorModel object in lieu of pixel by pixel
72 // filtering.
73 canFilterIndexColorModel = true;
74 }
75
76 /**
77 * Overrides <code>RGBImageFilter.filterRGB</code>.
78 */
79 public int filterRGB(int x, int y, int rgb) {
80 // Use NTSC conversion formula.
81 int gray = (int) ((0.30 * ((rgb >> 16) & 0xff) + 0.59
82 * ((rgb >> 8) & 0xff) + 0.11 * (rgb & 0xff)) / 3);
83
84 if (brighter) {
85 gray = (255 - ((255 - gray) * (100 - percent) / 100));
86 } else {
87 gray = (gray * (100 - percent) / 100);
88 }
89
90 if (gray < 0)
91 gray = 0;
92 if (gray > 255)
93 gray = 255;
94 return (rgb & 0xff000000) | (gray << 16) | (gray << 8)
95 | (gray << 0);
96 }
97 }
|