Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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

like image 552
Michael Avatar asked Jun 26 '11 18:06

Michael


People also ask

How do you convert rgba to Argb?

(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 .

Can you convert rgba to hex?

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.

What does rgba 255 0 0 0.2 color code in CSS means?

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).

How does rgba extend the RGB Colour values?

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).


1 Answers

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()).

like image 119
Karel Petranek Avatar answered Oct 13 '22 05:10

Karel Petranek