为什么三个 R、G 和 B 圆不能正确混合?

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

我正在尝试使用 python + pygame 来表示 RGB 颜色模型。

我想得到以下信息: 于是,我写了下面的代码:

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()

但我没有得到 RGB 颜色模型,而是得到了这个,颜色混合不正确: 有人知道它可能是什么吗?谢谢!

这应该有效:

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()

您想在圆的交点处添加颜色,因此使用 BLEND_RGB_ADD 标志。

因为你想要白色背景,所以我创建了第四个表面。