Skip to content

xnano.appearance

xnano.appearance

xnano.appearance

How a canvas, a placement or a region looks: its colors, border, padding and margin.

Every owner has the same four properties, color, border, padding and margin. Assigning to any of them changes the owner, and several assignments in a row are drawn once, before the next frame.

Classes:

  • ColorSlot –

    One color of an owner: the text color, the background or the border color.

  • Colors –

    The two colors of an owner: fg is the text, bg is what is behind it.

  • BorderSide –

    One side of a border: top, right, bottom or left.

  • BorderAppearance –

    The border of an owner.

  • Edges –

    The four edges of a padding or a margin, in cells.

  • PaddingAppearance –

    The space between the border and the content; see Edges.

  • MarginAppearance –

    The transparent space outside the border; see Edges.

ColorSlot

ColorSlot(owner: _Looked, name: str)

One color of an owner: the text color, the background or the border color.

A slot is live: changing it changes the owner. slot.value is the plain Color, a copy that changes nothing.

Attributes:

  • value (Color | None) –

    The plain color of the slot, or None when it has none.

Examples:

import xnano

with xnano.new(width=8, height=1) as canvas:
    label = canvas.write('hi', background='#336699')
    label.color.bg.darken(0.2)
    darker = label.color.bg.value
    assert darker != xnano.Color('#336699')
    assert darker.darken(0.5) != label.color.bg.value

Make the slot of an owner. Get one from owner.color instead of calling this.

Parameters:

  • owner (_Looked) –

    The canvas, placement or region.

  • name (str) –

    'foreground', 'background' or 'border'.

Methods:

  • darken –

    Make the color darker, in place.

  • lighten –

    Make the color lighter, in place.

  • random –

    Set the slot to a random color, or to a random shade of base, in place.

  • shade –

    Color every cell of the owner from a function of its position, in one write.

Source code in xnano/appearance.py
def __init__(self, owner: _Looked, name: str) -> None:
    """
    Make the slot of an owner. Get one from ``owner.color`` instead of calling this.

    Args:
        owner: The canvas, placement or region.
        name: ``'foreground'``, ``'background'`` or ``'border'``.
    """
    self._owner = owner
    self._name = name

value property

value: Color | None

The plain Color of the slot, or None when it has none.

darken

darken(amount: float) -> None

Make the color darker, in place.

This changes the owner and returns nothing. To get a darker copy, use slot.value.darken(amount). Assigning the result of this call to another slot raises a TypeError, but this call has already darkened the owner by then.

Parameters:

  • amount (float) –

    How much darker, from 0.0 (unchanged) to 1.0 (black).

Raises:

Source code in xnano/appearance.py
def darken(self, amount: float) -> None:
    """
    Make the color darker, in place.

    This changes the owner and returns nothing. To get a darker copy, use
    ``slot.value.darken(amount)``. Assigning the result of this call to another slot
    raises a ``TypeError``, but this call has already darkened the owner by then.

    Args:
        amount: How much darker, from ``0.0`` (unchanged) to ``1.0`` (black).

    Raises:
        TerminalError: The slot has no color.
    """
    self._write(self._require_value('darken').darken(amount))

lighten

lighten(amount: float) -> None

Make the color lighter, in place.

Parameters:

  • amount (float) –

    How much lighter, from 0.0 (unchanged) to 1.0 (white).

Raises:

Source code in xnano/appearance.py
def lighten(self, amount: float) -> None:
    """
    Make the color lighter, in place.

    Args:
        amount: How much lighter, from ``0.0`` (unchanged) to ``1.0`` (white).

    Raises:
        TerminalError: The slot has no color.
    """
    self._write(self._require_value('lighten').lighten(amount))

random

random(base: ColorName | ColorValue | None = None) -> Color

Set the slot to a random color, or to a random shade of base, in place.

This is slot = Color.random(base); see Color.random. random.seed(...) repeats the colors.

Parameters:

  • base (ColorName | ColorValue | None, default: None ) –

    The color to pick shades of, or None for any color.

Returns:

Raises:

  • ValueError –

    base is Color.reset, or not a color.

Examples:

import xnano

with xnano.new(width=8, height=1) as canvas:
    label = canvas.write('hi')
    chosen = label.color.fg.random('green')
    assert label.color.fg.value == chosen
    label.color.bg.random()
    assert label.color.bg.value is not None
