import pygame
import sys

pygame.init()

WIDTH, HEIGHT = 600, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pygame 颜色识别")

# 画彩色背景
def draw_color_area():
    for x in range(WIDTH):
        color = (
            int(x / WIDTH * 255),
            100,
            255 - int(x / WIDTH * 255)
        )
        pygame.draw.line(screen, color, (x, 0), (x, HEIGHT))

selected_color = None
clock = pygame.time.Clock()
running = True

while running:
    screen.fill((30, 30, 30))
    draw_color_area()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = pygame.mouse.get_pos()
            selected_color = screen.get_at((x, y))[:3]
            print(f"点击位置 ({x}, {y}) 的颜色是: {selected_color}")

    # 只画颜色块，不显示文字
    if selected_color:
        pygame.draw.rect(screen, selected_color, (10, 10, 80, 40))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()