Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blending two pygame.Color objects together

Tags:

python

pygame

I am wondering how to blend two pygame.Color objects and cannot find a solution after extensive reasearch.
I have created two pygame.Color objects and would like to create a new object who's colour value is the two colours blended together.

import pygame

white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)

#If I want to blend white and red with the option of how much dominace each should have what code would I write
like image 380
EmeraldThunder- Avatar asked Jun 20 '26 21:06

EmeraldThunder-


1 Answers

Use the pygame.Color.lerp function to linear interpolation to the given Colors.

white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)

pink = white.lerp(red, 0.5)

Minimal example:

import pygame

pygame.init()
window = pygame.display.set_mode((500, 100))
clock = pygame.time.Clock()

white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
pink25 = white.lerp(red, 0.25)
pink50 = white.lerp(red, 0.5)
pink75 = white.lerp(red, 0.75)

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False          

    window.fill(0)
    pygame.draw.circle(window, white, (50, 50), 45)
    pygame.draw.circle(window, pink25, (150, 50), 45)
    pygame.draw.circle(window, pink50, (250, 50), 45)
    pygame.draw.circle(window, pink75, (350, 50), 45)
    pygame.draw.circle(window, red, (450, 50), 45)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()
like image 192
Rabbid76 Avatar answered Jun 23 '26 10:06

Rabbid76