xnano.styles
xnano.styles
¶
xnano.styles
Colors, styles and styled text.
A Style holds colors and text modifiers; styled text is built from spans
and lines that each carry their own style.
Classes:
-
Color–A terminal color: a named palette color, a palette index or a true color.
-
Style–A foreground color, a background color and text modifiers.
-
StyledText–Text made of differently styled spans.
-
TextLine–One wrapped line of a
TextLayout. -
TextSpan–A run of text with one style.
Attributes:
-
ColorName(TypeAlias) –A named ANSI terminal color.
-
ColorValue(TypeAlias) –A color, an ANSI color name, a
'#rrggbb'string, or a(red, green, blue)tuple. -
TextContent(TypeAlias) –Text accepted by writing operations and components.
ColorName
module-attribute
¶
ColorName: TypeAlias = Literal['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'gray', 'grey', 'dark_gray', 'dark_grey', 'darkgray', 'darkgrey', 'light_black', 'light_red', 'bright_red', 'light_green', 'bright_green', 'light_yellow', 'bright_yellow', 'light_blue', 'bright_blue', 'light_magenta', 'bright_magenta', 'light_cyan', 'bright_cyan', 'white', 'reset']
A named ANSI terminal color.
gray and grey are the same color, and so are light_ and bright_.
dark_gray is the dim gray. reset restores the terminal default.
ColorValue
module-attribute
¶
ColorValue: TypeAlias = 'Color | ColorName | str | tuple[int, int, int]'
A color, an ANSI color name, a '#rrggbb' string, or a (red, green, blue) tuple.
A Tailwind name such as 'slate-400' is a TailwindColorBinding.
It is not parsed by Color.
- API Reference
-
API Reference
-
xnano
-
xnano -
xnano.appearance
appearance -
xnano.art
art -
xnano.buffers
buffers -
xnano.canvas
canvasCanvaswrite -
xnano.effects
effectsFade -
xnano.operations
operations -
xnano.pixel
pixelPixel -
xnano.placement
placementPlacementwrite -
xnano.region
regionRegionshade -
xnano.styles
stylesStyle -
xnano.terminal
terminalTerminalwrite -
xnano.widgets
widgets
-
-
xnano_core
-
xnano_core -
xnano_core._xnano_core
_xnano_core
-
-
xnano
TextContent
module-attribute
¶
TextContent: TypeAlias = 'str | StyledText | Sequence[str | StyledText]'
Text accepted by writing operations and components.
-
API Reference
- xnano
-
xnano_core
-
xnano_core -
xnano_core._xnano_core
_xnano_core
-
Color
¶
A terminal color: a named palette color, a palette index or a true color.
Maps to ratatui::style::Color.
Parameters:
-
value(ColorName | str | tuple[int, int, int]) –A color name such as
'red'or'light_blue', a hex string such as'#ff8800', a palette index string such as'4', or a(red, green, blue)tuple.
Attributes:
-
black(Color) –The black terminal color.
-
red(Color) –The red terminal color.
-
green(Color) –The green terminal color.
-
yellow(Color) –The yellow terminal color.
-
blue(Color) –The blue terminal color.
-
magenta(Color) –The magenta terminal color.
-
cyan(Color) –The cyan terminal color.
-
gray(Color) –The gray terminal color.
-
dark_gray(Color) –The dark gray terminal color.
-
light_red(Color) –The light red terminal color.
-
light_green(Color) –The light green terminal color.
-
light_yellow(Color) –The light yellow terminal color.
-
light_blue(Color) –The light blue terminal color.
-
light_magenta(Color) –The light magenta terminal color.
-
light_cyan(Color) –The light cyan terminal color.
-
white(Color) –The white terminal color.
-
reset(Color) –The reset terminal color, which follows the terminal theme.
-
is_reset(bool) –Whether this is the
resetcolor.
Raises:
-
ValueError–valueis not a recognized color.
Examples:
assert Color('#ff8800').to_rgb() == (255, 136, 0)
assert Color((1, 2, 3)) == Color.from_rgb(1, 2, 3)
assert Color('red').to_rgb() is None
Create a Color.
Parameters:
-
API Reference
-
xnano
-
xnano -
xnano.appearance
appearance -
xnano.buffers
buffers -
xnano.canvas
canvasCanvascolor -
xnano.operations
operations -
xnano.placement
placementPlacementcolor -
xnano.region
regionRegion__setitem__ -
xnano.styles
stylesColorValue -
xnano.terminal
terminalTerminalcolor -
xnano.types
typesColorValue -
xnano.widgets
widgets
-
-
xnano_core
-
xnano_core -
xnano_core._xnano_core
_xnano_core -
xnano_core._xnano_core._core_types
_core_types
-
-
xnano
-
API Reference
- xnano
-
xnano_core
-
xnano_core -
xnano_core._xnano_core
_xnano_core
-
Methods:
-
parse–Parse a color name, hex string or palette index string.
-
from_tailwind–A true color from a Tailwind palette and shade.
-
indexed–A color from the 256-color palette.
-
from_rgb–A true color from red, green and blue values between 0 and 255.
-
to_rgb–The
(red, green, blue)values of a true color, orNonefor named and indexed colors. -
to_hex–The color as a
'#rrggbb'string. -
from_hsl–A true color from hue, saturation and lightness.
-
from_hsv–A true color from hue, saturation and value.
-
random–A random true color, or a random shade of
base. -
to_hsl–The hue (degrees), saturation and lightness (percentages) of the color.
-
to_hsv–The hue (degrees), saturation and value (percentages) of the color.
-
lerp–The color between this one and
other. -
lighten–A lighter color for a positive
amount, a darker one for a negativeamount. -
darken–A darker color.
-
inverse–The color that has each red, green and blue channel inverted.
-
saturate–The color with its saturation scaled.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
is_reset
property
¶
is_reset: bool
Whether this is the reset color, which follows the terminal's theme.
parse
classmethod
¶
Parse a color name, hex string or palette index string.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
from_tailwind
classmethod
¶
from_tailwind(palette: TailwindColorName, shade: TailwindColorShade) -> Color
A true color from a Tailwind palette and shade.
Color does not read a name such as 'slate-400'. Pass the palette and the
shade here.
Parameters:
-
palette(TailwindColorName) –A palette such as
'slate'or'red'. -
shade(TailwindColorShade) –50,100through900, or950.
Raises:
-
ValueError–paletteis not a palette, orshadeis not a shade.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
indexed
classmethod
¶
A color from the 256-color palette.
Examples:
from_rgb
classmethod
¶
A true color from red, green and blue values between 0 and 255.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
to_rgb
¶
The (red, green, blue) values of a true color, or None for named and indexed colors.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
to_hex
¶
to_hex() -> str
The color as a '#rrggbb' string.
Only a true color has one, as with to_rgb.
Raises:
-
ValueError–The color is named, indexed or
reset, which the terminal's theme chooses.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
from_hsl
classmethod
¶
A true color from hue, saturation and lightness.
Parameters:
-
hue(float) –Degrees from 0 to 360.
-
saturation(float) –A percentage from 0 to 100.
-
lightness(float) –A percentage from 0 to 100.
Raises:
-
ValueError–A value is out of range.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
from_hsv
classmethod
¶
A true color from hue, saturation and value.
Parameters:
-
hue(float) –Degrees from 0 to 360.
-
saturation(float) –A percentage from 0 to 100.
-
value(float) –A percentage from 0 to 100.
Raises:
-
ValueError–A value is out of range.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
random
classmethod
¶
A random true color, or a random shade of base.
Without base every red, green and blue channel is picked at random. With base
the color keeps its hue and saturation and takes a random lightness from 15 to 85
percent, so every color it returns is a lighter or darker shade of base. The
colors come from Python's random
module, so random.seed(...) repeats them.
Parameters:
-
base(Color | ColorName | str | tuple[int, int, int] | None, default:None) –The color to pick shades of, or
Nonefor any color.
Raises:
-
ValueError–baseisColor.reset, or not a color.
Examples:
import random
random.seed(7)
first = Color.random()
random.seed(7)
assert Color.random() == first
hue, saturation, _ = Color.random('#3366cc').to_hsl()
assert round(hue) == 220 and round(saturation) == 60
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
to_hsl
¶
The hue (degrees), saturation and lightness (percentages) of the color.
Named and indexed colors use their standard values.
Raises:
-
ValueError–The color is
reset.
Examples:
hue, saturation, lightness = Color('#ff0000').to_hsl()
assert round(hue) == 0
assert round(saturation) == 100
assert round(lightness) == 50
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
to_hsv
¶
The hue (degrees), saturation and value (percentages) of the color.
Named and indexed colors use their standard values.
Raises:
-
ValueError–The color is
reset.
Examples:
hue, saturation, value = Color('#00ff00').to_hsv()
assert round(hue) == 120
assert round(saturation) == 100
assert round(value) == 100
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
lerp
¶
lerp(other: Color | ColorName | str | tuple[int, int, int], ratio: float, *, color_space: Literal['rgb', 'hsl', 'hsv'] = 'hsl') -> Color
The color between this one and other.
Parameters:
-
other(Color | ColorName | str | tuple[int, int, int]) –The color to blend toward.
-
ratio(float) –0.0is this color and1.0isother. -
color_space(Literal['rgb', 'hsl', 'hsv'], default:'hsl') –The space the blend runs through.
'hsl'and'hsv'keep colors vivid in the middle;'rgb'is the straight average.
Raises:
-
ValueError–A color is
reset, orcolor_spaceis not a color space.
Examples:
middle = Color('#000000').lerp('#ffffff', 0.5, color_space='rgb')
red, green, blue = middle.to_rgb()
assert red == green == blue
assert 126 <= red <= 129
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
lighten
¶
A lighter color for a positive amount, a darker one for a negative amount.
Parameters:
-
amount(float) –A fraction from -1.0 (black) to 1.0 (white).
-
color_space(Literal['rgb', 'hsl', 'hsv'], default:'hsl') –The space the change runs through.
Raises:
-
ValueError–The color is
reset, orcolor_spaceis not a color space.
Examples:
base = Color('#336699')
assert base.lighten(0.2).to_hsl()[2] > base.to_hsl()[2]
assert base.lighten(-0.2).to_hsl()[2] < base.to_hsl()[2]
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
darken
¶
A darker color.
Parameters:
-
amount(float) –A fraction from 0.0 (unchanged) to 1.0 (black). Use
lightento make a color lighter. -
color_space(Literal['rgb', 'hsl', 'hsv'], default:'hsl') –The space the change runs through.
Returns:
-
Color–The darkened color; the original is unchanged.
Raises:
-
ValueError–amountis outside 0.0 to 1.0, the color isreset, orcolor_spaceis not a color space.
Examples:
assert Color('#808080').darken(1.0).to_rgb() == (0, 0, 0)
base = Color('#336699')
assert base.darken(0.2).to_hsl()[2] < base.to_hsl()[2]
assert base.darken(0.0) == base
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
inverse
¶
inverse() -> Color
The color that has each red, green and blue channel inverted.
Named and indexed colors are inverted through their standard values, so the result is always a true color.
Returns:
-
Color–The inverted true color; the original is unchanged.
Raises:
-
ValueError–The color is
reset, which has no fixed value.
Examples:
assert Color('#ff8000').inverse().to_hex() == '#007fff'
assert Color('#ff8000').inverse().inverse() == Color('#ff8000')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
saturate
¶
The color with its saturation scaled.
Parameters:
-
factor(float) –0.0removes the saturation,1.0keeps it and more than1.0boosts it. -
color_space(Literal['rgb', 'hsl', 'hsv'], default:'hsl') –The space the change runs through.
Raises:
-
ValueError–The color is
reset, orcolor_spaceis not a color space.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
Style
¶
Style(foreground: ColorValue | None = None, background: ColorValue | None = None, *, bold: bool = False, dim: bool = False, italic: bool = False, underline: bool = False, blink: bool = False, reverse: bool = False, hidden: bool = False, crossed_out: bool = False, rapid_blink: bool = False, underline_color: ColorValue | None = None)
A foreground color, a background color and text modifiers.
Maps to ratatui::style::Style.
Parameters:
-
foreground(ColorValue | None, default:None) –The text color.
-
background(ColorValue | None, default:None) –The cell background color.
-
bold(bool, default:False) –Whether text is bold.
-
dim(bool, default:False) –Whether text is dimmed.
-
italic(bool, default:False) –Whether text is italic.
-
underline(bool, default:False) –Whether text is underlined.
-
blink(bool, default:False) –Whether text blinks.
-
reverse(bool, default:False) –Whether foreground and background swap.
-
hidden(bool, default:False) –Whether text is hidden.
-
crossed_out(bool, default:False) –Whether text is struck through.
Attributes:
-
foreground(Color | None) –The text color, or
Nonewhen unset. -
background(Color | None) –The background color, or
Nonewhen unset. -
modifiers(frozenset[Modifier]) –The enabled text modifiers.
Examples:
style = Style('red', 'black', bold=True)
assert style.foreground == Color('red')
assert 'bold' in style.modifiers
Create a Style.
Parameters:
-
foreground(ColorValue | None, default:None) –ColorValue | None. Defaults toNone. -
background(ColorValue | None, default:None) –ColorValue | None. Defaults toNone. -
bold(bool, default:False) –bool. Defaults toFalse. -
dim(bool, default:False) –bool. Defaults toFalse. -
italic(bool, default:False) –bool. Defaults toFalse. -
underline(bool, default:False) –bool. Defaults toFalse. -
blink(bool, default:False) –bool. Defaults toFalse. -
reverse(bool, default:False) –bool. Defaults toFalse. -
hidden(bool, default:False) –bool. Defaults toFalse. -
crossed_out(bool, default:False) –bool. Defaults toFalse. -
rapid_blink(bool, default:False) –bool. Defaults toFalse. -
underline_color(ColorValue | None, default:None) –ColorValue | None. Defaults toNone.
-
API Reference
-
xnano
-
xnano.buffers
buffersCellstyle -
xnano.operations
operationsClearstyle -
xnano.placement
placementPlacementstyle -
xnano.styles
styles -
xnano.types
types -
xnano.widgets
widgets
-
xnano.buffers
-
xnano_core
-
xnano_core -
xnano_core._xnano_core
_xnano_core
-
-
xnano
-
API Reference
-
xnano
-
xnano -
xnano.buffers
buffers -
xnano.canvas
canvasCanvas -
xnano.effects
effects -
xnano.operations
operations -
xnano.placement
placementPlacement -
xnano.styles
styles -
xnano.terminal
terminalTerminal -
xnano.widgets
widgets
-
-
xnano_core
-
xnano_core-
ApplyStyle -
Bar -
BarChart -
Block -
BlockTitle -
Border -
Buffer -
Calendar -
Canvas -
CanvasLabel -
Cell -
Chart -
ChartAxis -
ChartDataset -
Clear -
CustomWidget -
Dissolve -
Editor -
Effect -
Fade -
Gauge -
Glitch -
ItemList -
LineGauge -
ListItem -
Live -
Paragraph -
Region -
Scrollbar -
Sparkline -
SparklineBar -
Stylepatch -
Surface -
Table -
TableCell -
TableRow -
Tabs -
TextSpan -
Widget -
Write -
WriteCellText
-
-
xnano_core._xnano_core
_xnano_core-
ApplyStyle -
Bar -
BarChart -
Block -
BlockTitle -
Border -
Buffer -
Calendar -
Canvas -
CanvasLabel -
Cell -
Chart -
ChartAxis -
ChartDataset -
Clear -
CustomWidget -
Dissolve -
Editor -
Effect -
Fade -
Gauge -
Glitch -
ItemList -
LineGauge -
ListItem -
Live -
Paragraph -
Region -
Scrollbar -
Sparkline -
SparklineBar -
Stylepatch -
Surface -
Table -
TableCell -
TableRow -
Tabs -
TextSpan -
Widget -
Write -
WriteCellText
-
-
-
xnano
Methods:
-
patch–A style where every value
othersets replaces this style's value.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
patch
¶
A style where every value other sets replaces this style's value.
Examples:
base = Style('red', bold=True)
patched = base.patch(Style(italic=True))
assert patched.foreground == Color('red')
assert patched.modifiers == {'bold', 'italic'}
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
StyledText
¶
Text made of differently styled spans.
Parameters:
Attributes:
Examples:
text = StyledText([TextSpan('error: ', style=Style('red')), TextSpan('disk full')])
assert str(text) == 'error: disk full'
assert len(text.spans) == 2
Create a StyledText.
Parameters:
Methods:
-
__str__–The text without styles.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
TextLine
¶
One wrapped line of a TextLayout.
Attributes:
-
text(str) –The text of the line.
-
start_offset(int) –Where the line starts in the original text, counted in Python string offsets.
-
end_offset(int) –Where the line ends in the original text, counted in Python string offsets.
-
width(int) –The number of cells the line occupies.
Examples:
layout = layout_text('hello world', width=6)
line = layout.lines[0]
assert line.text == 'hello '
assert (line.start_offset, line.end_offset, line.width) == (0, 6, 6)
TextSpan
¶
A run of text with one style.
Maps to ratatui::text::Span.
Parameters:
Attributes:
Examples:
span = TextSpan('warning', style=Style('yellow', bold=True))
assert span.text == 'warning'
assert 'bold' in span.style.modifiers
Create a TextSpan.
Parameters: