문제

Hov can I make a string triplet to a RGB color? Like this:

color = "255,255,255"
background.fill((color))

This code gives me "invalid color argument". Why I want to do it this way? This string is saved in a file.

도움이 되었습니까?

해결책

pygame.Surface.fill expects:

The color argument can be either a RGB sequence, a RGBA sequence or a mapped color index. If using RGBA, the Alpha (A part of RGBA) is ignored unless the surface uses per pixel alpha (Surface has the SRCALPHA flag).

You could try something like

def str2rgb(s):
    rgba = s.split(",")
    if len(rgba) not in (3, 4):
        return (0, 0, 0)
    return tuple(map(int, rgba))


>>> color = "255,255,255"
>>> str2rgb(color)
(255, 255, 255)
>>> color2 = "255,255,255,0"
>>> str2rgb(color2)
(255, 255, 255, 0)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top