Skip to content
Advertisement

Why don’t three R, G and B circles blend correctly?

I am trying to represent the RGB color model using python + pygame.

I wanted to get the following: So then, I wrote the following code:

import pygame, sys
from pygame.locals import *

ALPHA = 100

TRANSPARENT = (255,0,255)
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0,100)
GREEN = (0,255,0,100)
BLUE = (0,0,255,100)

pygame.init()

size=(800,500)
screen= pygame.display.set_mode(size)

surf1 = pygame.Surface(size)
surf1.fill(TRANSPARENT)
surf1.set_colorkey(TRANSPARENT)
pygame.draw.circle(surf1, BLUE, (430, 280), 60 )

surf2 = pygame.Surface(size)
surf2.fill(TRANSPARENT)
surf2.set_colorkey(TRANSPARENT)
pygame.draw.circle(surf2, GREEN, (370, 280), 60 )

surf3 = pygame.Surface(size)
surf3.fill(TRANSPARENT)
surf3.set_colorkey(TRANSPARENT)
pygame.draw.circle(surf3, RED, (400, 220), 60 )

surf1.set_alpha(ALPHA)
surf2.set_alpha(ALPHA)
surf3.set_alpha(ALPHA)

while True :
    screen.fill(WHITE)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.blit(surf1, (0,0))
    screen.blit(surf2, (0,0))
    screen.blit(surf3, (0,0))
    pygame.display.flip()

But instead of getting the RGB color model, I got this, with colors incorrectly blended: Does anybody know what it could be? Thanks!

Advertisement

Answer

This should work:

import pygame, sys
from pygame.locals import *


BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0, 100)
GREEN = (0, 255, 0, 100)
BLUE = (0, 0, 255, 100)

pygame.init()

size = (800, 500)
screen = pygame.display.set_mode(size)

surf1 = pygame.Surface(size)
surf1.fill(BLACK)
surf1.set_colorkey(BLACK)
pygame.draw.circle(surf1, BLUE, (430, 280), 60)

surf2 = pygame.Surface(size)
surf2.fill(BLACK)

surf2.set_colorkey(BLACK)
pygame.draw.circle(surf2, GREEN, (370, 280), 60)

surf3 = pygame.Surface(size)
surf3.fill(BLACK)

surf3.set_colorkey(BLACK)
pygame.draw.circle(surf3, RED, (400, 220), 60)

surf4 = pygame.Surface(size)
surf4.set_colorkey(BLACK)
surf4.blit(surf1, (0, 0), special_flags=pygame.BLEND_RGB_ADD)
surf4.blit(surf2, (0, 0), special_flags=pygame.BLEND_RGB_ADD)
surf4.blit(surf3, (0, 0), special_flags=pygame.BLEND_RGB_ADD)


while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.fill(WHITE)
    screen.blit(surf4, (0, 0))

    pygame.display.flip()

You want to add the colors at the intersections of the circles, hence use the BLEND_RGB_ADD flag.

I created a fourth surface since you wanted a white background.

Advertisement