Set gamma with pygame 2.0.x

Viewed 313

I create a game and I would like to set the gamma of the screen. For example:

import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((600, 450))
font = pygame.font.SysFont('consolas', 25)

red   = 1
green = 1
blue  = 1

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()

    screen.fill((20, 20, 20))
    
    # draw colored zones
    for x in range(600):
        pygame.draw.line(screen, (x * 255 / 600, 0, 0), (x, 0), (x, 100))
    for x in range(600):
        pygame.draw.line(screen, (0, x * 255 / 600, 0), (x, 100), (x, 200))
    for x in range(600):
        pygame.draw.line(screen, (0, 0, x * 255 / 600), (x, 200), (x, 300))

    if pygame.mouse.get_pressed()[0]:
        x, y = pygame.mouse.get_pos()
        if y < 100:
            red   = x / 300
            pygame.display.set_gamma(red, green, blue)
        elif y < 200:
            green = x / 300
            pygame.display.set_gamma(red, green, blue)
        elif y < 300:
            blue  = x / 300
            pygame.display.set_gamma(red, green, blue)

    text = 'pygame.display.set_gamma(%.2f, %.2f, %.2f)' % (red, green, blue)
    text = font.render(text, 0, (255, 255, 255), True)
    screen.blit(text, (10, 440 - text.get_height())) # render the text

    pygame.display.flip()

But when I updated my pygame version to 2.0.0.dev6, the gamma correction didn't work, and I saw ugly colors, blue and yellow flashing irregularly.

I'm on Windows 10.

EDIT 1

After investigation, the green color seems not to be handled by the set_gamma function. When I change x value in the color (0, x * 255 / 600, 0), nothing happens.

EDIT 2

I now have the latest pygame version - now litteraly nothing happens.
According to the documentation,

Not all systems and hardware support gamma ramps

Ok! Maybe my computer doesn't handle this function! But:

if the function succeeds it will return True.

And I get True, even if nothing works as expected.
Does someone see clearly in this mess of bugs?

1 Answers

I help develop pygame.

Thank you for reporting this, I believe this is a bug in pygame.

I could replicate everything you said except for display.set_gamma() returning True even when it succeeded. For me it always returned False.

That difference aside, I found the source of the bug in the pygame source code, and submitted a patch to fix it. https://github.com/pygame/pygame/pull/2524

Assuming that this gets merged, the bug will be fixed in pygame 2.0.2.

But you want your code to work ASAP presumably, so you have a couple options.

  • Downgrade to pygame 1.9.6 for now, then upgrade to 2.0.2 once it comes out.
  • Build pygame from source on windows (sorta difficult)
  • If you happen to be on Python 3.8 64bit, I can send you a wheel file of pygame 2.0.2.dev1 to pip install from.

Edit: I just realized you posted this in August. Sorry this took so long. If you have an issue like this in the future, pygame's issue tracker on github is the place to go to report this stuff: https://github.com/pygame/pygame/issues

Related