| 1 |
/* |
| 2 |
* @(#)TintFilter.java 8/17/2011 |
| 3 |
* |
| 4 |
* Copyright 2002 - 2011 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
package com.jidesoft.icons; |
| 8 |
|
| 9 |
import java.awt.*; |
| 10 |
import java.awt.image.FilteredImageSource; |
| 11 |
import java.awt.image.ImageProducer; |
| 12 |
import java.awt.image.RGBImageFilter; |
| 13 |
|
| 14 |
/** |
| 15 |
* An image filter that tints the image with a color. |
| 16 |
*/ |
| 17 |
public class TintFilter extends RGBImageFilter { |
| 18 |
private int _alpha, _red, _green, _blue; |
| 19 |
private int _width, _height; |
| 20 |
private Insets _insets; |
| 21 |
|
| 22 |
public TintFilter(Color color, int width, int height, Insets insets) { |
| 23 |
_alpha = color.getAlpha(); |
| 24 |
_red = color.getRed(); |
| 25 |
_green = color.getGreen(); |
| 26 |
_blue = color.getBlue(); |
| 27 |
_width = width; |
| 28 |
_height = height; |
| 29 |
_insets = insets; |
| 30 |
canFilterIndexColorModel = true; |
| 31 |
} |
| 32 |
|
| 33 |
public int filterRGB(int x, int y, int rgb) { |
| 34 |
if (_insets != null && (x < _insets.left || x > _width - _insets.right - 1 || y < _insets.top || y > _height - _insets.bottom - 1)) { |
| 35 |
return rgb; |
| 36 |
} |
| 37 |
else { |
| 38 |
Color color = new Color(rgb, true); |
| 39 |
int r = Math.min(Math.max(color.getRed() + (color.getRed() * _red) / 100, 0), 255); |
| 40 |
int g = Math.min(Math.max(color.getGreen() + (color.getGreen() * _green) / 100, 0), 255); |
| 41 |
int b = Math.min(Math.max(color.getBlue() + (color.getBlue() * _blue) / 100, 0), 255); |
| 42 |
int a = Math.min(Math.max(color.getAlpha() + (color.getAlpha() * _alpha) / 100, 0), 255); |
| 43 |
return a << 24 | r << 16 | g << 8 | b; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Creates a tinted image |
| 49 |
* |
| 50 |
* @param i the original image |
| 51 |
* @param color the color to be tinted |
| 52 |
* @param insets the insets. The border area with the insets will not be tinted. |
| 53 |
* @return a tinted image |
| 54 |
*/ |
| 55 |
public static Image createTintedImage(Image i, Color color, Insets insets) { |
| 56 |
TintFilter filter = new TintFilter(color, i.getWidth(null), i.getHeight(null), insets); |
| 57 |
ImageProducer prod = new FilteredImageSource(i.getSource(), filter); |
| 58 |
return Toolkit.getDefaultToolkit().createImage(prod); |
| 59 |
} |
| 60 |
} |