Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame: Fill transparent areas of text with a color

I have several fonts that I would like to use that are basically outlines of letters, but the insides are transparent. How would I go about filling only the inside areas of these fonts with with a color? I suspect it would be using the special blitting RGBA_BLEND modes, but I am not familiar with their functionality.

Here is an example of the font I am using: https://www.dafont.com/fipps.font?back=bitmap

Right now, I am simply rendering the font onto a surface, and I've written a helper function for that. Ideally I would be able to integrate this into my function.

def renderText(surface, text, font, color, position):
    x, y = position[0], position[1]
    width, height = font.size(text)
    position = x-width//2, y-height//2
    render = font.render(text, 1, color)
    surface.blit(render, position)

Thank you so much for any help you can give me!

like image 463
Julien Sat-Vollhardt Avatar asked May 02 '18 17:05

Julien Sat-Vollhardt


Video Answer


1 Answers

An option is to define a surface the size of the text, fill that with the color you want, and blit the text on that. For example you could do this:

text = font.render('Hello World!', True, (255, 255, 255)
temp_surface = pygame.Surface(text.get_size())
temp_surface.fill((192, 192, 192))
temp_surface.blit(text, (0, 0))
screen.blit(temp_surface, (0, 0))

This will create a temporary surface that should fill in the transparent pixels of a text surface. There is another option of using set_at() but its too expensive in processing power for what you are doing and is best used for pre-processing surfaces.

Im certain that a better option using BLEND_RGBA_MULT will come from a more experienced user. Im not too good at blending modes either.

like image 145
Mercury Platinum Avatar answered Nov 02 '22 23:11

Mercury Platinum