pygame.Color
pygame object for color representations
Color(r, g, b) -> Color
Color(r, g, b, a=255) -> Color
Color(color_value) -> Color
— Gets or sets the red value of the Color.
— Gets or sets the green value of the Color.
— Gets or sets the blue value of the Color.
— Gets or sets the alpha value of the Color.
— Gets or sets the CMY representation of the Color.
— Gets or sets the HSVA representation of the Color.
— Gets or sets the HSLA representation of the Color.
— Gets or sets the I1I2I3 representation of the Color.
— Returns the normalized RGBA values of the Color.
— Applies a certain gamma value to the Color.
— Set the number of elements in the Color to 1,2,3, or 4.
— returns a linear interpolation to the given Color.
— returns a Color where the r,g,b components have been multiplied by the alpha.
— Sets the elements of the color

这个 Color 类表示 RGBA 值范围为0到255(含0和255)的颜色值。它允许基本的算术运算 - 二进制运算 + , - , * , // , % 、和一元运算 ~ - 可创建新颜色,支持转换为其他颜色空间,例如 HSV 或 HSL 并允许您调整单色通道。未指定时,Alpha默认为255(完全不透明)。算术运算和 correct_gamma() 方法保留子类。对于二元运算符,返回的颜色的类是运算符的左侧颜色对象的类。

颜色对象支持与其他颜色对象和3或4元素整数元组进行相等比较。在pyGame 1.8.1中有一个错误,默认的Alpha是0,而不是像以前那样是255。

颜色对象输出C级数组接口。该接口导出一个只读的一维无符号字节数组,该数组的分配长度与颜色相同。新的缓冲区接口也被导出,具有与数组接口相同的特性。

楼层分区, // 和模数, % ,运算符不会引发被零除的异常。相反,如果右侧颜色中的颜色或Alpha通道为0,则结果为0。例如:

# These expressions are True
Color(255, 255, 255, 255) // Color(0, 64, 64, 64) == Color(0, 3, 3, 3)
Color(255, 255, 255, 255) % Color(64, 64, 64, 0) == Color(63, 63, 63, 0)

使用 int(color) 返回颜色的不可变整数值,可用作 dict 钥匙。此整数值不同于 pygame.Surface.get_at_mapped()get the mapped color value at a single pixel , pygame.Surface.map_rgb()convert a color into a mapped color value 和 pygame.Surface.unmap_rgb()convert a mapped integer color value into a Color 。它可以作为 color_value 参数为 Color (与集一起使用时很有用)。

看见 命名颜色 以获取可用命名颜色的示例。

