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:
JavaScript
x
48
48
1
import pygame, sys
2
from pygame.locals import *
3
4
ALPHA = 100
5
6
TRANSPARENT = (255,0,255)
7
BLACK = (0,0,0)
8
WHITE = (255,255,255)
9
RED = (255,0,0,100)
10
GREEN = (0,255,0,100)
11
BLUE = (0,0,255,100)
12
13
pygame.init()
14
15
size=(800,500)
16
screen= pygame.display.set_mode(size)
17
18
surf1 = pygame.Surface(size)
19
surf1.fill(TRANSPARENT)
20
surf1.set_colorkey(TRANSPARENT)
21
pygame.draw.circle(surf1, BLUE, (430, 280), 60 )
22
23
surf2 = pygame.Surface(size)
24
surf2.fill(TRANSPARENT)
25
surf2.set_colorkey(TRANSPARENT)
26
pygame.draw.circle(surf2, GREEN, (370, 280), 60 )
27
28
surf3 = pygame.Surface(size)
29
surf3.fill(TRANSPARENT)
30
surf3.set_colorkey(TRANSPARENT)
31
pygame.draw.circle(surf3, RED, (400, 220), 60 )
32
33
surf1.set_alpha(ALPHA)
34
surf2.set_alpha(ALPHA)
35
surf3.set_alpha(ALPHA)
36
37
while True :
38
screen.fill(WHITE)
39
40
for event in pygame.event.get():
41
if event.type == pygame.QUIT:
42
sys.exit()
43
44
screen.blit(surf1, (0,0))
45
screen.blit(surf2, (0,0))
46
screen.blit(surf3, (0,0))
47
pygame.display.flip()
48
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:
JavaScript
1
51
51
1
import pygame, sys
2
from pygame.locals import *
3
4
5
BLACK = (0, 0, 0)
6
WHITE = (255, 255, 255)
7
RED = (255, 0, 0, 100)
8
GREEN = (0, 255, 0, 100)
9
BLUE = (0, 0, 255, 100)
10
11
pygame.init()
12
13
size = (800, 500)
14
screen = pygame.display.set_mode(size)
15
16
surf1 = pygame.Surface(size)
17
surf1.fill(BLACK)
18
surf1.set_colorkey(BLACK)
19
pygame.draw.circle(surf1, BLUE, (430, 280), 60)
20
21
surf2 = pygame.Surface(size)
22
surf2.fill(BLACK)
23
24
surf2.set_colorkey(BLACK)
25
pygame.draw.circle(surf2, GREEN, (370, 280), 60)
26
27
surf3 = pygame.Surface(size)
28
surf3.fill(BLACK)
29
30
surf3.set_colorkey(BLACK)
31
pygame.draw.circle(surf3, RED, (400, 220), 60)
32
33
surf4 = pygame.Surface(size)
34
surf4.set_colorkey(BLACK)
35
surf4.blit(surf1, (0, 0), special_flags=pygame.BLEND_RGB_ADD)
36
surf4.blit(surf2, (0, 0), special_flags=pygame.BLEND_RGB_ADD)
37
surf4.blit(surf3, (0, 0), special_flags=pygame.BLEND_RGB_ADD)
38
39
40
while True:
41
42
for event in pygame.event.get():
43
if event.type == pygame.QUIT:
44
sys.exit()
45
46
screen.fill(WHITE)
47
screen.blit(surf4, (0, 0))
48
49
pygame.display.flip()
50
51
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.