# RGBA Colors – Transparency in Python

In Chapter 7, we used transparency in an image to allow the visualization to “see through” to an image in the background. As it happens, any pixel can be assigned a degree of transparency that permits the same visual character. A color can be assigned a value that dictates how opaque or transparent it is, allowing colors behind it to influence how that pixel is seen. One can think of this as a fourth color value, in addition to red, green, and blue. It is referred to as alpha, and a color with four color parameters is said to be in the RGBA color space, for Red, Green, 5lue, and Alpha.

If the value of Alpha is 255, then the color is opaque; as it decreases in value, the transparency increases until at Alpha=0, the pixel or object cannot be seen. A program that draws three overlapping circles using colors with an Alpha value of 60 shows the visual effect of using transparency (Figure 9.5a). Unfortunately, transparency cannot be specified simply by providing the Alpha value as a fourth value to the color tuple.

Transparency is partly a property of the surface on which the pixels are drawn. What we must do is create a new surface for the transparent item, set the transparency of that surface to the desired value, draw the item on that surface, and then blit it to the display surface. To draw three overlapping circles with dif­ferent, transparent colors, this must be done three times, once for each circle. A draw function that does this is as follows:

def draw():

s = pygame.Surface((300,300))

s.set alpha(50)

s.fill((255,255,255))

fill = (255, 0, 0)

pygame.draw.circle (s, fill, (100, 100), 75)

screen.blit(s, (0,0))

s.set alpha(50)

s.fill((255,255,255))

fill = (0, 255, 0)

pygame.draw.circle (s, fill, (200, 100), 75)

screen.blit(s, (0,0))

s.set alpha(50)

s.fill((255,255,255))

fill = (0, 0, 255)
pygame.draw.circle (s, fill, (150, 200), 75)
screen.blit(s, (0, 0))

Figure 9.5
(a) Overlapping circles filled with transparent versions of red, green, and blue create new colors in
the overlapping regions. (b) Stroke colors can have transparency, too. Where the red and blue lines
intersect the red under the blue, it is seen as purple.

Source: Parker James R. (2021), Python: An Introduction to Programming, Mercury Learning and Information; Second edition.