参数
  • r (int) -- 范围为0到255(含)的红色值

  • g (int) -- 0到255之间的绿色数值

  • b (int) -- 0到255之间的蓝值(包括0和255

  • a (int) -- (可选)0到255之间的Alpha值,默认为255

  • color_value (Color or str or int or tuple(int, int, int, [int]) or list(int, int, int, [int])) -- 颜色值(有关支持的格式,请参阅下面的注释)。注意::支持 color_value 格式:|- **颜色对象:** 克隆给定的 :class:`Color` 对象|- 颜色名称:字符串: 要使用的颜色的名称,例如 'red' (所有支持的名称字符串都可以在 命名颜色 ,附样本)|- HTML颜色格式字符串: '#rrggbbaa' 或 '#rrggbb' ,其中rr、gg、bb和aa是0到0xFF范围内的两位十六进制数字,如果未提供,aa(Alpha)值默认为0xFF|- 十六进制数字字符串: '0xrrggbbaa' 或 '0xrrggbb' ,其中rr、gg、bb和aa是介于0x00和0xFF之间的两位十六进制数字,如果未提供,aa(Alpha)值默认为0xFF|- int: 要使用的颜色的整数值,使用十六进制数可以使此参数更具可读性,例如 0xrrggbbaa ,其中rr、gg、bb和aa是介于0x00和0xFF之间(包括0x00和0xFF)的两位十六进制数字,请注意,aa(Alpha)值对于int格式不是可选的,必须提供|- tuple/list of int color values: (R, G, B, A) 或 (R, G, B) ,其中R、G、B和A是0到255之间的整数值,如果未提供,则A(Alpha)值默认为255

返回

一个新创建的 Color 对象

返回类型

Color

Changed in pygame 2.0.0: 支持元组、列表和 Color 创建时的对象 Color 对象。

Changed in pygame 1.9.2: 颜色对象输出C级数组接口。

Changed in pygame 1.9.0: 颜色对象支持4元素的整数元组。

Changed in pygame 1.8.1: 类的新实现。

r
Gets or sets the red value of the Color.
r -> int

颜色的红色值。

g
Gets or sets the green value of the Color.
g -> int

颜色的绿色值。

b
Gets or sets the blue value of the Color.
b -> int

颜色的蓝色值。

a
Gets or sets the alpha value of the Color.
a -> int

颜色的Alpha值。

cmy
Gets or sets the CMY representation of the Color.
cmy -> tuple

这个 CMY 颜色的表示形式。这个 CMY 组件在范围内 C = [0, 1] , M = [0, 1] , Y = [0, 1] 。请注意,这不会返回绝对精确的 CMY 集合的值 RGB 在所有情况下都有价值。由于 RGB 从0到255的映射和 CMY 0-1舍入误差的映射可能会导致 CMY 值与您可能预期的略有不同。

hsva
Gets or sets the HSVA representation of the Color.
hsva -> tuple

这个 HSVA 颜色的表示形式。这个 HSVA 组件在范围内 H = [0,360] , S = [0,100] , V = [0,100] ,A= [0,100] 。请注意,这不会返回绝对精确的 HSV 集合的值 RGB 在所有情况下都有价值。由于 RGB 从0到255的映射和 HSV 0-100和0-360舍入误差的映射可能会导致 HSV 值与您可能预期的略有不同。

hsla
Gets or sets the HSLA representation of the Color.
hsla -> tuple

这个 HSLA 颜色的表示形式。这个 HSLA 组件在范围内 H = [0,360] , S = [0,100] , V = [0,100] ,A= [0,100] 。请注意,这不会返回绝对精确的 HSL 集合的值 RGB 在所有情况下都有价值。由于 RGB 从0到255的映射和 HSL 0-100和0-360舍入误差的映射可能会导致 HSL 值与您可能预期的略有不同。

i1i2i3
Gets or sets the I1I2I3 representation of the Color.
i1i2i3 -> tuple

这个 I1I2I3 颜色的表示形式。这个 I1I2I3 组件在范围内 I1 = [0, 1] , I2 = [-0.5, 0.5] , I3 = [-0.5, 0.5] 。请注意,这不会返回绝对精确的 I1I2I3 集合的值 RGB 在所有情况下都有价值。由于 RGB 从0到255的映射和 I1I2I3 0-1舍入误差的映射可能会导致 I1I2I3 值与您可能预期的略有不同。

normalize()
Returns the normalized RGBA values of the Color.
normalize() -> tuple

返回规格化的 RGBA 以浮点值表示的颜色值。

correct_gamma()
Applies a certain gamma value to the Color.
correct_gamma (gamma) -> Color

将特定的伽玛值应用于颜色并返回具有调整后的 RGBA 价值。

set_length()
Set the number of elements in the Color to 1,2,3, or 4.
set_length(len) -> None

默认颜色长度为4。颜色的长度可以为1、2、3或4。如果要解压为r、g、b而不是r、g、b、a,则此选项非常有用。如果要获取颜色的长度,请执行以下操作 len(acolor) 。

New in pygame 1.9.0.

lerp()
returns a linear interpolation to the given Color.
lerp(Color, float) -> Color

返回一个颜色,它是RGBA空间中自身和给定颜色之间的线性内插。第二个参数决定了自己和他人之间的距离。必须是介于0和1之间的值,其中0表示自身,1表示返回他人。

New in pygame 2.0.1.

premul_alpha()
returns a Color where the r,g,b components have been multiplied by the alpha.
premul_alpha() -> Color

返回一个新的颜色,其中每个红色、绿色和蓝色通道都与原始颜色的Alpha通道相乘。Alpha通道保持不变。

这在使用 BLEND_PREMULTIPLIED 混合模式标志用于 pygame.Surface.blit()draw one image onto another ,它假定使用它的所有曲面都使用预乘的Alpha颜色。

New in pygame 2.0.0.

update()
Sets the elements of the color
update(r, g, b) -> None
update(r, g, b, a=255) -> None
update(color_value) -> None

设置颜色的元素。请参阅参数 pygame.Color()pygame object for color representations 以获取此函数的参数。如果未设置Alpha值,它将不会更改。

New in pygame 2.0.1.




Edit on GitHub