1 /*
2 Wotonomy: OpenStep design patterns for pure Java applications.
3 Copyright (C) 2001 Intersect Software Corporation
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, see http://www.gnu.org
17 */
18
19 package net.wotonomy.ui.swing.components;
20
21 import java.awt.Color;
22 import java.awt.image.RGBImageFilter;
23
24 /***
25 * TintedImageFilter tints all gray pixels half-way towards
26 * the value passed into the constructor. This "tints" a
27 * mostly grayscale image. This has proven useful for tinting
28 * user interface decorative images towards one of the SystemColor
29 * constants to better mesh with a platform look and feel.
30 *
31 * @author michael@mpowers.net
32 * @author $Author: cgruber $
33 * @version $Revision: 893 $
34 */
35 public class TintedImageFilter extends RGBImageFilter
36 {
37 double redOffset, greenOffset, blueOffset;
38
39 public TintedImageFilter( Color aColor )
40 {
41 canFilterIndexColorModel = true;
42 redOffset = getOffset( aColor.getRed() );
43 greenOffset = getOffset( aColor.getGreen() );
44 blueOffset = getOffset( aColor.getBlue() );
45 }
46
47 /***
48 * Calculates the offset used to modify color
49 * values. This method returns half the difference
50 * between the specified color level and 192.
51 */
52 protected double getOffset( int colorValue )
53 {
54 return ( colorValue - 192 ) / 2;
55 }
56
57 public int filterRGB(int x, int y, int rgb)
58 {
59
60 int red = ( rgb & 0xff0000 ) >> 16;
61 int green = ( rgb & 0x00ff00 ) >> 8;
62 int blue = ( rgb & 0x0000ff );
63
64 // if roughly black
65 if ( red + green + blue < 30 ) return rgb;
66
67 // if roughly gray
68 if ( ( Math.abs( red - green ) < 10 )
69 && ( Math.abs( red - blue ) < 10 ) )
70 {
71 red += redOffset;
72 if ( red < 0 ) red = 0;
73 if ( red > 255 ) red = 255;
74 green += greenOffset;
75 if ( green < 0 ) green = 0;
76 if ( green > 255 ) green = 255;
77 blue += blueOffset;
78 if ( blue < 0 ) blue = 0;
79 if ( blue > 255 ) blue = 255;
80
81 return new Color( red, green, blue ).getRGB();
82 }
83
84 return rgb;
85 }
86 }
87
88 /*
89 * $Log$
90 * Revision 1.1 2006/02/16 13:22:22 cgruber
91 * Check in all sources in eclipse-friendly maven-enabled packages.
92 *
93 * Revision 1.2 2001/01/18 21:27:04 mpowers
94 * Made the tinting a little darker.
95 *
96 * Revision 1.1 2001/01/12 17:36:27 mpowers
97 * Contributing TintedImageFilter.
98 *
99 *
100 */