diff --git a/editor.py b/editor.py index 6dc11a4..5f70167 100644 --- a/editor.py +++ b/editor.py @@ -72,28 +72,81 @@ def _mix(a, b, amount): def _lighten(value, amount): return _mix(value, "#ffffff", amount) def _darken(value, amount): return _mix(value, "#000000", amount) +def _luminance(value): + rgb = _hex_rgb(value) + if rgb is None: + return 0.0 + channels = [c / 255.0 for c in rgb] + linear = [c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4 for c in channels] + return 0.2126 * linear[0] + 0.7152 * linear[1] + 0.0722 * linear[2] + +def _contrast_ratio(a, b): + return (max(_luminance(a), _luminance(b)) + 0.05) / (min(_luminance(a), _luminance(b)) + 0.05) + +def _contrast_text(background): + return "#ffffff" if _contrast_ratio("#ffffff", background) >= _contrast_ratio("#000000", background) else "#000000" + +def _ensure_contrast(foreground, background, minimum=4.5): + if _contrast_ratio(foreground, background) >= minimum: + return foreground + target = "#ffffff" if _contrast_ratio("#ffffff", background) > _contrast_ratio("#000000", background) else "#000000" + lo, hi = 0.0, 1.0 + for _ in range(12): + mid = (lo + hi) / 2 + candidate = _mix(foreground, target, mid) + if _contrast_ratio(candidate, background) >= minimum: + hi = mid + else: + lo = mid + return _mix(foreground, target, hi) + +def _surface_step(value, background, amount): + return _lighten(value, amount) if _luminance(background) < 0.5 else _darken(value, amount) + def derive_theme(base): c = dict(BASE_THEME) if isinstance(base, dict): for key in BASE_THEME: value = base.get(key) - if isinstance(value, str) and re.fullmatch(r"#[0-9a-fA-F]{6}", value.strip()): c[key] = value.strip() - bg, surface = c["background"], c["surface"] - text, secondary = c["text"], c["text_secondary"] - primary, accent = c["primary"], c["accent"] - success, warning, danger = c["success"], c["warning"], c["danger"] + if isinstance(value, str) and re.fullmatch(r"#[0-9a-fA-F]{6}", value.strip()): + c[key] = value.strip() + + bg = c["background"] + surface = c["surface"] + # User-selected text colors are kept as close as possible to the chosen + # color, but are automatically corrected when they would become unreadable. + text = _ensure_contrast(c["text"], bg, 4.5) + secondary = _ensure_contrast(c["text_secondary"], bg, 4.5) + surface_alt = _surface_step(surface, bg, 0.14) + control = _surface_step(surface, bg, 0.10) + control_hover = _surface_step(surface, bg, 0.20) + primary = c["primary"] + accent = c["accent"] + primary_hover = _surface_step(primary, bg, 0.14) + success = c["success"] + warning = c["warning"] + danger = c["danger"] + return { - "background": bg, "surface": surface, "surface_alt": _lighten(surface,.12), "surface_dark": _darken(bg,.22), - "text": text, "text_secondary": secondary, "text_muted": _mix(secondary,bg,.35), "text_faint": _mix(secondary,bg,.58), - "border": _lighten(surface,.28), "border_subtle": _lighten(surface,.16), "control": _lighten(surface,.12), "control_hover": _lighten(surface,.24), - "primary": primary, "primary_hover": _lighten(primary,.16), "accent": accent, - "success": _darken(success,.42), "success_text": _lighten(success,.68), "warning": _darken(warning,.48), - "danger": danger, "danger_soft": _darken(danger,.58), "danger_soft_hover": _darken(danger,.42), - "error": _darken(danger,.62), "error_text": _lighten(danger,.70), "recording": _darken(danger,.08), "recording_border": _darken(danger,.52), - "selection": "rgba(80,150,255,.16)", "shadow": "#000000", "scroll_thumb": _mix(secondary,bg,.35), - "scroll_thumb_hover": _mix(secondary,bg,.18), "scroll_thumb_active": secondary, "live_surface": _mix(danger,bg,.84), - "table_header": _lighten(surface,.08), "table_text": text, "footer": _mix(secondary,bg,.50), "white": "#ffffff", - "surface_modal": _lighten(surface,.05), "overlay": "rgba(0,0,0,.78)", + "background": bg, "surface": surface, "surface_alt": surface_alt, "surface_dark": _darken(bg,.22), + "text": text, "text_secondary": secondary, + "text_muted": _ensure_contrast(_mix(secondary,bg,.35), bg, 3.0), + "text_faint": _ensure_contrast(_mix(secondary,bg,.58), bg, 2.2), + "border": _surface_step(surface,bg,.28), "border_subtle": _surface_step(surface,bg,.16), + "control": control, "control_hover": control_hover, + "primary": primary, "primary_hover": primary_hover, "primary_text": _contrast_text(primary), + "accent": accent, + "success": success, "success_text": _contrast_text(success), + "warning": warning, "warning_text": _contrast_text(warning), + "danger": danger, "danger_soft": _mix(danger,bg,.82), "danger_soft_hover": _mix(danger,bg,.70), + "error": _mix(danger,bg,.72), "error_text": _contrast_text(_mix(danger,bg,.72)), + "recording": danger, "recording_border": _mix(danger,bg,.55), "recording_text": _contrast_text(danger), + "selection": _mix(primary,bg,.84), "shadow": "#000000", + "scroll_thumb": _mix(secondary,bg,.35), "scroll_thumb_hover": _mix(secondary,bg,.18), + "scroll_thumb_active": secondary, "live_surface": _mix(danger,bg,.84), + "table_header": _surface_step(surface,bg,.08), "table_text": _ensure_contrast(text,surface,4.5), + "footer": _ensure_contrast(_mix(secondary,bg,.50), bg, 2.2), "white": "#ffffff", + "surface_modal": _surface_step(surface,bg,.05), "overlay": "rgba(0,0,0,.78)", } DEFAULT_THEME = derive_theme(BASE_THEME) @@ -1980,7 +2033,7 @@ a { color:var(--color-text-secondary); } .color-field input[type=color] { width:48px; height:40px; padding:3px; background:var(--color-control); border:1px solid var(--color-control-hover); border-radius:5px; cursor:pointer; } .theme-intro { margin:-4px 0 16px; color:var(--color-text-muted); font-size:14px; line-height:1.45; } .actions { display:flex; gap:10px; flex-wrap:wrap; margin-top:20px; } -button { border:0; border-radius:5px; padding:10px 16px; background:var(--color-primary); color:var(--color-white); font-size:15px; cursor:pointer; } +button { border:0; border-radius:5px; padding:10px 16px; background:var(--color-primary); color:var(--color-primary-text); font-size:15px; cursor:pointer; } button:hover { background:var(--color-primary-hover); } .note { color:var(--color-text-muted); font-size:13px; margin-top:8px; } .message { padding:10px 12px; border-radius:6px; margin-bottom:16px; background:var(--color-success); color:var(--color-success-text); } diff --git a/recorder.py b/recorder.py index cdeae6d..ba5ba63 100644 --- a/recorder.py +++ b/recorder.py @@ -102,28 +102,81 @@ def _mix(a, b, amount): def _lighten(value, amount): return _mix(value, "#ffffff", amount) def _darken(value, amount): return _mix(value, "#000000", amount) +def _luminance(value): + rgb = _hex_rgb(value) + if rgb is None: + return 0.0 + channels = [c / 255.0 for c in rgb] + linear = [c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4 for c in channels] + return 0.2126 * linear[0] + 0.7152 * linear[1] + 0.0722 * linear[2] + +def _contrast_ratio(a, b): + return (max(_luminance(a), _luminance(b)) + 0.05) / (min(_luminance(a), _luminance(b)) + 0.05) + +def _contrast_text(background): + return "#ffffff" if _contrast_ratio("#ffffff", background) >= _contrast_ratio("#000000", background) else "#000000" + +def _ensure_contrast(foreground, background, minimum=4.5): + if _contrast_ratio(foreground, background) >= minimum: + return foreground + target = "#ffffff" if _contrast_ratio("#ffffff", background) > _contrast_ratio("#000000", background) else "#000000" + lo, hi = 0.0, 1.0 + for _ in range(12): + mid = (lo + hi) / 2 + candidate = _mix(foreground, target, mid) + if _contrast_ratio(candidate, background) >= minimum: + hi = mid + else: + lo = mid + return _mix(foreground, target, hi) + +def _surface_step(value, background, amount): + return _lighten(value, amount) if _luminance(background) < 0.5 else _darken(value, amount) + def derive_theme(base): c = dict(BASE_THEME) if isinstance(base, dict): for key in BASE_THEME: value = base.get(key) - if isinstance(value, str) and re.fullmatch(r"#[0-9a-fA-F]{6}", value.strip()): c[key] = value.strip() - bg, surface = c["background"], c["surface"] - text, secondary = c["text"], c["text_secondary"] - primary, accent = c["primary"], c["accent"] - success, warning, danger = c["success"], c["warning"], c["danger"] + if isinstance(value, str) and re.fullmatch(r"#[0-9a-fA-F]{6}", value.strip()): + c[key] = value.strip() + + bg = c["background"] + surface = c["surface"] + # User-selected text colors are kept as close as possible to the chosen + # color, but are automatically corrected when they would become unreadable. + text = _ensure_contrast(c["text"], bg, 4.5) + secondary = _ensure_contrast(c["text_secondary"], bg, 4.5) + surface_alt = _surface_step(surface, bg, 0.14) + control = _surface_step(surface, bg, 0.10) + control_hover = _surface_step(surface, bg, 0.20) + primary = c["primary"] + accent = c["accent"] + primary_hover = _surface_step(primary, bg, 0.14) + success = c["success"] + warning = c["warning"] + danger = c["danger"] + return { - "background": bg, "surface": surface, "surface_alt": _lighten(surface,.12), "surface_dark": _darken(bg,.22), - "text": text, "text_secondary": secondary, "text_muted": _mix(secondary,bg,.35), "text_faint": _mix(secondary,bg,.58), - "border": _lighten(surface,.28), "border_subtle": _lighten(surface,.16), "control": _lighten(surface,.12), "control_hover": _lighten(surface,.24), - "primary": primary, "primary_hover": _lighten(primary,.16), "accent": accent, - "success": _darken(success,.42), "success_text": _lighten(success,.68), "warning": _darken(warning,.48), - "danger": danger, "danger_soft": _darken(danger,.58), "danger_soft_hover": _darken(danger,.42), - "error": _darken(danger,.62), "error_text": _lighten(danger,.70), "recording": _darken(danger,.08), "recording_border": _darken(danger,.52), - "selection": "rgba(80,150,255,.16)", "shadow": "#000000", "scroll_thumb": _mix(secondary,bg,.35), - "scroll_thumb_hover": _mix(secondary,bg,.18), "scroll_thumb_active": secondary, "live_surface": _mix(danger,bg,.84), - "table_header": _lighten(surface,.08), "table_text": text, "footer": _mix(secondary,bg,.50), "white": "#ffffff", - "surface_modal": _lighten(surface,.05), "overlay": "rgba(0,0,0,.78)", + "background": bg, "surface": surface, "surface_alt": surface_alt, "surface_dark": _darken(bg,.22), + "text": text, "text_secondary": secondary, + "text_muted": _ensure_contrast(_mix(secondary,bg,.35), bg, 3.0), + "text_faint": _ensure_contrast(_mix(secondary,bg,.58), bg, 2.2), + "border": _surface_step(surface,bg,.28), "border_subtle": _surface_step(surface,bg,.16), + "control": control, "control_hover": control_hover, + "primary": primary, "primary_hover": primary_hover, "primary_text": _contrast_text(primary), + "accent": accent, + "success": success, "success_text": _contrast_text(success), + "warning": warning, "warning_text": _contrast_text(warning), + "danger": danger, "danger_soft": _mix(danger,bg,.82), "danger_soft_hover": _mix(danger,bg,.70), + "error": _mix(danger,bg,.72), "error_text": _contrast_text(_mix(danger,bg,.72)), + "recording": danger, "recording_border": _mix(danger,bg,.55), "recording_text": _contrast_text(danger), + "selection": _mix(primary,bg,.84), "shadow": "#000000", + "scroll_thumb": _mix(secondary,bg,.35), "scroll_thumb_hover": _mix(secondary,bg,.18), + "scroll_thumb_active": secondary, "live_surface": _mix(danger,bg,.84), + "table_header": _surface_step(surface,bg,.08), "table_text": _ensure_contrast(text,surface,4.5), + "footer": _ensure_contrast(_mix(secondary,bg,.50), bg, 2.2), "white": "#ffffff", + "surface_modal": _surface_step(surface,bg,.05), "overlay": "rgba(0,0,0,.78)", } DEFAULT_THEME = derive_theme(BASE_THEME) @@ -717,7 +770,9 @@ HTML = """ .site-header { display:flex; align-items:center; gap:18px; margin-bottom:24px; } .site-logo { max-width:140px; max-height:48px; object-fit:contain; display:block; flex:0 0 auto; } -.site-header h1 { margin:0; } +.site-header h1 { margin:0; flex:1; } +.header-link { color:var(--color-primary); text-decoration:none; font-weight:700; padding:8px 12px; border:1px solid var(--color-border); border-radius:6px; } +.header-link:hover { background:var(--color-control-hover); color:var(--color-text); } body { margin: 0; @@ -747,7 +802,7 @@ h1 { .status.recording { background: var(--color-danger); - color:var(--color-white); + color:var(--color-recording-text); } .status.idle { @@ -757,7 +812,7 @@ h1 { .status.error { background: var(--color-warning); - color:var(--color-white); + color:var(--color-warning-text); } .recording { @@ -966,6 +1021,7 @@ a.button:hover {