English 中文(简体)
Pygame - Color object
  • 时间:2024-12-22

Pygame - Color Object


Previous Page Next Page  

The Color class in Pygame is used to represent color of screen background, text, shapes and all other Pygame objects. It constructed by passing color values for Red, Green, Blue colors and optionally alpha value that represents opaque value. Each of these values range between 0 to 255.


color = pygame.Color(r, g, b, a=255)

Default value of alpha is 255, meaning fully opaque. Inspanidual attributes are accessible and can be set.

pygame.Color.r Gets or sets the red value of the Color.
pygame.Color.g Gets or sets the green value of the Color.
pygame.Color.b Gets or sets the blue value of the Color.
pygame.Color.a Gets or sets the alpha value of the Color.

Alternative color models pke CMY, HSVA, HSLA and i1i2i3 can also be used.

pygame.Color.cmy Gets or sets the CMY representation of the Color. Cyan, Magenta, Yellow
pygame.Color.hsva Gets or sets the HSVA representation of the Color. Hue, Saturation, Value
pygame.Color.hsla Gets or sets the HSLA representation of the Color. Hue, Saturation, Lightness
pygame.Color.i1i2i3 Gets or sets the I1I2I3 representation of the Color.

We can use predefined string constants to represent RGBA color combinations. Some of the predefined colors are psted below −

    black : (0, 0, 0, 255)

    blue : (0, 0, 255, 255),

    cyan : (0, 255, 255, 255),

    gold : (255, 215, 0, 255),

    gray : (190, 190, 190, 255),

    green : (0, 255, 0, 255),

    orange : (255, 165, 0, 255),

    purple : (160, 32, 240, 255),

    red : (255, 0, 0, 255),

    violet : (238, 130, 238, 255)

    yellow : (255, 255, 0, 255),

    white : (255, 255, 255, 255)

To enpst all predefined colors run following for loop −


for k, v in THECOLORS.items():
   THECOLORS[unicode_(k)] = v
Advertisements