Source code for dipy.viz.skyline.UI.theme

"""Theme utilities for Skyline UI components."""

from pathlib import Path
from urllib.request import urlretrieve

from dipy.utils.logging import logger
from dipy.utils.optpkg import optional_package

fury, has_fury, _ = optional_package("fury", min_version="2.0.0")
if has_fury:
    from PIL import Image
else:
    Image = fury

DIPY_DATA_MIRROR = "https://github.com/dipy/dipy_data/raw/refs/heads/master"
SKYLINE_HOME = Path("~").expanduser() / ".dipy-skyline"

if not SKYLINE_HOME.exists():
    SKYLINE_HOME.mkdir(parents=True, exist_ok=True)

ASSETS = SKYLINE_HOME / "assets"
if not ASSETS.exists():
    ASSETS.mkdir(parents=True, exist_ok=True)

FONTS = ASSETS / "fonts"
if not FONTS.exists():
    FONTS.mkdir(parents=True, exist_ok=True)

IMAGES = ASSETS / "images"
if not IMAGES.exists():
    IMAGES.mkdir(parents=True, exist_ok=True)

LOGO = IMAGES / "dipy-logo.png"
LOGO_SMALL = IMAGES / "dipy-logo-small.png"
if not LOGO.exists():
    logger.info("Downloading Skyline UI logo...")
    urlretrieve(f"{DIPY_DATA_MIRROR}/dipy-skyline/assets/images/dipy-logo.png", LOGO)

if has_fury:
    with Image.open(LOGO) as img:
        logo_img = img.convert("RGBA")
        logo_img.load()
        logo_img = logo_img.copy()

    if logo_img.size != (48, 48):
        logo_img = logo_img.resize((48, 48), Image.Resampling.LANCZOS)
        logo_img.save(LOGO, format="PNG", optimize=True)

    logo_small_ok = False
    if LOGO_SMALL.exists():
        with Image.open(LOGO_SMALL) as img_small_existing:
            logo_small_ok = img_small_existing.size == (32, 32)

    if not logo_small_ok:
        img_small = logo_img.resize((32, 32), Image.Resampling.LANCZOS)
        img_small.save(LOGO_SMALL, format="PNG", optimize=True)


FONT = FONTS / "Inter_18pt-Regular.ttf"
if not FONT.exists():
    logger.info("Downloading Skyline UI font...")
    urlretrieve(
        f"{DIPY_DATA_MIRROR}/dipy-skyline/assets/fonts/Inter_18pt-Regular.ttf", FONT
    )

FONT_AWESOME = FONTS / "fontawesome-webfont.ttf"
if not FONT_AWESOME.exists():
    logger.info("Downloading Font Awesome Icons for Skyline UI...")
    urlretrieve(
        f"{DIPY_DATA_MIRROR}/dipy-skyline/assets/fonts/fontawesome-webfont.ttf",
        FONT_AWESOME,
    )


[docs] def hex_to_rgba(hex_color, *, alpha=1.0): """Convert hex colors to rgba Parameters ---------- hex_color : str Hexcode for the color. alpha : float, optional Transparency of the color. Returns ------- tuple RGBA color tuple. """ hex_color = hex_color.lstrip("#") r = int(hex_color[0:2], 16) / 255.0 g = int(hex_color[2:4], 16) / 255.0 b = int(hex_color[4:6], 16) / 255.0 return (r, g, b, alpha)
THEME = { "background": hex_to_rgba("#191919"), "primary": hex_to_rgba("#EE942E"), "secondary": hex_to_rgba("#FFFFFF"), "text": hex_to_rgba("#838383"), "text_highlight": hex_to_rgba("#EE942E"), "shadow": hex_to_rgba("#838383", alpha=0.12), } SLIDER_THEME = { "track_color": THEME["secondary"], "track_bg": THEME["background"], "thumb_color": THEME["primary"], "track_covered_color": THEME["primary"], "label_color": THEME["text"], "value_color": THEME["text_highlight"], "shadow_color": THEME["shadow"], } WINDOW_THEME = { "title_color": THEME["text"], "title_active_color": THEME["text_highlight"], "background_color": THEME["background"], "collapse_color": THEME["secondary"], } SWITCH_THEME = { "background_color": THEME["background"], "active_color": THEME["text"], "inactive_text_color": THEME["text"], "active_text_color": THEME["secondary"], "border_color": THEME["text"], } DROPDOWN_THEME = { "background_color": THEME["background"], "border_color": THEME["text_highlight"], "hover_color": THEME["text_highlight"], "selected_color": THEME["secondary"], "arrow_color": THEME["text_highlight"], }