Source code in xnano/appearance.py
def random(self, base: ColorName | ColorValue | None = None) -> _core.Color:
    """
    Set the slot to a random color, or to a random shade of ``base``, in place.

    This is ``slot = Color.random(base)``; see
    [`Color.random`][xnano_core.Color.random]. ``random.seed(...)`` repeats the colors.

    Args:
        base: The color to pick shades of, or ``None`` for any color.

    Returns:
        The [`Color`][xnano_core.Color] the slot was set to.

    Raises:
        ValueError: ``base`` is ``Color.reset``, or not a color.

    Examples:
        ```python
        import xnano

        with xnano.new(width=8, height=1) as canvas:
            label = canvas.write('hi')
            chosen = label.color.fg.random('green')
            assert label.color.fg.value == chosen
            label.color.bg.random()
            assert label.color.bg.value is not None
        ```
    """
    color = _core.Color.random(base)
    self._write(color)
    return color

shade

shade(function: Callable[[int, int], ColorValue | None]) -> None

Color every cell of the owner from a function of its position, in one write.

Parameters:

  • function (Callable[[int, int], ColorValue | None]) –

    Called with (x, y) in cells; return the color, or None to leave the cell alone. For half-block or braille resolution use owner.region.half_blocks.shade or owner.region.braille.shade.

Raises:

  • TerminalError –

    The slot is border.color, which is one color for the whole border and cannot be shaded.

Examples:

import xnano

with xnano.new(width=4, height=2) as canvas:
    canvas.color.bg.shade(lambda x, y: (x * 40, y * 40, 0))
    assert canvas.region[3, 1].background.to_rgb() == (120, 40, 0)
Source code in xnano/appearance.py
def shade(self, function: Callable[[int, int], ColorValue | None]) -> None:
    """
    Color every cell of the owner from a function of its position, in one write.

    Args:
        function: Called with ``(x, y)`` in cells; return the color, or ``None`` to leave
            the cell alone. For half-block or braille resolution use
            ``owner.region.half_blocks.shade`` or ``owner.region.braille.shade``.

    Raises:
        TerminalError: The slot is ``border.color``, which is one color for the whole
            border and cannot be shaded.

    Examples:
        ```python
        import xnano

        with xnano.new(width=4, height=2) as canvas:
            canvas.color.bg.shade(lambda x, y: (x * 40, y * 40, 0))
            assert canvas.region[3, 1].background.to_rgb() == (120, 40, 0)
        ```
    """
    if self._name == 'border':
        raise TerminalError(
            'The border has one color, so border.color cannot be shaded cell by cell. '
            'Assign a color to border.color, or shade the content with color.fg.shade '
            'or color.bg.shade.'
        )
    from xnano.region import Region

    owner = self._owner
    region = owner if isinstance(owner, Region) else owner.region
    if self._name == 'foreground':
        region._shade_foreground(function)
    else:
        region.shade(function)

Colors

Colors(owner: _Looked)

The two colors of an owner: fg is the text, bg is what is behind it.

Assigning accepts a color name, a (red, green, blue) tuple, a Color or another ColorSlot; 'reset' clears the color. A value that is not a color raises at the assignment and leaves the colors unchanged.

Attributes:

Make the colors of an owner. Get them from owner.color instead of calling this.

Parameters:

  • owner (_Looked) –

    The canvas, placement or region.

Methods:

  • shade –

    Color the background of every cell from a function of its position, in one write.

Source code in xnano/appearance.py
def __init__(self, owner: _Looked) -> None:
    """
    Make the colors of an owner. Get them from ``owner.color`` instead of calling this.

    Args:
        owner: The canvas, placement or region.
    """
    self._owner = owner

fg property writable

The color of the text.

bg property writable

The color behind the text.

shade

shade(function: Callable[[int, int], ColorValue | None]) -> None

Color the background of every cell from a function of its position, in one write.

Parameters:

  • function (Callable[[int, int], ColorValue | None]) –

    Called with (x, y) in cells; return the color, or None to leave the cell alone. For half-block or braille resolution use owner.region.half_blocks.shade or owner.region.braille.shade.

Source code in xnano/appearance.py
def shade(self, function: Callable[[int, int], ColorValue | None]) -> None:
    """
    Color the background of every cell from a function of its position, in one write.

    Args:
        function: Called with ``(x, y)`` in cells; return the color, or ``None`` to leave
            the cell alone. For half-block or braille resolution use
            ``owner.region.half_blocks.shade`` or ``owner.region.braille.shade``.
    """
    self.bg.shade(function)

