3

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!

  • 1
    `TRANSPARENT = (255,0,255)`. Perhaps if you set this color to `(0, 0, 0)` or `(255, 255, 255)` it may work. This is the color that is considered transparent, so full black or full white may be sufficient. – Niloct Apr 01 '22 at 15:05
  • You need `special_flags=BLEND_ADD` on the blits to mix colors additively rather than interpolating them. You probably don't want to set alpha. – Paul Hankin Apr 01 '22 at 15:05
  • 1
    ADDITIVE vs. SUBTRACTIVE color blending. If you mix Red and Green light its different from mixing Red and Green PAINT. – Patrick Artner Apr 01 '22 at 15:06
  • @PaulHankin I removed the set alpha and added special_flags=pygame.BLEND_ADD but now it's all white :C – FranGamer1892 Apr 01 '22 at 15:15
  • You'll have to change the background colour to black – Paul Hankin Apr 01 '22 at 15:45
  • I think you should clear your screen every frame, to prevent the colors from getting darker and darker, but I don't know whether thisis the cause of the problem. – The_spider Apr 01 '22 at 18:45

1 Answers1

3

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.

xxSirFartAlotxx
  • 394
  • 1
  • 11