PyGameExamplesAndAnswers

StackOverflow            reply.it reply.it


Color

HSV/HSL

Related Stack Overflow questions:

The pygame.Color object can be used to convert between the RGB and HSL/HSV color schemes.

The hsva property:

Gets or sets the HSVA representation of the Color. The HSVA components are in the ranges H = [0, 360], S = [0, 100], V = [0, 100], A = [0, 100].

hsva = pygame.Color((red, green, blue, alpha)).hsva
color = pygame.Color(0)
color.hsva = (hue, saturation, value, alpha)
rgba = (color.r, color.g, color.b, color.a)

The hsla property:

Gets or sets the HSLA representation of the Color. The HSLA components are in the ranges H = [0, 360], S = [0, 100], V = [0, 100], A = [0, 100].

hsla = pygame.Color((red, green, blue, alpha)).hsla
color = pygame.Color(0)
color.hsla = (hue, saturation, lightness, alpha)
rgba = (color.r, color.g, color.b, color.a)

📁 Minimal example - Set HSLA color

HSV to RGB Color Conversion

Gradient

Lerp

Related Stack Overflow questions:

Pygame provides the pygame.Color object. The object can construct a color from various arguments (e.g. RGBA color channels, hexadecimal numbers, strings, …).
It also offers the handy method lerp, that can interpolate 2 colors:

Returns a Color which is a linear interpolation between self and the given Color in RGBA space

The pygame.Color object and the lerp method can be used to interpolate a color form a list of colors:

def lerp_color(colors, value):
    fract, index = math.modf(value)
    color1 = pygame.Color(colors[int(index) % len(colors)])
    color2 = pygame.Color(colors[int(index + 1) % len(colors)])
    return color1.lerp(color2, fract)