|
1 | | -"""Palettize: Python utility and CLI tool for generating and exporting color map objects.""" |
| 1 | +"""Palettize: Python utility and CLI tool for generating and exporting colormaps. |
2 | 2 |
|
3 | | -__version__ = "0.0.1.dev0" # Initial development version |
| 3 | +This module exposes a small, stable programmatic API so Palettize can be used |
| 4 | +directly from Python scripts and notebooks (in addition to the CLI). |
| 5 | +""" |
4 | 6 |
|
| 7 | +from __future__ import annotations |
5 | 8 |
|
6 | | -def hello() -> str: |
7 | | - return "Hello from palettize!" |
| 9 | +from typing import List, Optional |
| 10 | + |
| 11 | +from .core import ( |
| 12 | + Colormap, |
| 13 | + ColorStop, |
| 14 | + InputColor, |
| 15 | + parse_input_color, |
| 16 | +) |
| 17 | +from .presets import list_available_presets, load_preset_data |
| 18 | +from .scaling import ( |
| 19 | + ScalingFunction, |
| 20 | + get_scaler_by_name, |
| 21 | + get_linear_scaler, |
| 22 | + get_power_scaler, |
| 23 | + get_sqrt_scaler, |
| 24 | + get_log_scaler, |
| 25 | + get_symlog_scaler, |
| 26 | +) |
| 27 | +from .exporters import ( |
| 28 | + list_available_exporters, |
| 29 | + get_exporter, |
| 30 | + register_exporter, |
| 31 | + load_plugin_exporters, |
| 32 | +) |
| 33 | + |
| 34 | +# NOTE: When packaging via setuptools_scm, the version is provided dynamically. |
| 35 | +# Keeping a fallback here for local development if desired. |
| 36 | +__version__ = "0.0.1.dev0" |
| 37 | + |
| 38 | + |
| 39 | +def create_colormap( |
| 40 | + *, |
| 41 | + preset: Optional[str] = None, |
| 42 | + colors: Optional[List[InputColor]] = None, |
| 43 | + interpolation_space: str = "oklch", |
| 44 | + name: Optional[str] = None, |
| 45 | + cut_start: float = 0.0, |
| 46 | + cut_end: float = 1.0, |
| 47 | +) -> Colormap: |
| 48 | + """Create a `Colormap` either from a preset name or a list of colors. |
| 49 | +
|
| 50 | + Exactly one of `preset` or `colors` must be provided. |
| 51 | +
|
| 52 | + Args: |
| 53 | + preset: Name of a preset palette (e.g., "viridis" or "custom/grayscale"). |
| 54 | + colors: List of colors (hex, tuples, named strings, ColorAide `Color`, or dicts). |
| 55 | + interpolation_space: Color space used for interpolation (default "oklch"). |
| 56 | + name: Optional name for the resulting colormap. |
| 57 | + cut_start: Start of the sub-segment of the colormap to use (0-1). |
| 58 | + cut_end: End of the sub-segment of the colormap to use (0-1). |
| 59 | +
|
| 60 | + Returns: |
| 61 | + A `Colormap` instance. |
| 62 | +
|
| 63 | + Raises: |
| 64 | + ValueError: If neither or both of `preset` and `colors` are provided, or |
| 65 | + if parameters are otherwise invalid. |
| 66 | + """ |
| 67 | + if (preset is None and not colors) or (preset is not None and colors): |
| 68 | + raise ValueError("Provide exactly one of `preset` or `colors`.") |
| 69 | + |
| 70 | + if preset is not None: |
| 71 | + cmap = Colormap.from_preset( |
| 72 | + preset_name=preset, |
| 73 | + interpolation_space=interpolation_space, |
| 74 | + cut_start=cut_start, |
| 75 | + cut_end=cut_end, |
| 76 | + ) |
| 77 | + else: |
| 78 | + assert colors is not None |
| 79 | + cmap = Colormap.from_list( |
| 80 | + colors=colors, |
| 81 | + interpolation_space=interpolation_space, |
| 82 | + cut_start=cut_start, |
| 83 | + cut_end=cut_end, |
| 84 | + ) |
| 85 | + |
| 86 | + if name: |
| 87 | + cmap.name = name |
| 88 | + return cmap |
| 89 | + |
| 90 | + |
| 91 | +__all__ = ( |
| 92 | + # Version |
| 93 | + "__version__", |
| 94 | + # Core colormap API |
| 95 | + "Colormap", |
| 96 | + "ColorStop", |
| 97 | + "InputColor", |
| 98 | + "parse_input_color", |
| 99 | + "create_colormap", |
| 100 | + # Presets |
| 101 | + "list_available_presets", |
| 102 | + "load_preset_data", |
| 103 | + # Scaling |
| 104 | + "ScalingFunction", |
| 105 | + "get_scaler_by_name", |
| 106 | + "get_linear_scaler", |
| 107 | + "get_power_scaler", |
| 108 | + "get_sqrt_scaler", |
| 109 | + "get_log_scaler", |
| 110 | + "get_symlog_scaler", |
| 111 | + # Exporters |
| 112 | + "list_available_exporters", |
| 113 | + "get_exporter", |
| 114 | + "register_exporter", |
| 115 | + "load_plugin_exporters", |
| 116 | +) |
0 commit comments