color_helpers

This module contains varous helper functions related to color.

rivalcfg.color_helpers.is_color(string)

Checks if the given string is a valid color.

Parameters:

string (str) – The string to check.

Return type:

bool

>>> from rivalcfg.color_helpers import is_color
>>> is_color("#FF0000") # hexadecimal colors are supported
True
>>> is_color("#F00")  # short format allowed
True
>>> is_color("FF0000")  # "#" is optionnal
True
>>> is_color("#ff0000")  # case insensitive
True
>>> is_color("#FF00")  # not a valid color
False
>>> is_color("red")  # named color are supported
True
>>> is_color("RED")  # named color are case insensitive
True
>>> is_color("hello")  # not a valid color name
False
rivalcfg.color_helpers.parse_color_gradient_string(gradient)

Parse a color gradient string.

Parameters:

gradient (str) – The gradient string.

Return type:

list

>>> parse_color_gradient_string("0%: red, 33%: #00ff00, 66: 00f")
[{'pos': 0, 'color': (255, 0, 0)}, {'pos': 33, 'color': (0, 255, 0)}, {'pos': 66, 'color': (0, 0, 255)}]
>>> parse_color_gradient_string("-1%: red")
Traceback (most recent call last):
    ...
ValueError: invalid color stop position '-1%'
>>> parse_color_gradient_string("150: red")
Traceback (most recent call last):
    ...
ValueError: invalid color stop position '150%'
>>> parse_color_gradient_string("42%: hello")
Traceback (most recent call last):
    ...
ValueError: invalid color 'hello'
>>> parse_color_gradient_string("hello")
Traceback (most recent call last):
    ...
ValueError: invalid color gradient 'hello'. ...
rivalcfg.color_helpers.parse_color_string(color)

Converts a color string into an RGB tuple.

Parameters:

color (str) – The string to convert.

Returns:

(r, g, b)

>>> from rivalcfg.color_helpers import parse_color_string
>>> parse_color_string("#FF0000") # hexadecimal colors are supported
(255, 0, 0)
>>> parse_color_string("#F00")  # short format allowed
(255, 0, 0)
>>> parse_color_string("FF0000")  # "#" is optionnal
(255, 0, 0)
>>> parse_color_string("#ff0000")  # case insensitive
(255, 0, 0)
>>> parse_color_string("red")  # named color are supported
(255, 0, 0)
>>> parse_color_string("RED")  # named color are case insensitive
(255, 0, 0)