"""Palette and chart theming for the African Books Recovery Project. Colour follows the validated reference palette: categorical hues are assigned in fixed slot order and never cycled; sequential encodings use a single hue, light -> dark; ordinal ramps (the recovery funnel) stay inside the steps that clear contrast against the surface. Text always wears text tokens, never a series colour. """ from __future__ import annotations import altair as alt import streamlit as st # --- categorical slots, fixed order ------------------------------------------- CATEGORICAL = { "light": ["#2a78d6", "#eb6834", "#1baf7a", "#eda100", "#e87ba4", "#008300", "#4a3aa7", "#e34948"], "dark": ["#3987e5", "#d95926", "#199e70", "#c98500", "#d55181", "#008300", "#9085e9", "#e66767"], } # --- sequential / ordinal blue ramp ------------------------------------------- # Ordinal use starts no lighter than step 250 on light, no darker than 600 on dark. ORDINAL = { "light": ["#86b6ef", "#5598e7", "#3987e5", "#2a78d6", "#256abf", "#1c5cab"], "dark": ["#cde2fb", "#9ec5f4", "#6da7ec", "#3987e5", "#256abf", "#184f95"], } SURFACE = {"light": "#fcfcfb", "dark": "#1a1a19"} TEXT_PRIMARY = {"light": "#0b0b0b", "dark": "#ffffff"} TEXT_SECONDARY = {"light": "#52514e", "dark": "#c3c2b7"} TEXT_MUTED = {"light": "#75736c", "dark": "#8f8d84"} GRID = {"light": "#e8e7e3", "dark": "#2f2f2c"} BORDER = {"light": "#dedcd6", "dark": "#33332f"} PANEL = {"light": "#f5f4f1", "dark": "#212120"} # status palette — reserved, never reused as "series N" STATUS = { "light": {"good": "#008300", "warning": "#eda100", "serious": "#eb6834", "critical": "#e34948"}, "dark": {"good": "#008300", "warning": "#c98500", "serious": "#d95926", "critical": "#e66767"}, } def mode() -> str: """Resolve the viewer's theme. Falls back to light if Streamlit can't say.""" try: t = st.context.theme if t and getattr(t, "type", None) in ("light", "dark"): return t.type except Exception: pass try: base = st.get_option("theme.base") if base in ("light", "dark"): return base except Exception: pass return "light" class Palette: def __init__(self, m: str): self.m = m self.series = CATEGORICAL[m] self.ordinal = ORDINAL[m] self.surface = SURFACE[m] self.text = TEXT_PRIMARY[m] self.text2 = TEXT_SECONDARY[m] self.muted = TEXT_MUTED[m] self.grid = GRID[m] self.border = BORDER[m] self.panel = PANEL[m] self.status = STATUS[m] def scale(self, domain): """Categorical scale bound to an explicit domain, so colour follows the entity and a filter that drops series never repaints the survivors.""" return alt.Scale(domain=list(domain), range=self.series[: len(domain)]) def palette() -> Palette: return Palette(mode()) def register_altair_theme(p: Palette) -> None: def _theme(): return { "config": { "background": "transparent", "font": "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif", "view": {"stroke": "transparent", "continuousWidth": 400, "continuousHeight": 260}, "axis": { "labelColor": p.text2, "titleColor": p.muted, "titleFontSize": 11, "titleFontWeight": "normal", "labelFontSize": 11, "domainColor": p.grid, "tickColor": p.grid, "gridColor": p.grid, "gridOpacity": 0.7, "gridWidth": 1, "labelPadding": 6, }, "legend": { "labelColor": p.text2, "titleColor": p.muted, "labelFontSize": 11, "titleFontSize": 11, "titleFontWeight": "normal", "symbolType": "square", "symbolSize": 90, }, "title": {"color": p.text, "fontSize": 13, "fontWeight": 600, "anchor": "start", "dy": -6}, "bar": {"cornerRadiusEnd": 4}, "line": {"strokeWidth": 2}, "point": {"size": 90, "filled": True}, "range": {"category": p.series, "ordinal": p.ordinal, "heatmap": p.ordinal}, } } alt.theme.register("abrp", enable=True)(_theme) def css(p: Palette) -> str: return f""" """ def tiles(items) -> str: """items: iterable of (value, key, subtext).""" cells = "".join( f'
{v}
{k}
' f'
{s}
' for v, k, s in items ) return f'
{cells}
' def chip(confidence: str) -> str: cls = {"high": "hi", "medium": "me", "low": "lo"}.get(confidence, "") return f'{confidence} confidence'