Skip to content

Colors

artlo includes a series of color palettes, colormaps and helper functions for working with them. You can find the relevant code documentation below.

Palettes

You can access the color palettes via artlo.color.palettes. This object is a modified dictionary, and you can access the items by indexing (artlo.color.palettes['pastel']) or as attributes (artlo.color.palettes.vegetation). These return a list of hex codes for the colors in the palette. You'll typically want these for mapping items with a discrete, known number of colors. Custom color paletters include:

artlo.color.palettes.keys()

>>> dict_keys(['salo', 'cfo', 'bold', 'pastel', 'vegetation', 'wildfire', 'wind', 'weather'])

Colormaps

For mapping continuous patterns, or many classes, the above color palettes are also accessible as colormaps. These colormaps interpolate colors between the different palette values, meaning they can be indexed from 0-1 to retrieve the color at the indexed point. You can pass a single value or a list of values to retun as many colors as you need, which are returned as RGBA tuples.

# get the midpoint vegetation color value
artlo.color.cmaps.vegetation(0.5)

>>> (0.699679610406254, 0.7732154299628349, 0.3038831218762014, 1.0)

# get a range of color values
artlo.color.cmaps.vegetation([0.0, 0.5, 1.0])

>>> array([[0.97647059, 0.98039216, 0.89803922, 1.        ],
>>>        [0.69967961, 0.77321543, 0.30388312, 1.        ],
>>>        [0.19607843, 0.34901961, 0.        , 1.        ]])

Support functions

You can create your own color map by passing a custom color palette to artlo.color.create_cmap(['list', 'of', 'colors']).

Convert between hex/rgba with artlo.color.to_hex(rgba) or artlo.color.to_rgba(hex).

Color reference

Continuous color palettes

artlo continuous colormaps

Discrete color palettes

artlo discrete colormaps

The above images are generated by scripts/plot-colormaps.py.


Code documentation

Support for working with colormaps, palettes, and type conversions.

create_cmap(palette, n=None, stops=None, alpha=1.0, name='artlo')

Creates a matplotlib-format linear segmented color map from a list of colors.

Parameters:

Name Type Description Default
palette list

a list of hex codes or a list of (r,g,b,a) tuples

required
stops list

a list of 0.0-1.0 values mapping each palette entry with it's relative color map location

None
alpha float

scalar 0-1 float opacity value (transparent-opaque, respectively)

1.0
name str

the name of the color map

'artlo'

Returns:

Name Type Description
cmap

a matplotlib colormap.

Source code in artlo/color.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def create_cmap(
    palette: list,
    n: int = None,
    stops: list = None,
    alpha: float = 1.0,
    name: str = "artlo",
):
    """Creates a matplotlib-format linear segmented color map from a list of colors.

    Args:
        palette: a list of hex codes or a list of (r,g,b,a) tuples
        stops: a list of 0.0-1.0 values mapping each palette entry with it's relative color map location
        alpha: scalar 0-1 float opacity value (transparent-opaque, respectively)
        name: the name of the color map

    Returns:
        cmap: a matplotlib colormap.
    """
    assert type(palette) in (list, tuple), "palette must be iterable"
    assert len(palette) > 1, "palette must include more than one color"
    assert 0 < alpha <= 1, "alpha must be between 0-1"

    # convert hex codes to rgba tuples
    rgba = []
    for color in palette:

        # check if it's an rgb or an rgba tuple
        if type(color) in (list, tuple):
            if len(color) == 3:
                rgba.append(tuple(color + (1,)))
            else:
                rgba.append(tuple(color))

        else:
            if "#" in color:
                rgba.append(colors.ColorConverter.to_rgba(color))
            else:
                raise TypeError("color passed is not hexcode or rgba tuple")

    # update alpha values
    for idx, color in enumerate(rgba):
        lcolor = list(color)
        lcolor[3] = alpha
        rgba[idx] = tuple(lcolor)

    # set equal-distance stops if not explicitly passsed
    if stops is None:
        n_colors = len(rgba)
        step = 1.0 / (n_colors - 1)
        stops = [x * step for x in range(0, n_colors)]

    # create the formatted color map
    color_pairs = [(stop, color) for stop, color in zip(stops, rgba)]
    cmap = colors.LinearSegmentedColormap.from_list(name, color_pairs)

    return cmap

to_hex(rgba)

Converts an rgb/rgba color tuple to it's hexadecimal code.

Parameters:

Name Type Description Default
rgba tuple

an input color (e.g., rgb or rgba list/tuple).

required

Returns:

Name Type Description
hexcode

a hexadecimal string corresponding to the input color.

Source code in artlo/color.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def to_hex(rgba: tuple):
    """Converts an rgb/rgba color tuple to it's hexadecimal code.

    Args:
        rgba: an input color (e.g., rgb or rgba list/tuple).

    Returns:
        hexcode: a hexadecimal string corresponding to the input color.
    """
    try:
        hexcode = colors.to_hex(rgba)
    except ValueError:
        hexcode = "#{:02X}{:02X}{:02X}".format(rgba[0], rgba[1], rgba[2])

    return hexcode

to_rgba(hexcode)

Converts a hexadecimal color to a tuple of RGBA values.

Parameters:

Name Type Description Default
hexcode str

a hexadecimal string, e.g. "#000000" for black.

required

Returns:

Name Type Description
rgba

a 4-element list of 0-1 rgba values.

Source code in artlo/color.py
80
81
82
83
84
85
86
87
88
89
90
91
def to_rgba(hexcode: str):
    """Converts a hexadecimal color to a tuple of RGBA values.

    Args:
        hexcode: a hexadecimal string, e.g. "#000000" for black.

    Returns:
        rgba: a 4-element list of 0-1 rgba values.
    """
    rgba = colors.to_rgba(hexcode)

    return rgba