Skip to content

Commit

Permalink
Add hex support for base cmaps definition with sequential_cmaps()
Browse files Browse the repository at this point in the history
  • Loading branch information
tupui committed Oct 19, 2023
1 parent 8f44f5f commit a50c803
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions panel/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from panel.layout.gridstack import GridStack

import simdec as sd
from simdec.visualization import colormap_from_single_color
from simdec.visualization import single_color_to_colormap


# panel app
Expand Down Expand Up @@ -178,7 +178,7 @@ def create_color_pickers(states, colors):

@pn.cache
def palette(res, colors_picked):
cmaps = [colormap_from_single_color(color_picked) for color_picked in colors_picked]
cmaps = [single_color_to_colormap(color_picked) for color_picked in colors_picked]
return sd.palette(res.states[::-1], cmaps=cmaps[::-1])


Expand Down
23 changes: 21 additions & 2 deletions src/simdec/visualization.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import itertools
from typing import Literal

Expand All @@ -13,6 +14,11 @@


SEQUENTIAL_PALETTES = [
"#DC267F",
"#E8EA2F",
"#26DCD1",
"#C552E4",
"#3F45D0",
"Oranges",
"Purples",
"Reds",
Expand All @@ -34,7 +40,20 @@
]


def colormap_from_single_color(
@functools.cache
def sequential_cmaps():
cmaps = []
for cmap in SEQUENTIAL_PALETTES:
try:
cmap_ = mpl.colormaps[cmap]
except KeyError:
color = mpl.colors.hex2color(cmap)
cmap_ = single_color_to_colormap(color)
cmaps.append(cmap_)
return cmaps


def single_color_to_colormap(
rgba_color: list[float] | str, *, factor: float = 0.5
) -> mpl.colors.LinearSegmentedColormap:
"""Create a linear colormap using a single color."""
Expand Down Expand Up @@ -92,7 +111,7 @@ def palette(
"""
n_cmaps = states[0]
if cmaps is None:
cmaps = [mpl.colormaps[cmap] for cmap in SEQUENTIAL_PALETTES[:n_cmaps]]
cmaps = sequential_cmaps()[:n_cmaps]
else:
cmaps = cmaps[:n_cmaps]
if len(cmaps) != n_cmaps:
Expand Down

0 comments on commit a50c803

Please sign in to comment.