Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong colors antialiasing with Pygame on OS X

I'm using Pygame on a Macbook Pro (non-retina) running OS X. When I try to create an antialiased line or circle, it seems to be coming out as the wrong color. Here's an example:

import sys, pygame, random, math
import pygame.gfxdraw

black = (0,0,0)
white = (255, 255, 255)
green = (0, 255, 0)


pygame.init()

size = width, height = 800, 600

screen = pygame.display.set_mode(size)

# make the background
background = pygame.Surface(screen.get_size())
#background.fill(black)
background.fill(white)

while 1:
    # handle single events
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE: sys.exit()

    screen.blit(background, (0, 0))

    pygame.draw.line(screen, green, [0, 0], [50,30], 1)
    pygame.draw.aaline(screen, green, [0, 50],[50, 80], True)

    pygame.draw.circle(screen, black, [100, 100], 10)
    pygame.gfxdraw.aacircle(screen, 100, 130, 10, black)

    pygame.display.flip()

Which leads to:

color example

like image 322
user2171504 Avatar asked Oct 01 '22 13:10

user2171504


1 Answers

Haven't been able to find the cause for this despite searching through the pygame source code.

I did manage to come up with a workaround though. Two actually.

There seems to be a bug in the pygame.gfxdraw module that causes colors to be messed up in OS X when the value for blue is 0. If you change your black to (0,0,1), the circle will already look much better, but still have a slight blueish tinge. Change it to (0,0,2) and it will appear pretty much completely black. Makes little sense to me but it works.

Edit: That's for background colors that don't contain any blue. It appears that on background RGBs containing >0 blue, the circle needs at least that amount of blue to prevent the bug. For example, to get a black aacircle on a (50,50,50) background, the closest I was able to get was by using (0,0,50) for the circle. Any lower amount of blue will result in bright blue artifacts.

Adding blue doesn't help for fixing pygame.draw.aaline's behavior though. This must be a separate bug. However, pygame.gfxdraw.line is also anti-aliased. Use that method instead, change green to (0,255,1) and the cyan line will now be green.

enter image description here

like image 158
Junuxx Avatar answered Oct 04 '22 19:10

Junuxx