sdl2.ext.color - Color Representation and Conversion¶
The sdl2.ext.color module provides a number of classes and functions for working with colors.
The primary part of this module is the Color class, which allows flexible representation of colours across sdl2.ext functions and offers easy conversion between colorspaces (e.g. RGB to HSV). Additionally, this module provides functions for easily converting RGBA/ARGB integers and hexidecimal strings to Color objects.
-
class
sdl2.ext.color.
Color
(r=255, g=255, b=255, a=255)[source]¶ A class for working with and converting RGBA colors.
This class represents the 4 RGBA color channels (red, green, blue, and alpha transparency) as integers from 0 to 255. It also provides methods for converting colors to alternative color spaces (e.g. HSV or CMY).
Color
objects support basic arithmetic operations (+, -, *, /, %
), which operate on a per-channel basis. For example, the operationcolor = color1 + color2
is the same as
color = Color() color.r = min(color1.r + color2.r, 255) color.g = min(color1.g + color2.g, 255)
All arithmetic operations guarantee that the channel values stay within the allowed range of [0, 255].
Parameters: - r (int, optional) – An integer between 0 and 255 indicating the red level of the color. Defaults to 255.
- g (int, optional) – An integer between 0 and 255 indicating the green level of the color. Defaults to 255.
- b (int, optional) – An integer between 0 and 255 indicating the blue level of the color. Defaults to 255.
- a (int, optional) – An integer between 0 and 255 indicating the alpha trasparency level of the color, with 0 being fully transparent and 255 being fully opaque. Defaults to 255.
-
r
¶ The 8-bit RGBA red level for the color.
Type: “int
-
g
¶ The 8-bit RGBA green level for the color.
Type: “int
-
b
¶ The 8-bit RGBA blue level for the color.
Type: “int
-
a
¶ The 8-bit RGBA alpha transparency level for the color.
Type: “int
-
hsva
¶ A representation of the color in HSV(A) color space.
The HSVA color space represents colors in terms of hue, saturation, value (brightness), and alpha (transparency). Hue is represented as a value on color wheel between 0 and 360, whereas saturation, brightness, and alpha are all represented as values from 0 to 100.
Note that due to rounding errors, this may not return the exact HSVA values for the given color.
Type: tuple
-
hsla
¶ A representation of the color in HSL(A) color space.
The HSLA color space represents colors in terms of hue, saturation, lightness, and alpha (transparency). Hue is represented as a value on color wheel between 0 and 360, whereas saturation, lightness, and alpha are all represented as values from 0 to 100.
Note that due to rounding errors, this may not return the exact HSLA values for the given color.
Type: tuple
-
i1i2i3
¶ A representation of the color in I1I2I3 color space.
The I1I2I3 color space represents colors in terms of a color-independent intensity level (I1) and two chromatic channels (I2 and I3), with the aim of minimizing correlations between its channels for natural images. Intensity (I1) is represented as a float between 0.0 and 1.0, whereas the color channels (I2 and I3) are represented as floats between -0.5 and 0.5, inclusive.
Note that due to rounding errors, this may not return the exact I1I2I3 values for the given color.
Type: tuple
-
sdl2.ext.color.
is_rgb_color
(v)[source]¶ Checks whether a value be converted to an RGB color.
Parameters: v – The value to try and interpret as an RGB color. Returns: True if the value can be interpreted as an RGB color, otherwise False. Return type: bool
-
sdl2.ext.color.
is_rgba_color
(v)[source]¶ Checks whether a value be converted to an RGBA color.
Parameters: v – The value to try and interpret as an RGBA color. Returns: True if the value can be interpreted as an RGBA color, otherwise False. Return type: bool
-
sdl2.ext.color.
argb_to_color
(v)[source]¶ Converts a 32-bit ARGB integer value to a
sdl2.ext.Color
.Parameters: v (int) – An integer representing a color in ARGB format. Returns: An object representing the given color. Return type: sdl2.ext.Color
-
sdl2.ext.color.
ARGB
(v)¶ Converts a 32-bit ARGB integer value to a
sdl2.ext.Color
.Parameters: v (int) – An integer representing a color in ARGB format. Returns: An object representing the given color. Return type: sdl2.ext.Color
-
sdl2.ext.color.
rgba_to_color
(v)[source]¶ Converts a 32-bit RGBA integer value to a
sdl2.ext.Color
.Parameters: v (int) – An integer representing a color in RGBA format. Returns: An object representing the given color. Return type: sdl2.ext.Color
-
sdl2.ext.color.
RGBA
(v)¶ Converts a 32-bit RGBA integer value to a
sdl2.ext.Color
.Parameters: v (int) – An integer representing a color in RGBA format. Returns: An object representing the given color. Return type: sdl2.ext.Color
-
sdl2.ext.color.
string_to_color
(s)[source]¶ Converts a hex color string to a Color value.
Hex colors can be specified in any of the following formats:
- #RGB
- #RGBA
- #RRGGBB
- #RRGGBBAA
- 0xRGB
- 0xRGBA
- 0xRRGGBB
- 0xRRGGBBAA
Parameters: s (str) – A valid hex color in string format. Returns: An object representing the given color. Return type: sdl2.ext.Color
-
sdl2.ext.color.
convert_to_color
(v)[source]¶ Tries to convert an arbitrary object to a
sdl2.ext.Color
.If an integer is provided, it is assumed to be in ARGB layout.
Parameters: v – An arbitrary object type representing a color. Returns: An object representing the given color. Return type: sdl2.ext.Color
Raises: ValueError
– If the value could not be converted successfully.
-
sdl2.ext.color.
COLOR
(v)¶ Tries to convert an arbitrary object to a
sdl2.ext.Color
.If an integer is provided, it is assumed to be in ARGB layout.
Parameters: v – An arbitrary object type representing a color. Returns: An object representing the given color. Return type: sdl2.ext.Color
Raises: ValueError
– If the value could not be converted successfully.