Skip to content

Filter

Filter classes.

Note

Filters are used internally, and not recommended for use by Textual app developers.

Filters are used internally to process terminal output after it has been rendered. Currently this is used internally to convert the application to monochrome, when the NO_COLOR env var is set.

In the future, this system will be used to implement accessibility features.

NO_DIM module-attribute

NO_DIM = Style(dim=False)

A Style to set dim to False.

ANSIToTruecolor class

def __init__(self, terminal_theme):

Bases: LineFilter

Convert ANSI colors to their truecolor equivalents.

Parameters
Parameter Default Description
terminal_theme
TerminalTheme
required

A rich terminal theme.

apply method

def apply(self, segments, background):

Transform a list of segments.

Parameters
Parameter Default Description
segments
list[Segment]
required

A list of segments.

background
Color
required

The background color.

Returns
Type Description
list[Segment]

A new list of segments.

truecolor_style cached

def truecolor_style(self, style):

Replace system colors with truecolor equivalent.

Parameters
Parameter Default Description
style
Style
required

Style to apply truecolor filter to.

Returns
Type Description
Style

New style.

DimFilter class

def __init__(self, dim_factor=0.5):

Bases: LineFilter

Replace dim attributes with modified colors.

Parameters
Parameter Default Description
dim_factor
float
0.5

The factor to dim by; 0 is 100% background (i.e. invisible), 1.0 is no change.

apply method

def apply(self, segments, background):

Transform a list of segments.

Parameters
Parameter Default Description
segments
list[Segment]
required

A list of segments.

background
Color
required

The background color.

Returns
Type Description
list[Segment]

A new list of segments.

LineFilter class

Bases: ABC

Base class for a line filter.

apply abstractmethod

def apply(self, segments, background):

Transform a list of segments.

Parameters
Parameter Default Description
segments
list[Segment]
required

A list of segments.

background
Color
required

The background color.

Returns
Type Description
list[Segment]

A new list of segments.

Monochrome class

Bases: LineFilter

Convert all colors to monochrome.

apply method

def apply(self, segments, background):

Transform a list of segments.

Parameters
Parameter Default Description
segments
list[Segment]
required

A list of segments.

background
Color
required

The background color.

Returns
Type Description
list[Segment]

A new list of segments.

dim_color cached

def dim_color(background, color, factor):

Dim a color by blending towards the background

Parameters
Parameter Default Description
background
RichColor
required

background color.

color
RichColor
required

Foreground color.

factor
float
required

Blend factor

Returns
Type Description
RichColor

New dimmer color.

dim_style cached

def dim_style(style, background, factor):

Replace dim attribute with a dim color.

Parameters
Parameter Default Description
style
Style
required

Style to dim.

factor
float
required

Blend factor.

Returns
Type Description
Style

New dimmed style.

monochrome_style cached

def monochrome_style(style):

Convert colors in a style to monochrome.

Parameters
Parameter Default Description
style
Style
required

A Rich Style.

Returns
Type Description
Style

A new Rich style.