如何将 pygame 表面中的某些颜色列入白名单

How to whitelist certain colours in a pygame surface

有没有办法在 pygame 中将表面上的颜色列入白名单,并且任何不在该白名单中的颜色都会变得透明?

我试过使用 icon.set_colorkey() 但那只会去除一种颜色

您必须手动执行 pygame.Surface.convert_alpha()确保图像格式具有 alpha 通道。识别所有具有高于特定阈值(例如 230)的红绿蓝颜色通道的像素。将这些像素的颜色通道和 Alpha 通道更改为 (0, 0, 0, 0):

img = pygame.image.load(IMAGE).convert_alpha()

threshold = 230
for x in range(img.get_width()):
    for y in range(img.get_height()):
        color = img.get_at((x, y))
        if color.r > threshold and color.g > threshold and color.b > threshold:
            img.set_at((x, y), (0, 0, 0, 0))