[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Effecient way to 'darken' a surface?



On Tue, 2005-12-20 at 22:45 +0100, Troels Therkelsen wrote:
> arr = surfarray.array3d(surf)
> arr = maximum(zeros(arr.shape), arr - 200))
> surfarray.blit_array(surf, arr)
> 
> I dunno, perhaps there just isn't a significantly faster way of doing 
> this? Please prove me wrong!

This could be sped up a few ways. If your image uses an 8bit palette you
can quickly modify the palette colors in realtime and blit the new
background.

You can optimize the Numeric mojo a bit by not worrying about the
clamping.

def multiply(surface, value):
    "Value is 0 to 255. So 128 would be 50% darken"
    arr = pygame.surfarray.array3d(surface) * value
    arr >>= 8
    pygame.surfarray.blit_array(surface, arr)



I have gotten away with also creating all black images, setting the
alpha level and blitting. I can't guess if this would be slower of
faster than the Numeric version. If you are going to redo this many
times with the same sized images, you could reuse the "dark" Surface for
more speed.


def darken(surface, value):
    "Value is 0 to 255. So 128 would be 50% darken"
    dark = pygame.Surface(surface.get_size(), 32)
    dark.set_alpha(value, pygame.RLEACCEL)
    surface.blit(dark, (0, 0))