mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
monochrome tests
This commit is contained in:
@@ -7,6 +7,7 @@ from rich.segment import Segment
|
|||||||
from rich.style import Style
|
from rich.style import Style
|
||||||
|
|
||||||
from ._border import get_box, render_row
|
from ._border import get_box, render_row
|
||||||
|
from ._filter import LineFilter
|
||||||
from ._opacity import _apply_opacity
|
from ._opacity import _apply_opacity
|
||||||
from ._segment_tools import line_crop, line_pad, line_trim
|
from ._segment_tools import line_crop, line_pad, line_trim
|
||||||
from ._types import Lines
|
from ._types import Lines
|
||||||
@@ -134,6 +135,7 @@ class StylesCache:
|
|||||||
content_size=widget.content_region.size,
|
content_size=widget.content_region.size,
|
||||||
padding=styles.padding,
|
padding=styles.padding,
|
||||||
crop=crop,
|
crop=crop,
|
||||||
|
filter=widget.app._filter,
|
||||||
)
|
)
|
||||||
if widget.auto_links:
|
if widget.auto_links:
|
||||||
_style_links = style_links
|
_style_links = style_links
|
||||||
@@ -163,6 +165,7 @@ class StylesCache:
|
|||||||
content_size: Size | None = None,
|
content_size: Size | None = None,
|
||||||
padding: Spacing | None = None,
|
padding: Spacing | None = None,
|
||||||
crop: Region | None = None,
|
crop: Region | None = None,
|
||||||
|
filter: LineFilter | None = None,
|
||||||
) -> Lines:
|
) -> Lines:
|
||||||
"""Render a widget content plus CSS styles.
|
"""Render a widget content plus CSS styles.
|
||||||
|
|
||||||
@@ -212,6 +215,8 @@ class StylesCache:
|
|||||||
self._cache[y] = line
|
self._cache[y] = line
|
||||||
else:
|
else:
|
||||||
line = self._cache[y]
|
line = self._cache[y]
|
||||||
|
if filter:
|
||||||
|
line = filter.filter(line)
|
||||||
add_line(line)
|
add_line(line)
|
||||||
self._dirty_lines.difference_update(crop.line_range)
|
self._dirty_lines.difference_update(crop.line_range)
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ from ._animator import Animator, DEFAULT_EASING, Animatable, EasingFunction
|
|||||||
from ._callback import invoke
|
from ._callback import invoke
|
||||||
from ._context import active_app
|
from ._context import active_app
|
||||||
from ._event_broker import NoHandler, extract_handler_actions
|
from ._event_broker import NoHandler, extract_handler_actions
|
||||||
|
from ._filter import LineFilter, Monochrome
|
||||||
from .binding import Bindings, NoBinding
|
from .binding import Bindings, NoBinding
|
||||||
from .css.query import NoMatchingNodesError
|
from .css.query import NoMatchingNodesError
|
||||||
from .css.stylesheet import Stylesheet
|
from .css.stylesheet import Stylesheet
|
||||||
@@ -163,12 +164,18 @@ class App(Generic[ReturnType], DOMNode):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.features: frozenset[FeatureFlag] = parse_features(os.getenv("TEXTUAL", ""))
|
self.features: frozenset[FeatureFlag] = parse_features(os.getenv("TEXTUAL", ""))
|
||||||
|
|
||||||
|
self._filter: LineFilter | None = None
|
||||||
|
environ = dict(os.environ)
|
||||||
|
no_color = environ.pop("NO_COLOR", None)
|
||||||
|
if no_color is not None:
|
||||||
|
self._filter = Monochrome()
|
||||||
self.console = Console(
|
self.console = Console(
|
||||||
file=(_NullFile() if self.is_headless else sys.__stdout__),
|
file=(_NullFile() if self.is_headless else sys.__stdout__),
|
||||||
markup=False,
|
markup=False,
|
||||||
highlight=False,
|
highlight=False,
|
||||||
emoji=False,
|
emoji=False,
|
||||||
legacy_windows=False,
|
legacy_windows=False,
|
||||||
|
_environ=environ,
|
||||||
)
|
)
|
||||||
self.error_console = Console(markup=False, stderr=True)
|
self.error_console = Console(markup=False, stderr=True)
|
||||||
self.driver_class = driver_class or self.get_driver_class()
|
self.driver_class = driver_class or self.get_driver_class()
|
||||||
|
|||||||
@@ -311,6 +311,17 @@ class Color(NamedTuple):
|
|||||||
r, g, b, a = self
|
r, g, b, a = self
|
||||||
return f"rgb({r},{g},{b})" if a == 1 else f"rgba({r},{g},{b},{a})"
|
return f"rgb({r},{g},{b})" if a == 1 else f"rgba({r},{g},{b},{a})"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def monochrome(self) -> Color:
|
||||||
|
"""Get a monochrome version of this color.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Color: A new monochrome color.
|
||||||
|
"""
|
||||||
|
r, g, b, a = self
|
||||||
|
gray = round(r * 0.2126 + g * 0.7152 + b * 0.0722)
|
||||||
|
return Color(gray, gray, gray, a)
|
||||||
|
|
||||||
def __rich_repr__(self) -> rich.repr.Result:
|
def __rich_repr__(self) -> rich.repr.Result:
|
||||||
r, g, b, a = self
|
r, g, b, a = self
|
||||||
yield r
|
yield r
|
||||||
|
|||||||
@@ -31,6 +31,13 @@ def test_css():
|
|||||||
assert Color(10, 20, 30, 0.5).css == "rgba(10,20,30,0.5)"
|
assert Color(10, 20, 30, 0.5).css == "rgba(10,20,30,0.5)"
|
||||||
|
|
||||||
|
|
||||||
|
def test_monochrome():
|
||||||
|
assert Color(10, 20, 30).monochrome == Color(19, 19, 19)
|
||||||
|
assert Color(10, 20, 30, 0.5).monochrome == Color(19, 19, 19, 0.5)
|
||||||
|
assert Color(255, 255, 255).monochrome == Color(255, 255, 255)
|
||||||
|
assert Color(0, 0, 0).monochrome == Color(0, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
def test_rgb():
|
def test_rgb():
|
||||||
assert Color(10, 20, 30, 0.55).rgb == (10, 20, 30)
|
assert Color(10, 20, 30, 0.55).rgb == (10, 20, 30)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user