001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Anton Avtamonov
020: * @version $Revision$
021: */package javax.swing;
022:
023: import java.awt.Color;
024: import java.awt.Image;
025: import java.awt.Toolkit;
026: import java.awt.image.FilteredImageSource;
027: import java.awt.image.RGBImageFilter;
028:
029: import org.apache.harmony.x.swing.internal.nls.Messages;
030:
031: public class GrayFilter extends RGBImageFilter {
032: private static final float INTERVAL_BOUND = 1f / 3;
033:
034: public static Image createDisabledImage(Image i) {
035: FilteredImageSource fis = new FilteredImageSource(
036: i.getSource(), new GrayFilter(true, 50));
037: return Toolkit.getDefaultToolkit().createImage(fis);
038: }
039:
040: private final ColorTransformationStrategy strategy;
041: private final float interval;
042:
043: public GrayFilter(boolean bright, int grayPercentage) {
044: if (grayPercentage < 0 || grayPercentage > 100) {
045: throw new IllegalArgumentException(Messages
046: .getString("swing.09")); //$NON-NLS-1$
047: }
048:
049: canFilterIndexColorModel = true;
050:
051: if (bright) {
052: strategy = new BrightingColorTransformationStrategy(
053: grayPercentage / 100f);
054: } else {
055: strategy = new NormalColorTransformationStrategy();
056: }
057:
058: this .interval = INTERVAL_BOUND * (100 - grayPercentage) / 100f;
059: }
060:
061: @Override
062: public int filterRGB(int x, int y, int rgb) {
063: int alpha = (rgb >> 24) & 0xFF;
064: int r = (rgb >> 16) & 0xFF;
065: int g = (rgb >> 8) & 0xFF;
066: int b = rgb & 0xFF;
067:
068: // The formula is the ordinary transformation from colored to grayscale.
069: // Taken from http://www.netnam.vn/unescocourse/computervision/comp_frm.htm (Color Representation article).
070: double intensity = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
071: int filteredRGB = strategy.getRGB((float) intensity);
072: alpha = alpha << 24;
073: filteredRGB = (filteredRGB & 0x00FFFFFF) | alpha;
074:
075: return filteredRGB;
076: }
077:
078: private interface ColorTransformationStrategy {
079: int getRGB(float brightness);
080: }
081:
082: private class BrightingColorTransformationStrategy implements
083: ColorTransformationStrategy {
084: private final float lowBound;
085:
086: public BrightingColorTransformationStrategy(float lowBound) {
087: this .lowBound = lowBound;
088: }
089:
090: public int getRGB(float brightness) {
091: float normalizedBrightness = lowBound + brightness
092: * interval;
093: return Color.HSBtoRGB(0, 0, normalizedBrightness);
094: }
095: }
096:
097: private class NormalColorTransformationStrategy extends
098: BrightingColorTransformationStrategy {
099: public NormalColorTransformationStrategy() {
100: super (0);
101: }
102: }
103: }
|