1 /*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * 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 * @since 1.2
40 */
41 public class GrayFilter extends RGBImageFilter {
42 private boolean brighter;
43 private int percent;
44
45 /**
46 * Creates a disabled image
47 *
48 * @param i an {@code Image} to be created as disabled
49 * @return the new grayscale image created from {@code i}
50 */
51 public static Image createDisabledImage (Image i) {
52 GrayFilter filter = new GrayFilter(true, 50);
53 ImageProducer prod = new FilteredImageSource(i.getSource(), filter);
54 Image grayImage = Toolkit.getDefaultToolkit().createImage(prod);
55 return grayImage;
56 }
57
58 /**
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 }
--- EOF ---