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 */
25package javax.swing;
26
27import java.awt.*;
28import java.awt.image.*;
29import sun.awt.image.MultiResolutionCachedImage;
30
31/**
32 * An image filter that "disables" an image by turning
33 * it into a grayscale image, and brightening the pixels
34 * in the image. Used by buttons to create an image for
35 * a disabled button.
36 *
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