Transform RGBA with underlying color into RGB?
i've got a problem with transforming Colors in Java. The simplified problem look like the following:
My application contains an image. I've layed an Recangle over this image. The color of the Rectangle is defined as new Color(255, 255, 0, 80).
Is it possible to calculate / transform the Color which is shown on the Scre开发者_如何学Cen into a Color without Alpha-Value without the getPixelColor()-Method? Different formulated: Can I calculate a Color without alpha-value from a Color with alpha-value + the underlying color?
I hope someone can help me.
Regards, Michael
Just as the Wikipedia article states (assuming opaque background):
int r, g, b;
r = fgColor.getRed() * fgColor.getAlpha() + bgColor.getRed() * (255 - fgColor.getAlpha());
g = fgColor.getGreen() * fgColor.getAlpha() + bgColor.getGreen() * (255 - fgColor.getAlpha());
b = fgColor.getBlue() * fgColor.getAlpha() + bgColor.getBlue() * (255 - fgColor.getAlpha());
Color result = new Color(r / 255, g / 255, b / 255);
Disclaimer: haven't tested this but it should work.
If the foreground color is constant (such as a filled transparent rectangle), you can optimize a lot by precomputing fgColor.getComponent() * fgColor.getAlpha()
and (255 - fgColor.getAlpha())
.
精彩评论