PyGame颜色

一般的32位RGB,每个像素可以显示16.7百万种颜色。

第一个例子,可以生成所有的颜色。

# -*- coding: utf-8 -*- 
# Time : 2019/1/31 12:53 
# Author : ***
import pygame

if __name__ == "__main__":
    pygame.init()

    screen = pygame.display.set_mode((640, 480))

    all_colors = pygame.Surface((4096, 4096), depth=24)

    for r in range(256):
        print(r + 1, "out of 256")
        x = (r & 15) * 256
        y = (r >> 4) * 256
        for g in range(256):
            for b in range(256):
                all_colors.set_at((x + g, y + b), (r, g, b))

    pygame.image.save(all_colors, "./colors/allcolors-2.bmp")

第二个例子,用鼠标移动三个点,代表三原色的值,下方是三原色混合得到的结果。

# -*- coding: utf-8 -*- 
# Time : 2019/1/31 15:56 
# Author : ***
import pygame
from pygame.locals import *
from sys import exit

if __name__ == "__main__":
    pygame.init()
    screen = pygame.display.set_mode((640, 480), 0, 32)

    def create_scales(height):
        red_scale_surface = pygame.surface.Surface((640, height))
        green_scale_surface = pygame.surface.Surface((640, height))
        blue_scale_surface = pygame.surface.Surface((640, height))
        for x in range(640):
            c = int((x / 640.) * 255.)
            red = (c, 0, 0)
            green = (0, c, 0)
            blue = (0, 0, c)
            line_rect = Rect(x, 0, 1, height)
            pygame.draw.rect(red_scale_surface, red, line_rect)
            pygame.draw.rect(green_scale_surface, green, line_rect)
            pygame.draw.rect(blue_scale_surface, blue, line_rect)
        return red_scale_surface, green_scale_surface, blue_scale_surface


    red_scale, green_scale, blue_scale = create_scales(80)

    color = [127, 127, 127]

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

        screen.fill((0, 0, 0))

        screen.blit(red_scale, (0, 00))
        screen.blit(green_scale, (0, 80))
        screen.blit(blue_scale, (0, 160))

        x, y = pygame.mouse.get_pos()

        if pygame.mouse.get_pressed()[0]:
            for component in range(3):
                if y > component * 80 and y < (component + 1) * 80:
                    color[component] = int((x / 639.) * 255.)
            pygame.display.set_caption("PyGame Color Test - " + str(tuple(color)))

        for component in range(3):
            pos = (int((color[component] / 255.) * 639), component * 80 + 40)
            pygame.draw.circle(screen, (255, 255, 255), pos, 20)

        pygame.draw.rect(screen, tuple(color), (0, 240, 640, 240))

        pygame.display.update()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值