Types#
arcade.types.Color#
- class arcade.types.Color(r: int, g: int, b: int, a: int = 255)[source]#
A
tuple
subclass representing an RGBA Color.This class provides helpful utility methods and properties. When performance or brevity matters, arcade will usually allow you to use an ordinary
tuple
of RGBA values instead.All channels are byte values from 0 to 255, inclusive. If any are outside this range, a
ByteRangeError
will be raised, which can be handled as aValueError
.Examples:
>>> from arcade.types import Color >>> Color(255, 0, 0) Color(r=255, g=0, b=0, a=0) >>> Color(*rgb_green_tuple, 127) Color(r=0, g=255, b=0, a=127)
- Parameters:
r – the red channel of the color, between 0 and 255
g – the green channel of the color, between 0 and 255
b – the blue channel of the color, between 0 and 255
a – the alpha or transparency channel of the color, between 0 and 255
- classmethod from_gray(brightness: int, a: int = 255) Self [source]#
Return a shade of gray of the given brightness.
Example:
>>> custom_white = Color.from_gray(255) >>> print(custom_white) Color(r=255, g=255, b=255, a=255) >>> half_opacity_gray = Color.from_gray(128, 128) >>> print(half_opacity_gray) Color(r=128, g=128, b=128, a=128)
- Parameters:
brightness – How bright the shade should be
a – a transparency value, fully opaque by default
- Returns:
- classmethod from_hex_string(code: str) Self [source]#
Make a color from a hex code that is 3, 4, 6, or 8 hex digits long
Prefixing it with a pound sign (
#
/ hash symbol) is optional. It will be ignored if present.The capitalization of the hex digits (
'f'
vs'F'
) does not matter.3 and 6 digit hex codes will be treated as if they have an opacity of 255.
3 and 4 digit hex codes will be expanded.
Examples:
>>> Color.from_hex_string("#ff00ff") Color(r=255, g=0, b=255, a=255) >>> Color.from_hex_string("#ff00ff00") Color(r=255, g=0, b=255, a=0) >>> Color.from_hex_string("#FFF") Color(r=255, g=255, b=255, a=255) >>> Color.from_hex_string("FF0A") Color(r=255, g=255, b=0, a=170)
- classmethod from_iterable(iterable: Iterable[int]) Self [source]#
Create a color from an :py:class`Iterable` with 3-4 elements
If the passed iterable is already a Color instance, it will be returned unchanged. If the iterable has less than 3 or more than 4 elements, a ValueError will be raised.
Otherwise, the function will attempt to create a new Color instance. The usual rules apply, ie all values must be between 0 and 255, inclusive.
- Parameters:
iterable – An iterable which unpacks to 3 or 4 elements, each between 0 and 255, inclusive.
- classmethod from_normalized(color_normalized: Tuple[float, float, float, float]) Self [source]#
Convert normalized (0.0 to 1.0) channels into an RGBA Color
If the input channels aren’t normalized, a
arcade.utils.NormalizedRangeError
will be raised. This is a subclass of :py:class`ValueError` and can be handled as such.Examples:
>>> Color.from_normalized((1.0, 0.0, 0.0, 1.0)) Color(r=255, g=0, b=0, a=255) >>> normalized_half_opacity_green = (0.0, 1.0, 0.0, 0.5) >>> Color.from_normalized(normalized_half_opacity_green) Color(r=0, g=255, b=0, a=127)
- Parameters:
color_normalized (RGBANormalized) – The color as normalized (0.0 to 1.0) RGBA values.
- Returns:
- classmethod from_uint24(color: int, a: int = 255) Self [source]#
Return a Color from an unsigned 3-byte (24 bit) integer.
These ints may be between 0 and 16777215 (
0xFFFFFF
), inclusive.Example:
>>> Color.from_uint24(16777215) Color(r=255, g=255, b=255, a=255) >>> Color.from_uint24(0xFF0000) Color(r=255, g=0, b=0, a=255)
- Parameters:
color – a 3-byte int between 0 and 16777215 (
0xFFFFFF
)a – an alpha value to use between 0 and 255, inclusive.
- classmethod from_uint32(color: int) Self [source]#
Return a Color tuple for a given unsigned 4-byte (32-bit) integer
The bytes are interpreted as R, G, B, A.
Examples:
>>> Color.from_uint32(4294967295) Color(r=255, g=255, b=255, a=255) >>> Color.from_uint32(0xFF0000FF) Color(r=255, g=0, b=0, a=255)
- Parameters:
color (int) – An int between 0 and 4294967295 (
0xFFFFFFFF
)
- property normalized: Tuple[float, float, float, float]#
Return this color as a tuple of 4 normalized floats.
Examples:
>>> arcade.color.WHITE.normalized (1.0, 1.0, 1.0, 1.0) >>> arcade.color.BLACK.normalized (0.0, 0.0, 0.0, 1.0) >>> arcade.color.TRANSPARENT_BLACK.normalized (0.0, 0.0, 0.0, 0.0)
- classmethod random(r: int | None = None, g: int | None = None, b: int | None = None, a: int | None = None) Self [source]#
Return a random color.
The parameters are optional and can be used to fix the value of a particular channel. If a channel is not fixed, it will be randomly generated.
Examples:
# Randomize all channels >>> Color.random() Color(r=35, g=145, b=4, a=200) # Random color with fixed alpha >>> Color.random(a=255) Color(r=25, g=99, b=234, a=255)