BorderSide

BorderSide(owner: _Looked, name: str)

One side of a border: top, right, bottom or left.

Attributes:

  • kind (BorderKind | None) –

    The kind of line on this side, or None when the side is not drawn.

  • color (Color | None) –

    The color of this side, or None when it uses the border's own color.

Make a side. Get one from owner.border.top and its siblings instead.

Parameters:

  • owner (_Looked) –

    The canvas, placement or region.

  • name (str) –

    'top', 'right', 'bottom' or 'left'.

Source code in xnano/appearance.py
def __init__(self, owner: _Looked, name: str) -> None:
    """
    Make a side. Get one from ``owner.border.top`` and its siblings instead.

    Args:
        owner: The canvas, placement or region.
        name: ``'top'``, ``'right'``, ``'bottom'`` or ``'left'``.
    """
    self._owner = owner
    self._name = name

kind property writable

kind: BorderKind | None

The kind of line on this side, or None when the side is not drawn.

color property writable

color: Color | None

The color of this side, or None when it uses the border's own color.

BorderAppearance

BorderAppearance(owner: _Looked)

The border of an owner.

Attributes:

Make the border of an owner. Get it from owner.border instead of calling this.

Parameters:

  • owner (_Looked) –

    The canvas, placement or region.

Source code in xnano/appearance.py
def __init__(self, owner: _Looked) -> None:
    """
    Make the border of an owner. Get it from ``owner.border`` instead of calling this.

    Args:
        owner: The canvas, placement or region.
    """
    self._owner = owner

kind property writable

kind: BorderKind | None

The kind of border on every side that does not say otherwise.

top property writable

The top side.

right property writable

right: BorderSide

The right side.

bottom property writable

bottom: BorderSide

The bottom side.

left property writable

left: BorderSide

The left side.

color property writable

color: ColorSlot

The color of the border.

title property writable

title: str | None

The text written on the top edge, or None.

Edges

Edges(owner: _Looked)

The four edges of a padding or a margin, in cells.

Reading an edge gives the number of cells it is now. Assigning accepts a number of cells, a float or a percentage such as '10%', which is a share of the content's size when the look is drawn.

Attributes:

Make the edges of an owner. Get them from owner.padding or owner.margin.

Parameters:

  • owner (_Looked) –

    The canvas, placement or region.

Source code in xnano/appearance.py
def __init__(self, owner: _Looked) -> None:
    """
    Make the edges of an owner. Get them from ``owner.padding`` or ``owner.margin``.

    Args:
        owner: The canvas, placement or region.
    """
    self._owner = owner

top property writable

top: int

The space above.

right property writable

right: int

The space to the right.

bottom property writable

bottom: int

The space below.

left property writable

left: int

The space to the left.

PaddingAppearance

PaddingAppearance(owner: _Looked)

Bases: Edges

The space between the border and the content; see Edges.

Attributes:

Make the edges of an owner. Get them from owner.padding or owner.margin.

Parameters:

  • owner (_Looked) –

    The canvas, placement or region.

Source code in xnano/appearance.py
def __init__(self, owner: _Looked) -> None:
    """
    Make the edges of an owner. Get them from ``owner.padding`` or ``owner.margin``.

    Args:
        owner: The canvas, placement or region.
    """
    self._owner = owner

top property writable

top: int

The space above.

right property writable

right: int

The space to the right.

bottom property writable

bottom: int

The space below.

left property writable

left: int

The space to the left.

MarginAppearance

MarginAppearance(owner: _Looked)

Bases: Edges

The transparent space outside the border; see Edges.

Attributes:

Make the edges of an owner. Get them from owner.padding or owner.margin.

Parameters:

  • owner (_Looked) –

    The canvas, placement or region.

Source code in xnano/appearance.py
def __init__(self, owner: _Looked) -> None:
    """
    Make the edges of an owner. Get them from ``owner.padding`` or ``owner.margin``.

    Args:
        owner: The canvas, placement or region.
    """
    self._owner = owner

top property writable

top: int

The space above.

right property writable

right: int

The space to the right.

bottom property writable

bottom: int

The space below.

left property writable

left: int

The space to the left.