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 Screen 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
(pixel << 24) | (pixel >> 8) rotates a 32-bit integer 8 bits to the right, which would convert a 32-bit RGBA value to ARGB. This works because: pixel << 24 discards the RGB portion of RGBA off the left side, resulting in A000 . pixel >> 8 discards the A portion of RGBA off the right side, resulting in 0RGB .
RGBA to Hex (#rrggbbaa) Converting RGBA to hex with the #rgba or #rrggbbaa notation follows virtually the same process as the opaque counterpart. Since the alpha ( a ) is normally a value between 0 and 1, we need to multiply it by 255, round the result, then convert it to hexadecimal.
RGB Value. Each parameter (red, green, and blue) defines the intensity of the color between 0 and 255. For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest value (255) and the others are set to 0. To display black, set all color parameters to 0, like this: rgb(0, 0, 0).
RGBA Colors RGBA color values are an extension of RGB color values with an alpha channel - which specifies the opacity for a color. An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).
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())
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With