[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] RGBA question
ah, this looks like a good time to better explain SDL's "rules" for
dealing with different transparencies. there are three types of
transparency in SDL/pygame.
* First is 'colorkey'. This is by far the fastest type of transparency.
It's pretty standard, one color of the image is dubbed 'transparent' and
any pixel of the same color is not drawn when blitted. It is handled
with the Surface.set_colorkey() function. Many image formats can store
this colorkey.
* The second type is 'surface alpha'. This sets a constant alpha level
for the entire image when blitted. It can be mixed with colorkey
transparency to good effect. It is handled with the Surface.set_alpha()
function.
* Lastly there is the 'per pixel alpha'. In this mode each pixel of the
surface has its own transparency level. When a surface has pixel alpha
none of the other transparencies are used. So no colorkey or surface
alpha gets used. You need to create Surfaces with pixel alpha for them
to have it. You can use the Surface function with the SRCALPHA flag, or
call Surface.convert_alpha() to create a new copy of a surface with
pixel alpha. Many image formats can store this pixel alpha data.
now with this gainful insight, let's review the two code samples.
Andrew Barilla wrote:
> This works...
> artist_bg = pygame.Surface((994, 644))
> artist_bg.fill((206, 158, 90))
> artist_bg.set_alpha(125)
>
> but this doesn't...
> artist_bg = pygame.Surface((994, 644))
> artist_bg.fill((206, 158, 90, 125))
so in the first example we are using the 'surface alpha'. everything
good there. the second example we are trying to use the 'pixel alpha',
but the Surface created does not have per pixel alpha. The trick here
will be creating a different surface. change the first line to this,
artist_bg = pygame.Surface((994,644), pygame.SRCALPHA, 32)
this will create a new Surface with pixel alpha and 32 bits per pixel.
now your fill call will be able to set the proper alpha values and you
should be all set.
btw, a few of SDL's blitters have "optimized" cases where surface alpha
== 128. Surface.set_alpha(128) will usually be a bit quicker than
Surface.set_alpha(125). In any event, the alpha blitting is slower than
colorkeys. If you don't need the blending and speed is a concern, i
recommend people go for the colorkeys.
--
"Neither will I tell you by what authority I do these things"
pete*shinners.org