ANSI themes: text-opacity rasterizes ANSI colors to the Monokai fallback (HeaderTitle turns neon on blur)
Have you checked closed issues? (https://github.com/Textualize/textual/issues?q=is%3Aissue+is%3Aclosed) Yes
Have you checked against the most recent version of Textual? (https://pypi.org/search/?q=textual) Yes
The bug
Under an ANSI theme (ansi-dark / ansi-light), applying text-opacity < 100% to an ANSI-colored region renders it with hardcoded RGB from Rich's Monokai DEFAULT_TERMINAL_THEME instead of the terminal's own ANSI palette.
This happens out of the box with the built-in HeaderTitle. App.DEFAULT_CSS fades it on blur:
&:blur HeaderTitle {
text-opacity: 50%;
}
So a header with background: ansi_green renders in the terminal's green while focused, but the moment the window loses focus (AppBlur), the title flips to a fixed #98e024 (neon lime) that ignores the terminal and matches no other ansi_green element in the app. text-opacity is a blend against the background, which needs concrete RGB; ANSI colors are symbolic, so they get resolved through Monokai (green = #98e024) to do the math.
Complete working example (runs without modification, no terminal or interaction needed, it drives AppBlur itself and prints the rendered styles):
import asyncio
from textual import events
from textual.app import App, ComposeResult
from textual.widgets import Header
class Repro(App):
CSS = "Header { background: ansi_green; color: ansi_black; }"
def on_mount(self) -> None:
self.theme = "ansi-dark"
def compose(self) -> ComposeResult:
yield Header()
def title_style(app):
for segment in app.screen._compositor.render_strips()[0]:
if segment.text.strip() and segment.text.strip() != "⭘":
return segment.style
async def main():
app = Repro()
async with app.run_test() as pilot:
await pilot.pause()
print("focused:", title_style(app))
app.post_message(events.AppBlur())
await pilot.pause()
print("blurred:", title_style(app))
asyncio.run(main())
Output:
focused: color(0) on color(2)
blurred: #597d1f on #98e024
Focused, the title is symbolic ANSI (color(0) on color(2)) so the terminal picks the shade. Blurred, it is rasterized to Monokai RGB (#98e024).
Expected: under an ANSI theme the title stays in the terminal's palette on blur (either full ANSI green, or a genuinely dimmed ANSI color) rather than switching to a fixed off-palette RGB.
Actual: it renders as #98e024, Rich's Monokai green.
This is not header-specific, any text-opacity / opacity blend on an ANSI-colored widget under an ANSI theme hits it. The header is just the first place users see it, because the default blur rule triggers it on every focus change.
Workaround, for anyone who lands here, re-pin the title to full opacity:
Screen:blur HeaderTitle {
text-opacity: 100%;
}
textual diagnose
Textual Diagnostics
Versions
| Name | Value |
|---|---|
| Textual | 8.2.8 |
| Rich | 15.0.0 |
Python
| Name | Value |
|---|---|
| Version | 3.14.6 |
| Implementation | CPython |
| Compiler | GCC 16.1.1 20260430 |
Operating System
| Name | Value |
|---|---|
| System | Linux |
| Release | 7.1.2-arch3-1 |
Terminal
| Name | Value |
|---|---|
| Terminal Application | Kitty |
| TERM | xterm-kitty |
| COLORTERM | truecolor |
We found the following entries in the FAQ which you may find helpful:
Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.
This project is developed and maintained by Will McGugan. Consider sponsoring Will's work on this project (and others).
This is an automated reply, generated by FAQtory
I can reproduce the underlying behavior on current main directly in TextOpacity.process_segments: with native_ansi=True, color(0) on color(2) becomes Monokai RGB whenever opacity is partial. The existing native-ANSI snapshot only exercises the default terminal background, so it doesn't cover this case.
I'd like to work on this. My proposed minimal fix is to preserve the original symbolic style whenever either foreground or background is terminal-defined ANSI (default / standard colors) in native-ANSI mode, applying Rich's dim attribute at opacity <= 50% as the existing fallback does. Concrete RGB pairs would keep the current truecolor blend. I'd add focused tests for an ANSI foreground/background pair at both sides of the threshold.
@willmcgugan, would that behavior be acceptable?
Root cause
TextOpacity.process_segments's native-ANSI branch (src/textual/renderables/text_opacity.py) only preserves the original ANSI style when style.bgcolor.is_default is true:
if (
style is not None
and style.bgcolor is not None
and style.bgcolor.is_default
):
# dim + preserve original style
ansi_green is a real rich.color.ColorType.STANDARD color, not ColorType.DEFAULT, so this check fails and the segment falls through to the generic blending path, which resolves the ANSI color to RGB via the guessed ansi_theme (Monokai by default) and blends against that synthetic value — producing the fixed #98e024 reported here.
Fix
Broaden the check from is_default to type != ColorType.TRUECOLOR, since any non-truecolor background (default, 16-color standard, or 8-bit) has no reliable real RGB to blend against and should stay native + dimmed instead of being rasterized:
--- a/src/textual/renderables/text_opacity.py
+++ b/src/textual/renderables/text_opacity.py
@@ -2,7 +2,7 @@ import functools
from typing import Iterable, Tuple, cast
from rich.cells import cell_len
-from rich.color import Color
+from rich.color import Color, ColorType
from rich.console import Console, ConsoleOptions, RenderableType, RenderResult
from rich.segment import Segment
from rich.style import Style
@@ -98,7 +98,7 @@ class TextOpacity:
if (
style is not None
and style.bgcolor is not None
- and style.bgcolor.is_default
+ and style.bgcolor.type != ColorType.TRUECOLOR
):
# If the opacity is less than or equal to 50%, then set the dim attribute
if style.color is not None and opacity <= 0.5:
Verification
Reproduced the exact reported symptom directly against process_segments:
bg = RichColor.from_ansi(2) # ansi_green
fg = RichColor.from_ansi(7)
style = Style.from_color(color=fg, bgcolor=bg)
segments = [Segment("Header Title", style)]
list(TextOpacity.process_segments(segments, opacity=0.5, ansi_theme=DEFAULT_TERMINAL_THEME, native_ansi=True))
- Before fix:
Header Titlerenders as#aed26c on #98e024— reproduces the exact "neon lime" (#98e024) reported above. - After fix: renders as
dim color(7) on color(2)— native ANSI colors preserved, dimmed instead of rasterized.
Added a regression test (test_native_ansi_opacity_preserves_ansi_background) covering this directly against process_segments. Full pytest tests/renderables/ suite: 41/41 passed.
I have this branched and ready on my fork (shlokkvaishnav:fix/ansi-text-opacity-rasterization) but PR creation to this repo currently appears restricted to collaborators — happy to open a formal PR if that's re-enabled, or a maintainer/collaborator is welcome to cherry-pick this directly.