Skip to content

utils

Functions

lighten_color(color, opacity=0.5)

Change the opacity of a color.

Parameters:

Name Type Description Default
color Sequence | str

trace color

required
opacity float

opacity factor

0.5

Returns:

Type Description
str

less opaque color as a string for plotly usage

Source code in opentak/tak_theme/_utils.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def lighten_color(color: Sequence | str, opacity: float = 0.5) -> str:
    """Change the opacity of a color.

    :param color: trace color
    :param opacity: opacity factor
    :return: less opaque color as a string for plotly usage
    """
    if isinstance(color, str) and color.startswith("#"):
        fill_color_tuple = webcolors.hex_to_rgb(color)
    elif isinstance(color, str):
        fill_color_tuple = webcolors.name_to_rgb(color)
    else:
        fill_color_tuple = color

    return f"rgba{(*fill_color_tuple, opacity)}"

interpolate_colors(color_list, nb_colors, return_steps=False)

Given a list of colors, create a discrete gradient.

Parameters:

Name Type Description Default
color_list list[str]

list of colors

required
nb_colors int

nb of colors to get

required
return_steps bool

return float steps [0, 1].

False

Returns:

Type Description
list[str] | tuple[list[str], list[float]]

list of colors containing plus new colors interpolated

Source code in opentak/tak_theme/_utils.py
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
def interpolate_colors(
    color_list: list[str], nb_colors: int, return_steps: bool = False
) -> list[str] | tuple[list[str], list[float]]:
    """Given a list of colors, create a discrete gradient.

    :param color_list: list of colors
    :param nb_colors: nb of colors to get
    :param return_steps: return float steps [0, 1].

    :return: list of colors containing plus new colors interpolated
    """
    # similar to chroma web tool
    space_interpolation = "srgb"

    if len(color_list) < 2:
        raise ValueError("color_list should contain at least two colors")

    if nb_colors < len(color_list):
        raise ValueError("nb_colors should be higher than color_list")

    interpolator = Color.interpolate(color_list, space=space_interpolation)
    steps = cast("list[float]", np.linspace(0, 1, nb_colors).tolist())
    interpolated = [interpolator(x).to_string(hex=True) for x in steps]

    if return_steps:
        return interpolated, steps
    return interpolated

apply_mpl_style(fig=None)

Apply theme to a matplotlib figure.

Source code in opentak/tak_theme/_utils.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def apply_mpl_style(fig: Figure | None = None) -> None:
    """Apply theme to a matplotlib figure."""
    if fig is None:
        fig = plt.gcf()

    axs = fig.axes
    for ax in axs:
        ax.tick_params(axis="both", colors=LABEL_TICK)
        ax.xaxis.label.set_color(LABEL_TICK)
        ax.yaxis.label.set_color(LABEL_TICK)
        ax.grid(visible=True)
        # type ignore because the type hints for matplotlib are not up to date
        for tick in ax.get_xticklabels():  # type: ignore[operator]
            tick.set_fontname("Barlow")
            tick.set_fontweight("semibold")
        for tick in ax.get_yticklabels():  # type: ignore[operator]
            tick.set_fontname("Barlow")
            tick.set_fontweight("semibold")
    fig.tight_layout()

set_style()

Set plotly default style to 'tak_theme'.

Source code in opentak/tak_theme/_utils.py
89
90
91
92
93
def set_style() -> None:
    """Set plotly default style to 'tak_theme'."""
    pio.templates.default = "tak_theme"
    with contextlib.suppress(ImportError):
        plt.style.use("opentak.tak_theme.mpl_tak")  # type: ignore[attr-defined]