Skip to content

Tailwind Colors

Tailwind CSS color Palettes and helper functions.

PyRetailScience includes the raw Tailwind CSS color palettes and ListedColormaps and LinearSegmentedColormaps versions for use when charting.

Colors from Tailwind CSS https://raw.githubusercontent.com/tailwindlabs/tailwindcss/a1e74f055b13a7ef5775bdd72a77a4d397565016/src/public/colors.js

get_base_cmap()

Returns a ListedColormap with all the Tailwind colors.

Returns:

Name Type Description
ListedColormap ListedColormap

A ListedColormap with all the Tailwind colors.

Source code in pyretailscience/style/tailwind.py
def get_base_cmap() -> ListedColormap:
    """Returns a ListedColormap with all the Tailwind colors.

    Returns:
        ListedColormap: A ListedColormap with all the Tailwind colors.
    """
    color_order = [
        "red",
        "orange",
        "yellow",
        "green",
        "teal",
        "sky",
        "indigo",
        "purple",
        "pink",
        "slate",
        "amber",
        "lime",
        "emerald",
        "cyan",
        "blue",
        "violet",
        "fuchsia",
        "rose",
    ]
    color_numbers = [500, 300, 700]
    colors = [COLORS[color][color_number] for color_number in color_numbers for color in color_order]

    return ListedColormap(colors)

get_color_list(name, starting_color_code=50, ending_color_code=950)

Returns a filtered list of colors from the Tailwind color palette based on the given range.

Parameters:

Name Type Description Default
name str

The name of the color palette (e.g., "blue", "red").

required
starting_color_code int

The lowest color shade to use (default: 50).

50
ending_color_code int

The highest color shade to use (default: 950).

950

Returns:

Type Description
list[str]

list[str]: A filtered list of colors from the Tailwind color palette.

Source code in pyretailscience/style/tailwind.py
def get_color_list(name: str, starting_color_code: int = 50, ending_color_code: int = 950) -> list[str]:
    """Returns a filtered list of colors from the Tailwind color palette based on the given range.

    Args:
        name (str): The name of the color palette (e.g., "blue", "red").
        starting_color_code (int): The lowest color shade to use (default: 50).
        ending_color_code (int): The highest color shade to use (default: 950).

    Returns:
        list[str]: A filtered list of colors from the Tailwind color palette.
    """
    if name not in COLORS:
        msg = f"Color pallete {name} not found. Available color palettes are: {', '.join(COLORS.keys())}."
        raise ValueError(msg)
    return [COLORS[name][key] for key in sorted(COLORS[name].keys()) if starting_color_code <= key <= ending_color_code]

get_linear_cmap(name, starting_color_code=50, ending_color_code=950)

Returns a linear segmented colormap using Tailwind colors.

This function allows restricting the color range used in the colormap.

Parameters:

Name Type Description Default
name str

The name of the Tailwind color (e.g., "blue", "red").

required
starting_color_code int

The lowest color shade to use (default: 50).

50
ending_color_code int

The highest color shade to use (default: 950).

950

Returns:

Name Type Description
LinearSegmentedColormap LinearSegmentedColormap

A colormap object for matplotlib.

Source code in pyretailscience/style/tailwind.py
def get_linear_cmap(name: str, starting_color_code: int = 50, ending_color_code: int = 950) -> LinearSegmentedColormap:
    """Returns a linear segmented colormap using Tailwind colors.

    This function allows restricting the color range used in the colormap.

    Args:
        name (str): The name of the Tailwind color (e.g., "blue", "red").
        starting_color_code (int): The lowest color shade to use (default: 50).
        ending_color_code (int): The highest color shade to use (default: 950).

    Returns:
        LinearSegmentedColormap: A colormap object for matplotlib.
    """
    return LinearSegmentedColormap.from_list(
        f"{name}_linear_colormap",
        get_color_list(name, starting_color_code, ending_color_code),
    )

get_listed_cmap(name)

Returns a ListedColormap from the Tailwind color pallete of the given name.

Parameters:

Name Type Description Default
name str

The name of the color pallete.

required

Returns:

Name Type Description
ListedColormap ListedColormap

The color pallete as a ListedColormap.

Source code in pyretailscience/style/tailwind.py
def get_listed_cmap(name: str) -> ListedColormap:
    """Returns a ListedColormap from the Tailwind color pallete of the given name.

    Args:
        name (str): The name of the color pallete.

    Returns:
        ListedColormap: The color pallete as a ListedColormap.
    """
    return ListedColormap(get_color_list(name))

get_multi_color_cmap()

Returns a generator for multiple Tailwind colors and shades.

Returns:

Name Type Description
Generator Generator[str, None, None]

A generator yielding the required colors in a looping fashion.

Source code in pyretailscience/style/tailwind.py
def get_multi_color_cmap() -> Generator[str, None, None]:
    """Returns a generator for multiple Tailwind colors and shades.

    Returns:
        Generator: A generator yielding the required colors in a looping fashion.
    """
    color_order = ["green", "blue", "red", "orange", "yellow", "violet", "pink"]
    color_numbers = [500, 300, 700]

    return cycle([COLORS[color][color_number] for color_number in color_numbers for color in color_order])

get_single_color_cmap()

Returns a generator for Tailwind green shades.

Returns:

Name Type Description
Generator Generator[str, None, None]

A generator yielding green shades in a looping fashion.

Source code in pyretailscience/style/tailwind.py
def get_single_color_cmap() -> Generator[str, None, None]:
    """Returns a generator for Tailwind green shades.

    Returns:
        Generator: A generator yielding green shades in a looping fashion.
    """
    color_numbers = [500, 300, 700]
    return cycle([COLORS["green"][shade] for shade in color_numbers])