gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:26:30 25 4
gpt4 key购买 nike

我正在尝试使用 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 标志。

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

关于python - 为什么三个 R、G 和 B 圆不能正确混合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71708994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com