Okay.
I have been seriously struggling with this lately. I've searched and searched forever. The closest I found was this thread here: http://72.5.124.102/thread.jspa?messageID=1662301
What I am trying to acheive:
I want to give my interface grayscale images for icons(png), and have a filter class that can color the grayscale images to a certain color, without changing the black/gray/white values. The effect I am looking for is exactly like the Color blending mode on Photoshop, Gimp has it too.
Even though the color is changing, the actual image should not. Similar to a grayscale effect, but instead of gray it uses a color.
So the image shouldn't be getting darker, lighter, distorted, etc. Just a colored tint. The effect I'm talking about is far different than just putting a semi-transparent panel over it with the color I want.
I have found this exact type of effect in a Java image editor from JHLabs, and I've attempted to demystify the code of the application but I cannot seem to find out how they are doing it.
Here is my current code, taken from the URL outlined at the top of this post. I've also got a demo interface, that paints "TheIcon.png", waits one second, filters the image and repaints the same image.
The problem is, after it filters all I get is the black interface. I don't know if the image is there, or if it's just completely transparent. But it's not working D=
I would like the chance to understand what this code is doing, but I can't find any articles or anything like that-- at least none that I can understand. I've been reluctant to read them because the first few I went through were doing something completely different. Anyways here's the code. =)
Interface.java
import java.awt.*;
import java.net.URL;
import javax.swing.*;
public class Interface extends JFrame {
private static final long serialVersionUID = 1L;
private static JPanel contentPane;
private static Image displayedImage;
public Interface() {
super();
setImage();
initializeComponent(); this.setVisible(true);
try { Thread.sleep(1000); } catch(Exception e) { }
filterImage();
}
private void initializeComponent() {
contentPane = new JPanel() {
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g) {
Dimension d = getSize();
g.setColor(new Color(0,0,0));
g.fillRect(0, 0, d.width, d.height);
g.drawImage(displayedImage, 0, 0, d.width, d.height, null);
}
};
this.setContentPane(contentPane);
this.setTitle("Colorize Test");
this.setSize(new Dimension(340, 250));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setResizable(false);
}
public static void filterImage() {
displayedImage = ColorFilter.filterImage(displayedImage);
contentPane.repaint();
}
private void setImage() {
// Set image
URL imagePath = getClass().getResource("TheIcon.png");
ImageIcon tmp = null;
try {
tmp = new ImageIcon(imagePath);
} catch(Exception e) {
System.out.println("Could not find the image in the path given");
System.exit(1);
}
displayedImage = tmp.getImage();
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) { }
new Interface();
}
}
ColorFilter.java
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.RGBImageFilter;
public class ColorFilter extends RGBImageFilter {
private float hue;
public static Image filterImage(Image image) {
ImageFilter filter = new ColorFilter(Color.red);
FilteredImageSource filteredSrc = new FilteredImageSource(image.getSource(), filter);
image = Toolkit.getDefaultToolkit().createImage(filteredSrc);
return image;
}
public ColorFilter(Color c) {
float[] hsb = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
hue = hsb[0];
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb) {
//detect the transparency of the pixel
int alpha = rgb & 0xff000000;
//if the pixel is fully transparent make nothing
if (alpha == 0) { return rgb; }
float[] hsb = Color.RGBtoHSB((rgb >> 16) & 0xff, (rgb >> 8) & 0xff, rgb & 0xff, null);
//change the pixel to the new color
hsb[0] = hue;
//add the transparency of the pixel and return it
return (Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]) & 0xffffff) | alpha;
}
}