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:
fgis the text,bgis what is behind it. -
BorderSide–One side of a border:
top,right,bottomorleft. -
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:
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
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) to1.0(black).
Raises:
-
TerminalError–The slot has no color.
Source code in xnano/appearance.py
lighten
¶
lighten(amount: float) -> None
Make the color lighter, in place.
Parameters:
-
amount(float) –How much lighter, from
0.0(unchanged) to1.0(white).
Raises:
-
TerminalError–The slot has no color.
Source code in xnano/appearance.py
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
Nonefor any color.
Returns:
Raises:
-
ValueError–baseisColor.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
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, orNoneto leave the cell alone. For half-block or braille resolution useowner.region.half_blocks.shadeorowner.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
Colors
¶
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
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, orNoneto leave the cell alone. For half-block or braille resolution useowner.region.half_blocks.shadeorowner.region.braille.shade.
Source code in xnano/appearance.py
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
Nonewhen the side is not drawn. -
color(Color | None) –The color of this side, or
Nonewhen 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
kind
property
writable
¶
kind: BorderKind | None
The kind of line on this side, or None when the side is not drawn.
BorderAppearance
¶
The border of an owner.
Attributes:
-
kind(BorderKind | None) –'plain','rounded','double'or'thick';Nonefor no border. -
top(BorderSide) –The top side, a
BorderSide. -
right(BorderSide) –The right side.
-
bottom(BorderSide) –The bottom side.
-
left(BorderSide) –The left side.
-
color(ColorSlot) –The border color, a
ColorSlot. A side may have its own. -
title(str | None) –The text written on the top edge, or
None.
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
kind
property
writable
¶
kind: BorderKind | None
The kind of border on every side that does not say otherwise.
Edges
¶
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:
-
top(int) –The space above.
-
right(int) –The space to the right.
-
bottom(int) –The space below.
-
left(int) –The space to the left.
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
PaddingAppearance
¶
Bases: Edges
The space between the border and the content; see Edges.
Attributes:
-
top(int) –The space above.
-
right(int) –The space to the right.
-
bottom(int) –The space below.
-
left(int) –The space to the left.
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
MarginAppearance
¶
Bases: Edges
The transparent space outside the border; see Edges.
Attributes:
-
top(int) –The space above.
-
right(int) –The space to the right.
-
bottom(int) –The space below.
-
left(int) –The space to the left.
Make the edges of an owner. Get them from owner.padding or owner.margin.
Parameters:
-
owner(_Looked) –The canvas, placement or region.