feat(theme): add tone mapping and fix missing palette colors
This commit is contained in:
@@ -4567,6 +4567,27 @@ def render_theme_panel(app: App) -> None:
|
||||
app._flush_to_config()
|
||||
models.save_config(app.config)
|
||||
|
||||
imgui.separator()
|
||||
imgui.text("Tone Mapping (Per-Palette)")
|
||||
curr_p = theme.get_current_palette()
|
||||
|
||||
imgui.text("Brightness")
|
||||
ch_b, b = imgui.slider_float("##tm_b", theme.get_brightness(curr_p), 0.1, 2.0, "%.2f")
|
||||
if ch_b: theme.set_brightness(curr_p, b); app._flush_to_config(); models.save_config(app.config)
|
||||
|
||||
imgui.text("Contrast")
|
||||
ch_c, c = imgui.slider_float("##tm_c", theme.get_contrast(curr_p), 0.1, 2.0, "%.2f")
|
||||
if ch_c: theme.set_contrast(curr_p, c); app._flush_to_config(); models.save_config(app.config)
|
||||
|
||||
imgui.text("Gamma")
|
||||
ch_g, g = imgui.slider_float("##tm_g", theme.get_gamma(curr_p), 0.1, 3.0, "%.2f")
|
||||
if ch_g: theme.set_gamma(curr_p, g); app._flush_to_config(); models.save_config(app.config)
|
||||
|
||||
if imgui.button("Reset Tone Mapping"):
|
||||
theme.reset_tone_mapping(curr_p)
|
||||
app._flush_to_config()
|
||||
models.save_config(app.config)
|
||||
|
||||
imgui.end()
|
||||
if app.perf_profiling_enabled: app.perf_monitor.end_component("_render_theme_panel")
|
||||
|
||||
|
||||
+60
-13
@@ -72,6 +72,38 @@ _current_scale: float = 1.0
|
||||
_transparency: float = 1.0
|
||||
_child_transparency: float = 1.0
|
||||
|
||||
# Per-palette tone mapping: { "Palette Name": value }
|
||||
_brightness: dict[str, float] = {}
|
||||
_contrast: dict[str, float] = {}
|
||||
_gamma: dict[str, float] = {}
|
||||
|
||||
def _get_tm(d: dict[str, float], palette: str, default: float) -> float:
|
||||
return d.get(palette, default)
|
||||
|
||||
def get_brightness(palette: str) -> float: return _get_tm(_brightness, palette, 1.0)
|
||||
def get_contrast(palette: str) -> float: return _get_tm(_contrast, palette, 1.0)
|
||||
def get_gamma(palette: str) -> float: return _get_tm(_gamma, palette, 1.0)
|
||||
|
||||
def set_brightness(palette: str, val: float) -> None: _brightness[palette] = val; apply(palette)
|
||||
def set_contrast(palette: str, val: float) -> None: _contrast[palette] = val; apply(palette)
|
||||
def set_gamma(palette: str, val: float) -> None: _gamma[palette] = val; apply(palette)
|
||||
|
||||
def reset_tone_mapping(palette: str) -> None:
|
||||
for d in [_brightness, _contrast, _gamma]:
|
||||
if palette in d: del d[palette]
|
||||
apply(palette)
|
||||
|
||||
def _tone_map(rgb: tuple[float, float, float, float], palette: str) -> tuple[float, float, float, float]:
|
||||
b, c, g = get_brightness(palette), get_contrast(palette), get_gamma(palette)
|
||||
r, g_val, bl, a = rgb
|
||||
# 1. Brightness
|
||||
r *= b; g_val *= b; bl *= b
|
||||
# 2. Contrast
|
||||
r = (r - 0.5) * c + 0.5; g_val = (g_val - 0.5) * c + 0.5; bl = (bl - 0.5) * c + 0.5
|
||||
# 3. Gamma
|
||||
r = max(0, r)**(1.0/g); g_val = max(0, g_val)**(1.0/g); bl = max(0, bl)**(1.0/g)
|
||||
return (max(0.0, min(1.0, r)), max(0.0, min(1.0, g_val)), max(0.0, min(1.0, bl)), a)
|
||||
|
||||
_crt_filter = CRTFilter()
|
||||
_alert_pulsing = AlertPulsing()
|
||||
_status_flicker = StatusFlicker()
|
||||
@@ -129,7 +161,7 @@ def apply(palette_name: str) -> None:
|
||||
imgui.style_colors_dark()
|
||||
style = imgui.get_style()
|
||||
for col_enum, rgba in colours.items():
|
||||
style.set_color_(col_enum, imgui.ImVec4(*rgba))
|
||||
style.set_color_(col_enum, imgui.ImVec4(*_tone_map(rgba, palette_name)))
|
||||
elif palette_name in _TOML_PALETTES:
|
||||
colours = _TOML_COLOUR_CACHE.get(palette_name, {})
|
||||
if not colours:
|
||||
@@ -139,17 +171,15 @@ def apply(palette_name: str) -> None:
|
||||
imgui.style_colors_dark()
|
||||
style = imgui.get_style()
|
||||
for colenum, rgba in colours.items():
|
||||
style.set_color_(colenum, imgui.ImVec4(*rgba))
|
||||
style.set_color_(colenum, imgui.ImVec4(*_tone_map(rgba, palette_name)))
|
||||
elif hasattr(hello_imgui.ImGuiTheme_, palette_name):
|
||||
theme_enum = getattr(hello_imgui.ImGuiTheme_, palette_name)
|
||||
hello_imgui.apply_theme(theme_enum)
|
||||
# hello_imgui doesn't expose the underlying dict easily to tone-map after-the-fact
|
||||
# without re-reading every enum. For now, BUILTIN and TOML themes get full TM.
|
||||
else:
|
||||
# Fallback to Nord Dark if requested but not found, otherwise ImGui Dark
|
||||
if palette_name == "Nord Dark":
|
||||
# This should not happen since it's in _PALETTES, but for safety
|
||||
imgui.style_colors_dark()
|
||||
else:
|
||||
imgui.style_colors_dark()
|
||||
# Fallback
|
||||
imgui.style_colors_dark()
|
||||
|
||||
# 2. Apply our "Subtle Rounding" professional tweaks on top of ANY theme
|
||||
style = imgui.get_style()
|
||||
@@ -187,6 +217,11 @@ def apply(palette_name: str) -> None:
|
||||
style.anti_aliased_fill = True
|
||||
style.anti_aliased_lines_use_tex = True
|
||||
|
||||
# 3. Sync syntax palette and clear markdown render cache
|
||||
apply_syntax_palette(get_syntax_palette_for_theme(palette_name))
|
||||
import src.markdown_helper
|
||||
src.markdown_helper.get_renderer().clear_cache()
|
||||
|
||||
def set_scale(factor: float) -> None:
|
||||
"""Set the global font/UI scale factor."""
|
||||
global _current_scale
|
||||
@@ -204,16 +239,23 @@ def save_to_config(config: dict) -> None:
|
||||
config["theme"]["scale"] = _current_scale
|
||||
config["theme"]["transparency"] = _transparency
|
||||
config["theme"]["child_transparency"] = _child_transparency
|
||||
sys.stderr.write(f"[DEBUG theme_2] save_to_config: palette={_current_palette}, transparency={_transparency}\n")
|
||||
# Tone mapping
|
||||
tm = {}
|
||||
for p in set(list(_brightness.keys()) + list(_contrast.keys()) + list(_gamma.keys())):
|
||||
tm[p] = {
|
||||
"brightness": _brightness.get(p, 1.0),
|
||||
"contrast": _contrast.get(p, 1.0),
|
||||
"gamma": _gamma.get(p, 1.0)
|
||||
}
|
||||
config["theme"]["tone_mapping"] = tm
|
||||
sys.stderr.write(f"[DEBUG theme_2] save_to_config: palette={_current_palette}\n")
|
||||
sys.stderr.flush()
|
||||
|
||||
def load_from_config(config: dict) -> None:
|
||||
"""Read [theme] from config. Font is handled separately at startup."""
|
||||
import sys
|
||||
global _current_font_path, _current_font_size, _current_scale, _current_palette, _transparency, _child_transparency
|
||||
global _current_font_path, _current_font_size, _current_scale, _current_palette, _transparency, _child_transparency, _brightness, _contrast, _gamma
|
||||
t = config.get("theme", {})
|
||||
sys.stderr.write(f"[DEBUG theme_2] load_from_config raw: {t}\n")
|
||||
sys.stderr.flush()
|
||||
_current_palette = t.get("palette", "10x Dark")
|
||||
if _current_palette in ("", "DPG Default"):
|
||||
_current_palette = "10x Dark"
|
||||
@@ -223,7 +265,12 @@ def load_from_config(config: dict) -> None:
|
||||
_current_scale = float(t.get("scale", 1.0))
|
||||
_transparency = float(t.get("transparency", 1.0))
|
||||
_child_transparency = float(t.get("child_transparency", 1.0))
|
||||
sys.stderr.write(f"[DEBUG theme_2] load_from_config effective: palette={_current_palette}, transparency={_transparency}\n")
|
||||
# Tone mapping
|
||||
tm = t.get("tone_mapping", {})
|
||||
_brightness = {p: float(v.get("brightness", 1.0)) for p, v in tm.items()}
|
||||
_contrast = {p: float(v.get("contrast", 1.0)) for p, v in tm.items()}
|
||||
_gamma = {p: float(v.get("gamma", 1.0)) for p, v in tm.items()}
|
||||
sys.stderr.write(f"[DEBUG theme_2] load_from_config: palette={_current_palette}\n")
|
||||
sys.stderr.flush()
|
||||
|
||||
def apply_current() -> None:
|
||||
|
||||
+33
-3
@@ -9,19 +9,20 @@ from typing import Any
|
||||
VALID_SYNTAX_PALETTES: tuple[str, ...] = ("dark", "light", "mariana", "retro_blue")
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass
|
||||
class ThemePalette:
|
||||
window_bg: tuple[int, int, int] = (0, 0, 0)
|
||||
text: tuple[int, int, int] = (200, 200, 200)
|
||||
text_disabled: tuple[int, int, int] = (130, 130, 130)
|
||||
child_bg: tuple[int, int, int] = (0, 0, 0)
|
||||
popup_bg: tuple[int, int, int] = (0, 0, 0)
|
||||
border: tuple[int, int, int] = (60, 60, 60)
|
||||
border_shadow: tuple[int, int, int] = (0, 0, 0)
|
||||
frame_bg: tuple[int, int, int] = (45, 45, 45)
|
||||
frame_bg_hovered: tuple[int, int, int] = (60, 60, 60)
|
||||
frame_bg_active: tuple[int, int, int] = (75, 75, 75)
|
||||
title_bg: tuple[int, int, int] = (40, 40, 40)
|
||||
title_bg_active: tuple[int, int, int] = (60, 45, 15)
|
||||
title_bg_collapsed: tuple[int, int, int] = (30, 30, 30)
|
||||
menu_bar_bg: tuple[int, int, int] = (35, 35, 35)
|
||||
scrollbar_bg: tuple[int, int, int] = (30, 30, 30)
|
||||
scrollbar_grab: tuple[int, int, int] = (80, 80, 80)
|
||||
@@ -39,11 +40,40 @@ class ThemePalette:
|
||||
separator: tuple[int, int, int] = (60, 60, 60)
|
||||
separator_hovered: tuple[int, int, int] = (100, 100, 100)
|
||||
separator_active: tuple[int, int, int] = (200, 200, 200)
|
||||
resize_grip: tuple[int, int, int] = (60, 60, 60)
|
||||
resize_grip_hovered: tuple[int, int, int] = (100, 100, 100)
|
||||
resize_grip_active: tuple[int, int, int] = (200, 200, 200)
|
||||
tab: tuple[int, int, int] = (60, 60, 60)
|
||||
tab_hovered: tuple[int, int, int] = (100, 100, 100)
|
||||
tab_selected: tuple[int, int, int] = (100, 100, 100)
|
||||
tab_dimmed: tuple[int, int, int] = (60, 60, 60)
|
||||
tab_dimmed_selected: tuple[int, int, int] = (100, 100, 100)
|
||||
docking_preview: tuple[int, int, int] = (100, 100, 100)
|
||||
docking_empty_bg: tuple[int, int, int] = (20, 20, 20)
|
||||
text: tuple[int, int, int] = (200, 200, 200)
|
||||
text_disabled: tuple[int, int, int] = (130, 130, 130)
|
||||
text_selected_bg: tuple[int, int, int] = (60, 100, 150)
|
||||
table_header_bg: tuple[int, int, int] = (55, 55, 55)
|
||||
table_border_strong: tuple[int, int, int] = (60, 60, 60)
|
||||
table_border_light: tuple[int, int, int] = (40, 40, 40)
|
||||
table_row_bg: tuple[int, int, int] = (0, 0, 0)
|
||||
table_row_bg_alt: tuple[int, int, int] = (10, 10, 10)
|
||||
nav_cursor: tuple[int, int, int] = (100, 100, 100)
|
||||
nav_windowing_dim_bg: tuple[int, int, int] = (20, 20, 20)
|
||||
nav_windowing_highlight: tuple[int, int, int] = (200, 200, 200)
|
||||
modal_window_dim_bg: tuple[int, int, int] = (10, 10, 10)
|
||||
plot_lines: tuple[int, int, int] = (100, 100, 100)
|
||||
plot_lines_hovered: tuple[int, int, int] = (200, 100, 100)
|
||||
plot_histogram: tuple[int, int, int] = (100, 100, 100)
|
||||
plot_histogram_hovered: tuple[int, int, int] = (200, 100, 100)
|
||||
drag_drop_target: tuple[int, int, int] = (200, 200, 0)
|
||||
drag_drop_target_bg: tuple[int, int, int] = (0, 0, 0)
|
||||
input_text_cursor: tuple[int, int, int] = (200, 200, 200)
|
||||
tab_dimmed_selected_overline: tuple[int, int, int] = (100, 100, 100)
|
||||
tab_selected_overline: tuple[int, int, int] = (100, 100, 100)
|
||||
text_link: tuple[int, int, int] = (60, 100, 150)
|
||||
tree_lines: tuple[int, int, int] = (60, 60, 60)
|
||||
unsaved_marker: tuple[int, int, int] = (200, 200, 200)
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict[str, Any]) -> ThemePalette:
|
||||
@@ -122,7 +152,7 @@ def load_theme_file(path: Path, scope: str) -> ThemeFile:
|
||||
raise ValueError(f"failed to parse theme TOML {path}: {e}") from e
|
||||
if not isinstance(data, dict):
|
||||
raise ValueError(f"theme TOML {path} must be a top-level table")
|
||||
name = path.stem
|
||||
name = data.get("name", path.stem)
|
||||
theme = ThemeFile.from_dict(name, data, source_path=path, scope=scope)
|
||||
return theme
|
||||
|
||||
|
||||
Reference in New Issue
Block a user