Palette re-derives color roles from RGB, so overriding one role recolors unrelated roles and backgrounds
Priority: high (correctness: /colors <role> <hex> changes roles the user did not name, including backgrounds)
Area: crates/jcode-tui-style/src/palette.rs, crates/jcode-tui-style/src/theme_mode.rs, crates/jcode-tui-style/src/theme.rs
Version: jcode v0.86.0 (e589cbe5a)
Root cause of: #1391 (reasoning cannot be recolored without touching every dim element)
TL;DR (plain language)
jcode has no idea which role a pixel came from. It decides by nearest gray.
Every semantic color is emitted as a raw RGB value, and later a per-frame pass
guesses which role that RGB "belongs to" by finding the closest role default
within a fixed distance. So /colors dim red does not mean "recolor dim". It
means "recolor every color within ~0.16 Oklab of dim's default", which today
includes tool, border, the raw markdown-dim literal, and even the user_bg
and selection_bg backgrounds.
Evidence (measured, not inferred)
Running the real functions (Palette::set + remap_literal_with) on the
default palette, then overriding exactly one role:
only dim overridden to red (255,0,0):
tool (120,120,120) -> (253,146,130)
border (100,100,110) -> (253,105,97)
user_bg ( 35, 40, 50) -> (174, 0, 24) <- background
selection_bg ( 60, 60, 80) -> (213, 29, 55) <- background
raw md_dim (100,100,100) -> (252,104, 87)
only tool overridden to green (0,255,0):
dim (80,80,80) -> (43,199,39)
pending (140,140,140) -> (187,254,181)
border (100,100,110) -> (50,227,72)
only border overridden to blue (0,0,255):
tool (120,120,120) -> (5,81,246)
dim (80,80,80) -> (0,46,167)
pending (140,140,140) -> (49,112,248)
selection_bg (60,60,80) -> (4,12,147)Note user_bg and selection_bg are backgrounds and are graded by separate
harmony rules; a foreground override still drags them.
Root cause
Role identity is modeled, then discarded, then re-inferred.
- The neutral render model tags every span with a semantic role
(
crates/jcode-render-core/src/model.rs,StyleRole). - The TUI resolves that role to a concrete RGB at style construction
(
palette::role_color, thetheme.rsaccessors) and drops the role. - A per-frame pass re-guesses the role from the RGB:
theme_mode::adapt_buffer_for_display→palette::configured_native_color→palette::remap_literal_using, matching the nearest overridden role withinFAMILY_RADIUS = 0.16(palette.rs:481).
Two consequences fall out of step 3:
- Only overridden roles are candidates. So overriding one gray role makes it claim every gray within radius, because the roles that would compete for those colors are not in the candidate set. Which role "owns" a gray therefore depends on which other roles the user configured - the same pixel changes meaning as the config changes.
- Backgrounds go through the same attribution (
adaptis applied tocell.fg,cell.bg, andcell.underline_color,theme_mode.rs:300), so text-role overrides repaint backgrounds.
The heuristic exists because ~856 raw rgb(...) literals across 55 files emit
values instead of roles; the frame pass was the retrofit that made "every color
configurable" true without editing them. The cost is that no role is truly
independent.
Secondary: md_dim_color() is (100,100,100) while Role::Dim is
(80,80,80). "dim" is two different colors and the markdown one has no role
owner at all; it is claimed by whichever gray role the user happens to override
(border when everything is overridden).
Desired invariant
A color's role is decided where the color is chosen, and is never re-derived from its RGB.
This is the whole fix. Everything below is how to get there without a big-bang rewrite.
Proposed direction
- Resolve role → configured color at the point of use (accessors return the override), so role output is authoritative and needs no later pass.
- Delete role attribution from the frame pass. The pass keeps only theme/light contrast repair. The proximity match is retired.
- Configurability then comes from adopting roles, not from guessing. Raw
literals stop being auto-configurable; migrate them to role accessors family
by family.
crates/jcode-tui-style/src/palette_literals.rsis already the complete audit list (227 distinct literals) and the existingcoverage::most_tui_literals_are_reachable_from_some_roletest is the tracker - it inverts from "every literal is claimed by some role" to "no raw literals remain".
First PR (targeted, this is the one to open)
Stop the bleed: make role output exact and immune to re-attribution.
- Accessors/
role_colorresolve to the configured palette color instead of the default (identity at the source). configured_native_colorshort-circuits: if a frame color already equals a configured role's output, return it unchanged. Role cells and backgrounds can no longer be dragged by another role's override.- Raw literals keep the legacy proximity path for now (unchanged behavior for them), but they can no longer capture role-resolved cells.
Result: /colors dim changes dim and nothing else that is role-resolved;
/colors tool, /colors border, and backgrounds become independent.
Scope of the win, precisely: this makes role-resolved colors independent. It
does not by itself fix #1391, because the markdown-dim literals render the
raw value (100,100,100) and would still be captured by any role whose default
is that value. Completing #1391 is step 3 restricted to one family: give the
md_dim_color() call sites a role owner so no gray literal is left for reasoning
to capture. That is a second, separately reviewable PR.
Known limits to state in the PR: raw literals still bleed into each other (and
still capture each other) until they are adopted into roles; the
md_dim_color/Role::Dim split is not resolved here.
Non-goals
- Not touching markdown rendering, the reasoning sentinel, or renderer duplication (separate root causes).
- Not swapping ratatui/pulldown-cmark. The identity loss is in our adapter, not the library.
Test
- Override
dimalone:role_color(Role::Tool),Role::Border,Role::UserBg,Role::SelectionBgare byte-identical to default. - Override
toolalone:dimis byte-identical. - Override any foreground role: backgrounds are byte-identical.
- No overrides: output byte-identical to the historical hard-coded look (existing default-palette tests).
Source: 1jehuang/jcode