ColorPair
Module: terminaltexteffects.utils.graphics
Basic Usage
ColorPair objects are used to represent a foreground and background color pair.
Usage
import terminaltexteffects as tte
color_pair = tte.ColorPair(fg=tte.Color("FF0000"), bg=tte.Color("00FF00"))
Alternate Signature
Colors can be specified using strings or integers. Color objects will be created automatically.
import terminaltexteffects as tte
color_pair = tte.ColorPair(fg="FF0000", bg="00FF00")
fg
and/or bg
are optional and default to None
.
Printing ColorPairs
ColorPair objects can be printed to see the resulting colors.

ColorPair Reference
Represents a pair of colors to specify a character's foreground and background colors.
On init, if fg or bg is not a Color object, create a Color object with the value.
Attributes:
Name |
Type |
Description |
fg_color |
Color | None
|
The foreground color. None if no foreground color is specified.
|
bg_color |
Color | None
|
The background color. None if no background color is specified.
|
fg |
InitVar[Color | str | int | None]
|
The initial foreground color value.
|
bg |
InitVar[Color | str | int | None]
|
The initial background color value.
|
Source code in terminaltexteffects/utils/graphics.py
| @dataclass()
class ColorPair:
"""Represents a pair of colors to specify a character's foreground and background colors.
On init, if fg or bg is not a Color object, create a Color object with the value.
Attributes:
fg_color (Color | None): The foreground color. None if no foreground color is specified.
bg_color (Color | None): The background color. None if no background color is specified.
fg (InitVar[Color | str | int | None]): The initial foreground color value.
bg (InitVar[Color | str | int | None]): The initial background color value.
"""
fg_color: Color | None = field(init=False, default=None)
bg_color: Color | None = field(init=False, default=None)
fg: InitVar[Color | str | int | None] = None
bg: InitVar[Color | str | int | None] = None
def __post_init__(self, init_fg_color: Color | str | int | None, init_bg_color: Color | str | int | None) -> None:
"""If either fg or bg is not a Color object, create a Color object with the value."""
if init_fg_color is not None and not isinstance(init_fg_color, Color):
self.fg_color = Color(init_fg_color)
else:
self.fg_color = init_fg_color
if init_bg_color is not None and not isinstance(init_bg_color, Color):
self.bg_color = Color(init_bg_color)
else:
self.bg_color = init_bg_color
def __str__(self) -> str:
"""Return a string representation of the ColorPair object."""
color_block = (
f"{colorterm.fg(self.fg_color.rgb_color) if self.fg_color else ''}"
f"{colorterm.bg(self.bg_color.rgb_color) if self.bg_color else ''}####{ansitools.reset_all()}"
)
return (
f"Foreground Color Code: {self.fg_color.rgb_color if self.fg_color else ''}"
f"{f' | Foreground XTerm Color: {self.fg_color.xterm_color}' if self.fg_color and self.fg_color.xterm_color else ''}\n"
f"Background Color Code: {self.bg_color.rgb_color if self.bg_color else ''}"
f"{f' | Background XTerm Color: {self.bg_color.xterm_color}' if self.bg_color and self.bg_color.xterm_color else ''}"
f"\nColor Appearance: {color_block}"
)
|
__post_init__(init_fg_color, init_bg_color)
If either fg or bg is not a Color object, create a Color object with the value.
Source code in terminaltexteffects/utils/graphics.py
| def __post_init__(self, init_fg_color: Color | str | int | None, init_bg_color: Color | str | int | None) -> None:
"""If either fg or bg is not a Color object, create a Color object with the value."""
if init_fg_color is not None and not isinstance(init_fg_color, Color):
self.fg_color = Color(init_fg_color)
else:
self.fg_color = init_fg_color
if init_bg_color is not None and not isinstance(init_bg_color, Color):
self.bg_color = Color(init_bg_color)
else:
self.bg_color = init_bg_color
|
__str__()
Return a string representation of the ColorPair object.
Source code in terminaltexteffects/utils/graphics.py
| def __str__(self) -> str:
"""Return a string representation of the ColorPair object."""
color_block = (
f"{colorterm.fg(self.fg_color.rgb_color) if self.fg_color else ''}"
f"{colorterm.bg(self.bg_color.rgb_color) if self.bg_color else ''}####{ansitools.reset_all()}"
)
return (
f"Foreground Color Code: {self.fg_color.rgb_color if self.fg_color else ''}"
f"{f' | Foreground XTerm Color: {self.fg_color.xterm_color}' if self.fg_color and self.fg_color.xterm_color else ''}\n"
f"Background Color Code: {self.bg_color.rgb_color if self.bg_color else ''}"
f"{f' | Background XTerm Color: {self.bg_color.xterm_color}' if self.bg_color and self.bg_color.xterm_color else ''}"
f"\nColor Appearance: {color_block}"
)
|