GrayFilter.java revision 15514:82d35714476e
1223328Sgavin/*
279971Sobrien * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3223328Sgavin * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
479971Sobrien *
5223328Sgavin * This code is free software; you can redistribute it and/or modify it
679971Sobrien * under the terms of the GNU General Public License version 2 only, as
7223328Sgavin * published by the Free Software Foundation.  Oracle designates this
879971Sobrien * particular file as subject to the "Classpath" exception as provided
9223328Sgavin * by Oracle in the LICENSE file that accompanied this code.
1079971Sobrien *
11223328Sgavin * This code is distributed in the hope that it will be useful, but WITHOUT
1279971Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13223328Sgavin * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1479971Sobrien * version 2 for more details (a copy is included in the LICENSE file that
15223328Sgavin * accompanied this code).
16223328Sgavin *
1779971Sobrien * You should have received a copy of the GNU General Public License version
18223328Sgavin * 2 along with this work; if not, write to the Free Software Foundation,
1979971Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20223328Sgavin *
21223328Sgavin * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2279971Sobrien * or visit www.oracle.com if you need additional information or have any
23223328Sgavin * questions.
2479971Sobrien */
2579971Sobrienpackage javax.swing;
26223328Sgavin
2779971Sobrienimport java.awt.*;
28223328Sgavinimport java.awt.image.*;
29223328Sgavinimport sun.awt.image.MultiResolutionCachedImage;
3079971Sobrien
31223328Sgavin/**
3279971Sobrien * An image filter that "disables" an image by turning
33223328Sgavin * it into a grayscale image, and brightening the pixels
3479971Sobrien * in the image. Used by buttons to create an image for
35223328Sgavin * a disabled button.
3679971Sobrien *
37 * @author      Jeff Dinkins
38 * @author      Tom Ball
39 * @author      Jim Graham
40 * @since 1.2
41 */
42public class GrayFilter extends RGBImageFilter {
43    private boolean brighter;
44    private int percent;
45
46    /**
47     * Creates a disabled image
48     *
49     * @param i  an {@code Image} to be created as disabled
50     * @return  the new grayscale image created from {@code i}
51     */
52    public static Image createDisabledImage(Image i) {
53        if (i instanceof MultiResolutionImage) {
54            return MultiResolutionCachedImage
55                    .map((MultiResolutionImage) i,
56                         (img) -> createDisabledImageImpl(img));
57        }
58        return createDisabledImageImpl(i);
59    }
60
61    private static Image createDisabledImageImpl(Image i) {
62        GrayFilter filter = new GrayFilter(true, 50);
63        ImageProducer prod = new FilteredImageSource(i.getSource(), filter);
64        Image grayImage = Toolkit.getDefaultToolkit().createImage(prod);
65        return grayImage;
66    }
67
68    /**
69     * Constructs a GrayFilter object that filters a color image to a
70     * grayscale image. Used by buttons to create disabled ("grayed out")
71     * button images.
72     *
73     * @param b  a boolean -- true if the pixels should be brightened
74     * @param p  an int in the range 0..100 that determines the percentage
75     *           of gray, where 100 is the darkest gray, and 0 is the lightest
76     */
77    public GrayFilter(boolean b, int p) {
78        brighter = b;
79        percent = p;
80
81        // canFilterIndexColorModel indicates whether or not it is acceptable
82        // to apply the color filtering of the filterRGB method to the color
83        // table entries of an IndexColorModel object in lieu of pixel by pixel
84        // filtering.
85        canFilterIndexColorModel = true;
86    }
87
88    /**
89     * Overrides <code>RGBImageFilter.filterRGB</code>.
90     */
91    public int filterRGB(int x, int y, int rgb) {
92        // Use NTSC conversion formula.
93        int gray = (int)((0.30 * ((rgb >> 16) & 0xff) +
94                         0.59 * ((rgb >> 8) & 0xff) +
95                         0.11 * (rgb & 0xff)) / 3);
96
97        if (brighter) {
98            gray = (255 - ((255 - gray) * (100 - percent) / 100));
99        } else {
100            gray = (gray * (100 - percent) / 100);
101        }
102
103        if (gray < 0) gray = 0;
104        if (gray > 255) gray = 255;
105        return (rgb & 0xff000000) | (gray << 16) | (gray << 8) | (gray << 0);
106    }
107}
108