# Theme Polish & Tone Mapping

## Problem

1. **Missing Theme Colors**: The `ThemePalette` dataclass in `src/theme_models.py` only defined a subset of the ~55 ImGui colors. Because `from_dict` strictly matched dataclass fields, colors like `resize_grip` and `tab_dimmed` from the TOML files were being discarded, breaking window resizing handles and inactive tab styling.
2. **Context Preview Syntax Palette**: `theme_2.apply()` failed to apply the syntax palette for non-NERV themes, and `src/markdown_helper.py` cached its `TextEditor` instances without clearing them on theme switch. This caused "Context Preview" to remain stuck on the previous theme's syntax colors.
3. **Light Theme Brightness**: The user requested a way to dim light themes. We will introduce a Tone Mapping system (Brightness, Contrast, Gamma) that mathematical adjusts the RGB colors before applying them to ImGui. The user requested this to be saved per-palette so each theme can have its own exposure profile.

## Proposed Solution

### 1. Fix Theme Models
- Ensure `src/theme_models.py`'s `ThemePalette` dataclass has all missing ImGui colors (e.g., `resize_grip`, `resize_grip_active`, `resize_grip_hovered`, `tab_dimmed`, `tab_dimmed_selected`, `docking_preview`, `plot_lines`, `nav_windowing_highlight`, etc.). *(Note: I proactively applied the class definition update during exploration, but will formally commit it)*.

### 2. Fix Context Preview Syntax Highlight Sync
- Update `src/theme_2.py` to ensure `apply_syntax_palette()` is called for *all* themes during `apply()`.
- Add an `import src.markdown_helper; src.markdown_helper.get_renderer().clear_cache()` call to the end of `theme_2.apply()` to force code blocks to recreate their `TextEditor` instances with the new palette.

### 3. Per-Palette Tone Mapping
- Add mathematical tone mapping variables to `src/theme_2.py`: `_brightness`, `_contrast`, and `_gamma` (stored as dictionaries keyed by the palette name to allow per-palette saving).
- Implement a math function to adjust RGB floats:
  - Brightness: `c * brightness`
  - Contrast: `(c - 0.5) * contrast + 0.5`
  - Gamma: `pow(c, 1.0 / gamma)`
- Update the palette application loop in `theme_2.apply()` to pass every color float through this tone mapper before calling `style.set_color_()`.
- Update `save_to_config` and `load_from_config` to persist the tone mapping overrides per-palette under `[theme.tone_mapping.<palette>]`.
- Add Brightness, Contrast, and Gamma sliders to the Theme panel in `src/gui_2.py`.

## Implementation Steps
1. **Model & Sync Fixes**: Verify `src/theme_models.py` and update `src/theme_2.py`'s `apply()` function to trigger syntax updates and markdown cache clearing.
2. **Tone Mapping Logic**: Add the dicts and the math `_tone_map(rgb, palette)` function to `theme_2.py`, wrapping all color assignments.
3. **State Persistence**: Update `save_to_config` / `load_from_config` to handle the new per-palette dictionary.
4. **UI Integration**: Add the 3 sliders to `_render_theme_panel` in `src/gui_2.py`, complete with a "Reset to Defaults" button for the current palette.
5. **Testing**: Run the existing test suite and verify no regressions in config saving.