Canvas Configuration¶
import chafa
config = chafa.CanvasConfig()
config.height = 10
config.width = 10
config.fg_color = (255, 0, 255)
CanvasConfig¶
A CanvasConfig
describes a set of parameters for Canvas
, such as its geometry, color space and other output characteristics.
To create a new CanvasConfig
, simply initialise that class. You can then modify it changing it’s properties (which are inherited from it’s base class ReadOnlyCanvasConfig
) and via it’s setter functions e.g. CanvasConfig.set_symbol_map()
before using it to initialise a Canvas
.
Note that it is not possible to change a canvas’ configuration after the canvas is created.
- class chafa.CanvasConfig¶
- Bases:
- copy()¶
Creates a new
CanvasConfig
that’s a copy of this one.- Return type:
- set_symbol_map(symbol_map: SymbolMap)¶
Assigns a copy of symbol_map to config.
- Parameters:
symbol_map (SymbolMap) – The symbol_map.
- calc_canvas_geometry(src_width: int, src_height: int, font_ratio: float, zoom: bool = False, stretch: bool = False)¶
Calculates an optimal geometry for a
Canvas
given the width and height of an input image, font ratio, zoom and stretch preferences. This will then set the config’s width and height to the calculated values.- Parameters:
- Raises:
ValueError – if src_width src_height or font_ratio are <= 0
TypeError – if zoom or stretch are None
ReadOnlyCanvasConfig¶
This class describes a read only CanvasConfig
. This is returned from e.g. Canvas.peek_config()
.
The properties of a ReadOnlyCanvasConfig
become editable when inherited by a CanvasConfig
.
Attention
All properties of
ReadOnlyCanvasConfig
can be inspected but not changed!
- class chafa.ReadOnlyCanvasConfig¶
-
- property pixel_mode¶
- Type:
The config’s stored
PixelMode
. This determines how pixel graphics are rendered in the output.
- property canvas_mode¶
- Type:
The config’s stored
CanvasMode
. This determines how colours (and colour control codes) are used in the output.
- property color_extractor¶
- Type:
The config’s stored
ColorExtractor
. This determines how colours are approximated in character symbol output e.g.PixelMode.CHAFA_PIXEL_MODE_SYMBOLS
.
- property color_space¶
- Type:
The config’s stored
ColorSpace
.
- property dither_mode¶
- Type:
The config’s stored
DitherMode
.
- property dither_intensity¶
- Type:
The relative intensity of the dithering pattern applied during image conversion. 1.0 is the default, corresponding to a moderate intensity.
- property dither_width¶
- Type:
The config’s stored dither grain width in pixels. These values can be 1, 2, 4 or 8. 8 corresponds to the size of an entire character cell. The default is 4 pixels.
- property dither_height¶
- Type:
The config’s stored dither grain width in pixels. These values can be 1, 2, 4 or 8. 8 corresponds to the size of an entire character cell. The default is 4 pixels.
- property transparency_threshold¶
- Type:
The threshold above which full transparency will be used.
- property fg_only¶
- Type:
Queries whether to use foreground colors only, leaving the background unmodified in the canvas output. When this is set, the canvas will emit escape codes to set the foreground color only.
Note
This is relevant only when the
pixel_mode
is set toPixelMode.CHAFA_PIXEL_MODE_SYMBOLS
.
- property fg_color¶
-
The assumed foreground color of the output device. This is used to determine how to apply the foreground pen in FGBG modes like
CanvasMode.CHAFA_CANVAS_MODE_FGBG
.The color is a tuple containing 3 integers in range [0,255] corresponding to red, green and blue respectively.
- property bg_color¶
-
The assumed background color of the output device. This is used to determine how to apply the background pen in FGBG modes like
CanvasMode.CHAFA_CANVAS_MODE_FGBG
.The color is a tuple containing 3 integers in range [0,255] corresponding to red, green and blue respectively.
- property preprocessing¶
- Type:
Indicates whether automatic image preprocessing should be enabled. This allows Chafa to boost contrast and saturation in an attempt to improve legibility.
Note
The type of preprocessing applied (if any) depends on the
canvas_mode
.
- property work_factor¶
- Type:
Gets the work/quality tradeoff factor. A higher value means more time and memory will be spent towards a higher quality output.
- property optimizations¶
- Type:
tuple[Optimizations, …]
The config’s optimization flags. When enabled, these may produce more compact output at the cost of reduced compatibility and increased CPU use.
The config’s optimizations are a tuple containing all enabled flags.
Note
Output quality is unaffected by optimizations.
- get_geometry()¶
Get the config’s canvas geometry in character cells. This is the same as inspecting
width
andheight
- peek_symbol_map()¶
Returns a read only version of the
SymbolMap
belonging to the config.Note
There are currently no implemented attributes or getter functions for
ReadOnlySymbolMap
.- Return type: