color_tools.importers
Palette importer system.
Importers convert external palette formats into the shared color_tools palette representation:
- PaletteExportData
├── list[ColorRecord] └── PaletteMetadata
Importer classes register automatically when this package is imported.
Basic usage:
>>> from color_tools.importers import import_palette
>>>
>>> palette = import_palette("palette.gpl")
>>> print(palette.metadata.name)
Explicit importer selection is also available:
>>> from color_tools.importers import get_importer
>>>
>>> importer = get_importer("gpl")
>>> palette = importer.import_palette("palette.gpl")
Automatic detection becomes especially useful for extensions shared by multiple
formats. For example, future JASC PAL and RIFF PAL importers can both register
the .pal extension and distinguish themselves by inspecting file contents.
- class color_tools.importers.ImporterDependency(package, import_name, extra=None)[source]
Bases:
objectOptional third-party dependency required by an importer.
- Variables:
package – Distribution/package name used when installing the dependency.
import_name – Python module name used to test whether the dependency is installed.
extra – Optional color-tools dependency extra that installs the package.
- Parameters:
- class color_tools.importers.ImporterMetadata(name, description, file_extensions, is_binary=False, dependencies=())[source]
Bases:
objectMetadata describing a palette importer’s capabilities.
- Variables:
name – Machine-readable importer identifier such as
"gpl"or"jasc_pal".description – Human-readable description of the palette format.
file_extensions –
File extensions recognized by the importer, without leading dots.
Importers use a tuple because a single format may legitimately be associated with more than one extension.
is_binary – Whether the input format is binary.
dependencies – Optional third-party dependencies required by the importer.
- Parameters:
- dependencies: tuple[ImporterDependency, ...]
- exception color_tools.importers.MissingImporterDependencyError(importer_name, dependencies)[source]
Bases:
RuntimeErrorRaised when an importer requires an unavailable optional dependency.
- Variables:
importer_name – Machine-readable importer name.
dependencies – Missing dependencies required by the importer.
- Parameters:
importer_name (str)
dependencies (tuple[ImporterDependency, ...])
- Return type:
None
- class color_tools.importers.PaletteImporter[source]
Bases:
ABCBase class for all palette importers.
Concrete importers provide format metadata and implement
_import_palette_impl().The base class handles:
Input-path validation.
Optional dependency checking.
Extension matching.
Format-detection dispatch.
Shared ColorRecord construction.
Color-space conversions are performed through color_tools.conversions so imported colors use the same calculations as colors created elsewhere in the library.
A format with an ambiguous extension should override
_can_import_impl()to inspect the file signature or contents.For example, both JASC PAL and RIFF PAL use
.pal, so those importers should distinguish themselves by examining their respective headers.- abstract property metadata: ImporterMetadata
Return metadata describing this importer.
- Returns:
Importer metadata.
- property missing_dependencies: tuple[ImporterDependency, ...]
Return optional dependencies that are not currently installed.
- Returns:
Tuple containing unavailable dependencies.
- property is_available: bool
Return whether the importer can run in the current environment.
- Returns:
True if all required dependencies are installed.
- import_palette(input_path)[source]
Import a palette file.
- Parameters:
- Return type:
- Returns:
PaletteExportData containing imported colors and metadata.
- Raises:
FileNotFoundError – If the supplied file does not exist.
IsADirectoryError – If input_path refers to a directory.
MissingImporterDependencyError – If a required optional dependency is unavailable.
- color_tools.importers.register_importer(cls)[source]
Register a palette importer class.
The importer is instantiated once during registration to retrieve its metadata. The class itself is stored and fresh instances are created on demand.
- Parameters:
cls (
type[PaletteImporter]) – PaletteImporter subclass.- Return type:
- Returns:
The original class, allowing use as a decorator.
- Raises:
ValueError – If another importer already uses the same metadata name.
Example
>>> @register_importer ... class ExampleImporter(PaletteImporter): ... ...
- color_tools.importers.get_importer(format_name)[source]
Create an importer by registered format name.
- Parameters:
format_name (
str) – Importer metadata name.- Return type:
- Returns:
Fresh importer instance.
- Raises:
ValueError – If the format is not registered.
- color_tools.importers.get_importers_for_extension(extension, *, available_only=True)[source]
Return importers registered for a file extension.
More than one importer may be returned because extensions are not unique.
- Parameters:
- Return type:
- Returns:
Fresh importer instances matching the extension.
- color_tools.importers.detect_importer(input_path)[source]
Detect the importer for a palette file.
Detection proceeds in two stages:
Select importers registered for the file extension.
Ask each candidate to inspect the file using can_import().
This allows formats that share extensions to distinguish themselves using file signatures or headers.
- Parameters:
- Return type:
- Returns:
Matching importer.
- Raises:
FileNotFoundError – If the file does not exist.
ValueError – If there are no candidate importers, no candidate recognizes the file, or multiple candidates recognize it.
- color_tools.importers.import_palette(input_path, *, format_name=None)[source]
Import a palette using explicit or automatic format selection.
- Parameters:
- Return type:
- Returns:
Imported PaletteExportData.
Example
Automatic detection:
>>> palette = import_palette("palette.gpl")
Explicit format:
>>> palette = import_palette( ... "palette.pal", ... format_name="jasc_pal", ... )
- color_tools.importers.list_import_formats(*, available_only=True)[source]
List registered palette import formats.
- class color_tools.importers.GPLImporter[source]
Bases:
PaletteImporterImport GIMP Palette (.gpl) files.
Standard GPL metadata is preserved where possible. color_tools-specific Author, Description, and Tags comments are also recovered when present.
- property metadata: ImporterMetadata
Return metadata describing the GPL importer.
- class color_tools.importers.HexImporter[source]
Bases:
PaletteImporterImport plain-text hexadecimal RGB palette files.
The format is intentionally minimal:
One RGB color per line.
Six hexadecimal digits.
Leading
#is accepted but not required.Blank lines are ignored.
Comment lines beginning with
# `` or ``;are ignored.
No palette-level metadata is available in the format.
- property metadata: ImporterMetadata
Return metadata describing the HEX importer.
- class color_tools.importers.JascPalImporter[source]
Bases:
PaletteImporterImport JASC-PAL (.pal) palette files.
JASC PAL is a simple text format consisting of:
JASC-PALsignature0100versionDeclared color count
One
R G Bentry per color
The
.palextension is shared with RIFF PAL, so this importer detects its format by checking the JASC-PAL header.- property metadata: ImporterMetadata
Return metadata describing the JASC PAL importer.
- class color_tools.importers.RiffPalImporter[source]
Bases:
PaletteImporterImport RIFF PAL (.pal) palette files.
RIFF PAL is a binary RIFF container whose form type is
PAL `` and whose palette data is stored in a ``datachunk.The
.palextension is shared with JASC PAL, so this importer detects its format by checking the RIFF and PAL signatures.- property metadata: ImporterMetadata
Return metadata describing the RIFF PAL importer.