diff --git a/src/prompt_toolkit/styles/style_transformation.py b/src/prompt_toolkit/styles/style_transformation.py index 7db723c39..dbf5f908d 100644 --- a/src/prompt_toolkit/styles/style_transformation.py +++ b/src/prompt_toolkit/styles/style_transformation.py @@ -173,7 +173,7 @@ def transform_attrs(self, attrs: Attrs) -> Attrs: # If a foreground color is given without a background color. no_background = not attrs.bgcolor or attrs.bgcolor == "default" - has_fgcolor = attrs.color and attrs.color != "ansidefault" + has_fgcolor = attrs.color and attrs.color not in ("default", "ansidefault") if has_fgcolor and no_background: # Calculate new RGB values. diff --git a/tests/test_style_transformation.py b/tests/test_style_transformation.py index 0651e7fa8..888ea5dd1 100644 --- a/tests/test_style_transformation.py +++ b/tests/test_style_transformation.py @@ -2,7 +2,7 @@ import pytest -from prompt_toolkit.styles import AdjustBrightnessStyleTransformation, Attrs +from prompt_toolkit.styles import AdjustBrightnessStyleTransformation, Attrs, Style @pytest.fixture @@ -50,3 +50,24 @@ def test_adjust_brightness_style_transformation(default_attrs): attrs = tr2.transform_attrs(default_attrs._replace(color="00ffaa")) assert attrs.color == "00ffaa" + + +@pytest.mark.parametrize("color", [None, "", "default", "ansidefault"]) +@pytest.mark.parametrize("brightness", [(0.5, 1.0), (0.0, 0.5)]) +def test_adjust_brightness_preserves_default_foreground( + default_attrs, color, brightness +): + attrs = default_attrs._replace(color=color, bold=True) + tr = AdjustBrightnessStyleTransformation(*brightness) + + assert tr.transform_attrs(attrs) == attrs + + +@pytest.mark.parametrize("style_string", ["fg:default", "fg:default bg:default"]) +def test_adjust_brightness_preserves_explicit_default_style(style_string): + style = Style.from_dict({"text": style_string}) + attrs = style.get_attrs_for_style_str("class:text") + tr = AdjustBrightnessStyleTransformation(0.5, 1.0) + + assert attrs.color == "default" + assert tr.transform_attrs(attrs) == attrs