Question

I'm rewriting an old game of mine for Firemonkey, originally written in Delphi 2010. Those days it was easy making the backgrounds of the game pieces transparent using TImage.Transparent := true, but in Firemonkey I don't find it so easy. I have learned that designtime allows you to easily set the TransparentColor, and then the object background is just as transparent as you wanted it to, but how can one make this happen runtime? I'm creating a lot of dynamic TImage objects, functioning as pieces in a game, and obviously need them to have transparent backgrounds. Anyone who can help me out here? Thanks in advance.

Was it helpful?

Solution

FireMonkey images use a 32-bit per pixel schema with 8 bits each for red, green and blue plus an extra 8 bits for transparency, also called an alpha channel. If the value of the alpha channel is #FF the pixel will be opaque, #00 will be completely transparent and values in between will vary the transparency accordingly.

Therefore a colour in FireMonkey (actually a TAlphaColor) can be set with an 8 digit hex number where the bits map to #AARRGGBB where AA is the transparency/alpha channel.

Thus, #FF00FF00 is opaque green and #88FF0000 is semi-transparent red. Any color value in which the first two digits are #00 with be completely transparent.

FireMonkey includes are pre-defined constant claNull for a completely transparent colour.

You can also use the TAlphaColorRec record to access individual colours of a TAlphaColor variable using it's fields, A, R, G and B. E.g.

Red := TAlphaColorRec(MyColour).R;

TAlphaColorRec(MyColour).A := #00;

OTHER TIPS

Yoy can change de background color (simple color) of Image and select it in the MultiResBitmap editor BEFORE load the bipmap image (bitmap with white on background):

enter image description here

Or add real transparency to the image and convert it to PNG. If you load a PNG with thansparency the editor charge it perfectly.

enter image description here

Regards.

I give a code answer here Is there an FMX function to set a TImage's transparent color at runtime? that might solve this problem. It's in C++ with XE5, but I imagine conversion to Delphi should be fairly straightforward.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top