blob: c76ed58a75ee9568bdf4f684f825a77a1770f27c [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-1998 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * 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 */
25package javax.swing;
26
27import java.awt.*;
28import 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 */
40public class GrayFilter extends RGBImageFilter {
41 private boolean brighter;
42 private int percent;
43
44 /**
45 * Creates a disabled image
46 */
47 public static Image createDisabledImage (Image i) {
48 GrayFilter filter = new GrayFilter(true, 50);
49 ImageProducer prod = new FilteredImageSource(i.getSource(), filter);
50 Image grayImage = Toolkit.getDefaultToolkit().createImage(prod);
51 return grayImage;
52 }
53
54 /**
55 * Constructs a GrayFilter object that filters a color image to a
56 * grayscale image. Used by buttons to create disabled ("grayed out")
57 * button images.
58 *
59 * @param b a boolean -- true if the pixels should be brightened
60 * @param p an int in the range 0..100 that determines the percentage
61 * of gray, where 100 is the darkest gray, and 0 is the lightest
62 */
63 public GrayFilter(boolean b, int p) {
64 brighter = b;
65 percent = p;
66
67 // canFilterIndexColorModel indicates whether or not it is acceptable
68 // to apply the color filtering of the filterRGB method to the color
69 // table entries of an IndexColorModel object in lieu of pixel by pixel
70 // filtering.
71 canFilterIndexColorModel = true;
72 }
73
74 /**
75 * Overrides <code>RGBImageFilter.filterRGB</code>.
76 */
77 public int filterRGB(int x, int y, int rgb) {
78 // Use NTSC conversion formula.
79 int gray = (int)((0.30 * ((rgb >> 16) & 0xff) +
80 0.59 * ((rgb >> 8) & 0xff) +
81 0.11 * (rgb & 0xff)) / 3);
82
83 if (brighter) {
84 gray = (255 - ((255 - gray) * (100 - percent) / 100));
85 } else {
86 gray = (gray * (100 - percent) / 100);
87 }
88
89 if (gray < 0) gray = 0;
90 if (gray > 255) gray = 255;
91 return (rgb & 0xff000000) | (gray << 16) | (gray << 8) | (gray << 0);
92 }
93}