Is there any way of doing this to an entire graphics context instead of a specific image?
I’m looking for something like a customized Composite.
I have this snippet:
public class TintingComposite implements Composite
{
int m_r;
int m_g;
int m_b;
double m_newColorPercentage; // 0-1
double m_oldPercentage;//0-1
public TintingComposite(Color a_color, double a_dPercentage)
{
m_newColorPercentage = a_dPercentage;
m_oldPercentage = 1 - m_newColorPercentage;
m_r = (int) (a_color.getRed() * m_newColorPercentage);
m_g = (int) (a_color.getGreen() * m_newColorPercentage);
m_b = (int) (a_color.getBlue() * m_newColorPercentage);
}
public CompositeContext createContext(ColorModel srcColorModel,
ColorModel dstColorModel,
RenderingHints hints)
{
return new CompositeContext()
{
public void compose(Raster src, Raster dstIn, WritableRaster dstOut)
{
for (int x = 0; x < dstOut.getWidth(); x++)
{
for (int y = 0; y < dstOut.getHeight(); y++)
{
// Get the source pixels
int[] srcPixels = new int[4];
src.getPixel(x, y, srcPixels);
//blend in the new r,g,b but keep the alpha
srcPixels[0] = (int) (srcPixels[0] *
m_oldPercentage + m_r) & 0xff;
srcPixels[1] = (int) (srcPixels[1] *
m_oldPercentage + m_g) & 0xff;
srcPixels[2] = (int) (srcPixels[2] *
m_oldPercentage + m_b) & 0xff;
dstOut.setPixel(x, y, srcPixels);
}
}
}
public void dispose()
{
}
};
}
}
The only problem is that I whenever I try to set a pixel which has an alpha different from 1, I end up making it opaque.
I suspect my problem is that when I call dstOut.getNumBands() it returns 3 while src.getNumBands() returns 4.
Any idea? Alternatives?
Thanks.