Skip to content

Commit 08e0ff9

Browse files
committed
make importable, adjust titiler formatting implementation
1 parent d7b7130 commit 08e0ff9

File tree

3 files changed

+142
-6
lines changed

3 files changed

+142
-6
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,33 @@ palettize --help
7373
-O type=diverging -O pivot=0
7474
```
7575
76+
## Programmatic usage
77+
78+
You can also use Palettize from Python by importing the library.
79+
80+
```python
81+
from palettize import (
82+
create_colormap,
83+
list_available_presets,
84+
get_scaler_by_name,
85+
)
86+
87+
# Inspect presets
88+
print(list_available_presets()[:5])
89+
90+
# Create from preset
91+
cmap = create_colormap(preset="custom/grayscale", name="Grayscale", cut_start=0.1, cut_end=0.9)
92+
print(cmap.get_color(0.5)) # hex string
93+
print(cmap.get_color(0.5, output_format="rgb_tuple"))
94+
95+
# Create from a list of colors
96+
cmap2 = create_colormap(colors=["#0000ff", "white", "#ff0000"], name="BlueWhiteRed")
97+
98+
# Use a scaler to map data values onto the colormap
99+
scaler = get_scaler_by_name("symlog", domain_min=-10, domain_max=10, linthresh=1, base=10)
100+
print(cmap2.apply_scaler(3.2, scaler))
101+
```
102+
76103
## Features
77104
78105
- **Flexible Colormap Creation**: Generate colormaps from lists of colors (hex, RGB, named) or use built-in presets.

src/palettize/__init__.py

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,116 @@
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.
22
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+
"""
46

7+
from __future__ import annotations
58

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+
)

src/palettize/exporters/titiler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ def export(
8181

8282
# The required structure is a dictionary with a "colormap" key,
8383
# where the value is the JSON-dumped string of our color map.
84-
payload = {"colormap": json.dumps(color_map_dict)}
84+
payload = {"colormap": json.dumps(color_map_dict, indent=None, separators=(",", ":"))}
8585

8686
# Finally, URL-encode the entire payload.
87-
return urllib.parse.urlencode(payload)
87+
return urllib.parse.urlencode(payload, quote_via=urllib.parse.quote)
8888

8989

9090
# Example Usage:

0 commit comments

Comments
 (0)