4
\$\begingroup\$

Ive seen a lot of people make sprite sheets magenta instead of transparent and then they make the magenta transparent using code. What is best (Performance) to use transparent images or use a color (example : magenta) instead of transparency?

\$\endgroup\$
  • \$\begingroup\$ I could give a more helpful code sample with my answer if you give more details- what kind of graphics are you going for? How many different levels of transparency do you need? \$\endgroup\$ – Michael Hoffmann Jul 12 '15 at 5:57
2
\$\begingroup\$

It depends entirely on your needs: using transparency keys, you usually only have fully-transparent or fully-opaque colors*; using an alpha component, you can have 255 different levels of opacity. Do you want transparent smoke and flames for your explosion sprites, or just Super-Mario-Fireball style ones, with all-or-nothing opacity? It depends on your application's needs.

*Unless you use multiple transparency keys to represent different levels of opaqueness. For example, your program could treat magenta pixels as fully transparent; pixels with odd-numbered R-components as half transparent; and pixels with prime-number G components as 3/4 transparent. Whether that's a more memory-efficient option requires a lot of case-specific math to determine, but it's pretty much always more complicated than just using an alpha component in the source content, and it can really limit the colors available to you if you use too many color keys.

(Please comment if you'd like a code sample and/or images- I'm on mobile right now.)

\$\endgroup\$
3
\$\begingroup\$

It is quite the same thing. When rendering occurs, the program can compute opaque and transparent pixels to be drawn for color interpolation on the screen. The only difference on the two techniques you mentioned is that trasparent images carry transparency information on their own, and color key gives transparency information to an image from within the program, but when the engine draws on the screen it still checks for what to draw and what not to draw, every frame.

\$\endgroup\$
  • 1
    \$\begingroup\$ There are some important differences. Alpha transparency requires a separate colour channel, and so it can increase the size of the texture on disc & in memory by about 33%. But it's also natively supported by GPU texture compression formats, so if your textures are amenable to compression this can often be just as efficient or even moreso than storing an image with palette transparency. \$\endgroup\$ – DMGregory Jul 11 '15 at 13:36
  • 1
    \$\begingroup\$ @DMGregory: More generally, in some systems the fastest graphic formats to work with may use color-keyed transparency, while on others alpha-keyed transparency might be better. Historically, some systems had hardware support only for color keying, and some embedded systems (typically those with 16-color or 256-color hardware) still do, but most systems today support alpha channels. \$\endgroup\$ – supercat Jul 11 '15 at 16:50
  • \$\begingroup\$ @supercat, can you give some examples of where color-keying has hardware support? I read a few good articles on that years ago, and some references would be a great refresher. \$\endgroup\$ – Michael Hoffmann Jul 12 '15 at 5:59
  • 1
    \$\begingroup\$ @MichaelHoffmann: Some TFT-LCD controllers of the style used in cell phones have a color-keyed mode; I can't think of any I've seen with an alpha-channel mode; when clocking out 16 bits per pixel, they always use the extra bit for green. Display hardware with fewer than 16 bits per pixel almost always uses a reserved color for transparency if it supports it at all. I find it somewhat curious, actually, that transparency support isn't more common in TFT-LCD controllers, since nearly all of them allow an arbitrary rectangular area to be written in any combination of row-major, column-major,... \$\endgroup\$ – supercat Jul 12 '15 at 18:59
  • \$\begingroup\$ ...row increasing/decreasing, and column increasing/decreasing, without having to reload coordinates during the process (very useful for DMA) but on devices without transparency support the value of that ability is limited unless the application only has need for opaque rectangles. \$\endgroup\$ – supercat Jul 12 '15 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.