color_tools.exporters.python_exporter

Python source-code palette exporter.

Exports palettes as directly usable Python source code.

Supported representations:

dict
    PALETTE = {
        "Medium Blue": (53, 105, 184),
        "Muted Violet": (128, 88, 166),
    }

list
    PALETTE = [
        (53, 105, 184),  # Medium Blue
        (128, 88, 166),  # Muted Violet
    ]

tuple
    PALETTE = (
        (53, 105, 184),  # Medium Blue
        (128, 88, 166),  # Muted Violet
    )

constants
    MEDIUM_BLUE = (53, 105, 184)
    MUTED_VIOLET = (128, 88, 166)

RGB values may be emitted either as standard 0-255 integers or normalized 0.0-1.0 floating-point values. Hexadecimal strings are also supported.

Palette metadata may optionally be emitted as a separate Python dictionary.

Example:

>>> from color_tools.exporters import get_exporter
>>> from color_tools.exporters.python_exporter import PythonExportOptions
>>>
>>> exporter = get_exporter("python")
>>> exporter.export_palette(
...     palette,
...     "palette.py",
...     options=PythonExportOptions(
...         representation="dict",
...         normalized=True,
...     ),
... )
class color_tools.exporters.python_exporter.PythonExportOptions(representation='dict', value_format='rgb', normalized=False, include_alpha=False, include_metadata=True, include_names_as_comments=True, variable_name='PALETTE', precision=6)[source]

Bases: ExportOptionsBase

Per-export configuration for Python source-code palettes.

Variables:
  • representation

    Python structure used to represent the palette.

    Supported values:

    • "dict"

    • "list"

    • "tuple"

    • "constants"

  • value_format

    Color representation.

    "rgb" produces tuples such as (53, 105, 184).

    "hex" produces strings such as "#3569B8".

  • normalized

    Emit RGB channels as normalized floating-point values in the range 0.0-1.0 instead of integers in the range 0-255.

    Only valid with value_format="rgb".

  • include_alpha

    Add a fully opaque alpha channel to RGB values.

    Integer output uses 255.

    Normalized output uses 1.0.

    Only valid with value_format="rgb".

  • include_metadata – When exporting PaletteExportData, emit palette metadata as a separate dictionary.

  • include_names_as_comments – Include color names as trailing comments for list and tuple representations.

  • variable_name

    Python variable used for dict, list, and tuple representations.

    The metadata variable is derived from this name by appending _METADATA.

    For example:

    variable_name="UI_COLORS"
    

    produces:

    UI_COLORS_METADATA = {…} UI_COLORS = {…}

  • precision – Decimal precision used for normalized floating-point channels.

Parameters:
  • representation (Literal['dict', 'list', 'tuple', 'constants'])

  • value_format (Literal['rgb', 'hex'])

  • normalized (bool)

  • include_alpha (bool)

  • include_metadata (bool)

  • include_names_as_comments (bool)

  • variable_name (str)

  • precision (int)

representation: Literal['dict', 'list', 'tuple', 'constants']
value_format: Literal['rgb', 'hex']
normalized: bool
include_alpha: bool
include_metadata: bool
include_names_as_comments: bool
variable_name: str
precision: int
__post_init__()[source]

Validate Python exporter options.

Return type:

None

class color_tools.exporters.python_exporter.PythonExporter[source]

Bases: PaletteExporter

Export palettes as Python source code.

This exporter is intended to produce source that can be dropped directly into Python applications, games, tools, and rendering code.

Several output structures are available through PythonExportOptions:

  • Dictionary

  • List

  • Tuple

  • Named constants

RGB values may be emitted as 0-255 integers or normalized floating-point values. Hexadecimal strings are also supported.

Example:

>>> exporter = get_exporter("python")
>>> exporter.export_palette(
...     palette,
...     "palette.py",
...     options=PythonExportOptions(
...         representation="dict",
...         normalized=True,
...         variable_name="GAME_COLORS",
...     ),
... )

Result:

GAME_COLORS_METADATA = {
    ...
}

GAME_COLORS = {
    "Medium Blue": (0.207843, 0.411765, 0.721569),
    ...
}

The generated structure is intentionally predictable so a future color_tools Python importer can safely parse canonical exporter output using Python’s AST without executing the source file.

property metadata: ExporterMetadata

Return metadata describing the Python exporter.