Pygame 简明教程
Pygame - Color Object
Pygame 中的 Color 类用于表示屏幕背景、文本、形状和所有其他 Pygame 对象的颜色。它是通过传递红色、绿色、蓝色颜色的颜色值以及表示不透明值的 Alpha 值(可选)构建的。这些值中的每一个都在 0 到 255 之间。
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)
Alpha 的默认值为 255,表示完全不透明。各个属性可以访问和可以设置。
Default value of alpha is 255, meaning fully opaque. Individual 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. |
还可以使用 CMY、HSVA、HSLA 和 i1i2i3 等其他替代颜色模型。
Alternative color models like 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. |
我们可以使用预定义的字符串常量来表示 RGBA 颜色组合。下面列出了一些预定义的颜色:
We can use predefined string constants to represent RGBA color combinations. Some of the predefined colors are listed 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)
要列出所有预定义颜色,请运行以下 FOR 循环:
To enlist all predefined colors run following for loop −
for k, v in THECOLORS.items():
THECOLORS[unicode_(k)] = v