Decoders API

The thcolor.decoders module defines base utilities for decoding color expressions.

class thcolor.decoders.ColorDecoder

Base color decoder.

This color decoder behaves as a mapping returning syntax elements, with the additional properties controlling its behaviour.

The properties defined at class definition time are the following:

__mapping__

Defines the base mapping that is copied at the instanciation of each class.

__ncol_support__

Defines whether natural colors (NCol) are supported while decoding or not.

__extended_hex_support__

Defines whether 4 or 8-digit hexadecimal colors (starting with a ‘#’) are allowed or not.

__defaults_to_netscape_color

Defines whether color decoding defaults to Netscape color parsing or not.

These properties cannot be changed at runtime, although they might be in a future version of thcolor.

decode(expr, prefer_colors=False, prefer_angles=False)

Decode a color expression.

When top-level result(s) are not colors and colors are actually expected if possible to obtain, the caller should set prefer_colors to True in order for top-level conversions to take place.

Otherwise, when top-level result(s) are not angles and angles are actually expected if possible to obtain, the caller should set prefer_angles to True in order for top-level conversions to take place.

Return type

Sequence[Union[Color, Angle, int, float, None]]

For convenience, the following class is defined to define color decoders:

class thcolor.decoders.MetaColorDecoder

Base meta color decoder, which gets the function and things.

class thcolor.decoders.alias(name, args=())

Define an alias for a function.

thcolor.decoders.fallback(value)

Decorator for setting a fallback value on a function.

When a function is used as a symbol instead of a function, by default, it yields that it is callable and should be accompanied with arguments.

Using this decorator on a function makes it to be evaluated as a color in this case.

See Defining a decoder for more information about how to use these classes.