sphericolor 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of sphericolor might be problematic. Click here for more details.
- sphericolor/__init__.py +8 -0
- sphericolor/api.py +154 -0
- sphericolor/colors.py +47 -0
- sphericolor/data/__init__.py +0 -0
- sphericolor/data/srgb_cam16ucs.ply +6443 -0
- sphericolor/diagnostics.py +24 -0
- sphericolor/distances.py +24 -0
- sphericolor/embedding.py +31 -0
- sphericolor/gamut.py +42 -0
- sphericolor/methods/__init__.py +0 -0
- sphericolor/methods/bivariate.py +38 -0
- sphericolor/methods/hsluv.py +18 -0
- sphericolor/methods/ray.py +51 -0
- sphericolor/methods/simplex.py +89 -0
- sphericolor/result.py +50 -0
- sphericolor/validation.py +108 -0
- sphericolor-0.1.0.dist-info/METADATA +150 -0
- sphericolor-0.1.0.dist-info/RECORD +20 -0
- sphericolor-0.1.0.dist-info/WHEEL +4 -0
- sphericolor-0.1.0.dist-info/licenses/LICENSE +22 -0
sphericolor/__init__.py
ADDED
sphericolor/api.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"""Public generation API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
from numpy.typing import ArrayLike, NDArray
|
|
10
|
+
|
|
11
|
+
from .colors import cam16ucs_to_rgb, rgb_to_cam16ucs, rgb_to_hex
|
|
12
|
+
from .diagnostics import build_diagnostics
|
|
13
|
+
from .distances import great_circle_matrix
|
|
14
|
+
from .embedding import metric_mds
|
|
15
|
+
from .methods.bivariate import bivariate_colors
|
|
16
|
+
from .methods.hsluv import hsluv_colors
|
|
17
|
+
from .methods.ray import project_directions_to_gamut
|
|
18
|
+
from .methods.simplex import fit_embedding_to_gamut
|
|
19
|
+
from .result import ColorMapping, Diagnostics, immutable_metadata, readonly
|
|
20
|
+
from .validation import (
|
|
21
|
+
DISTANCE_METHODS,
|
|
22
|
+
validate_bivariate_map,
|
|
23
|
+
validate_colors,
|
|
24
|
+
validate_coordinates,
|
|
25
|
+
validate_distance_matrix,
|
|
26
|
+
validate_labels,
|
|
27
|
+
validate_method,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _mapping(
|
|
32
|
+
*,
|
|
33
|
+
labels: tuple[Any, ...],
|
|
34
|
+
rgb: NDArray[np.float64],
|
|
35
|
+
method: str,
|
|
36
|
+
metadata: dict[str, Any],
|
|
37
|
+
diagnostics: Diagnostics | None,
|
|
38
|
+
) -> ColorMapping:
|
|
39
|
+
checked_rgb = validate_colors(rgb, len(labels))
|
|
40
|
+
cam16ucs = rgb_to_cam16ucs(checked_rgb)
|
|
41
|
+
return ColorMapping(
|
|
42
|
+
labels=labels,
|
|
43
|
+
rgb=readonly(checked_rgb),
|
|
44
|
+
hex=readonly(rgb_to_hex(checked_rgb)),
|
|
45
|
+
cam16ucs=readonly(cam16ucs),
|
|
46
|
+
method=method,
|
|
47
|
+
metadata=immutable_metadata(metadata),
|
|
48
|
+
diagnostics=diagnostics,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _distance_mapping(
|
|
53
|
+
distances: NDArray[np.float64],
|
|
54
|
+
labels: tuple[Any, ...],
|
|
55
|
+
method: str,
|
|
56
|
+
random_state: int | None,
|
|
57
|
+
diagnostics: bool,
|
|
58
|
+
metadata: dict[str, Any],
|
|
59
|
+
) -> ColorMapping:
|
|
60
|
+
embedding = metric_mds(distances, random_state)
|
|
61
|
+
if method == "ray":
|
|
62
|
+
mapped, method_metadata = project_directions_to_gamut(embedding)
|
|
63
|
+
else:
|
|
64
|
+
mapped, method_metadata = fit_embedding_to_gamut(embedding, distances)
|
|
65
|
+
rgb, clipped = cam16ucs_to_rgb(mapped)
|
|
66
|
+
displayed_cam16ucs = rgb_to_cam16ucs(rgb)
|
|
67
|
+
metadata.update(method_metadata)
|
|
68
|
+
metadata.update({"clipped_color_count": clipped, "random_state": random_state})
|
|
69
|
+
diagnostic_result = (
|
|
70
|
+
build_diagnostics(distances, displayed_cam16ucs, embedding, mapped)
|
|
71
|
+
if diagnostics
|
|
72
|
+
else None
|
|
73
|
+
)
|
|
74
|
+
return _mapping(
|
|
75
|
+
labels=labels,
|
|
76
|
+
rgb=rgb,
|
|
77
|
+
method=method,
|
|
78
|
+
metadata=metadata,
|
|
79
|
+
diagnostics=diagnostic_result,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def from_latlon(
|
|
84
|
+
latitudes: ArrayLike,
|
|
85
|
+
longitudes: ArrayLike,
|
|
86
|
+
*,
|
|
87
|
+
labels: Sequence[Any] | None = None,
|
|
88
|
+
method: str = "ray",
|
|
89
|
+
random_state: int | None = 69,
|
|
90
|
+
diagnostics: bool = False,
|
|
91
|
+
bivariate_map: str = "cube_diagonal",
|
|
92
|
+
) -> ColorMapping:
|
|
93
|
+
"""Generate colors for an ordered set of latitude-longitude points."""
|
|
94
|
+
selected_method = validate_method(method)
|
|
95
|
+
lat, lon = validate_coordinates(latitudes, longitudes)
|
|
96
|
+
checked_labels = validate_labels(labels, lat.size)
|
|
97
|
+
distances = great_circle_matrix(lat, lon)
|
|
98
|
+
metadata: dict[str, Any] = {
|
|
99
|
+
"input": "latitude_longitude",
|
|
100
|
+
"distance": "great_circle_km",
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if selected_method in DISTANCE_METHODS:
|
|
104
|
+
return _distance_mapping(
|
|
105
|
+
distances,
|
|
106
|
+
checked_labels,
|
|
107
|
+
selected_method,
|
|
108
|
+
random_state,
|
|
109
|
+
diagnostics,
|
|
110
|
+
metadata,
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
if selected_method == "hsluv":
|
|
114
|
+
rgb = hsluv_colors(lat, lon)
|
|
115
|
+
else:
|
|
116
|
+
selected_bivariate = validate_bivariate_map(bivariate_map)
|
|
117
|
+
rgb = bivariate_colors(lat, lon, selected_bivariate)
|
|
118
|
+
metadata["bivariate_map"] = selected_bivariate
|
|
119
|
+
cam16ucs = rgb_to_cam16ucs(rgb)
|
|
120
|
+
diagnostic_result = (
|
|
121
|
+
build_diagnostics(distances, cam16ucs, None, None) if diagnostics else None
|
|
122
|
+
)
|
|
123
|
+
metadata["clipped_color_count"] = 0
|
|
124
|
+
return _mapping(
|
|
125
|
+
labels=checked_labels,
|
|
126
|
+
rgb=rgb,
|
|
127
|
+
method=selected_method,
|
|
128
|
+
metadata=metadata,
|
|
129
|
+
diagnostics=diagnostic_result,
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def from_distance_matrix(
|
|
134
|
+
distances: ArrayLike,
|
|
135
|
+
*,
|
|
136
|
+
labels: Sequence[Any] | None = None,
|
|
137
|
+
method: str = "ray",
|
|
138
|
+
random_state: int | None = 69,
|
|
139
|
+
diagnostics: bool = False,
|
|
140
|
+
) -> ColorMapping:
|
|
141
|
+
"""Generate colors from an ordered pairwise-distance matrix."""
|
|
142
|
+
selected_method = validate_method(method)
|
|
143
|
+
if selected_method not in DISTANCE_METHODS:
|
|
144
|
+
raise ValueError("distance-matrix input supports only ray and simplex")
|
|
145
|
+
matrix = validate_distance_matrix(distances)
|
|
146
|
+
checked_labels = validate_labels(labels, matrix.shape[0])
|
|
147
|
+
return _distance_mapping(
|
|
148
|
+
matrix,
|
|
149
|
+
checked_labels,
|
|
150
|
+
selected_method,
|
|
151
|
+
random_state,
|
|
152
|
+
diagnostics,
|
|
153
|
+
{"input": "distance_matrix", "distance": "user_supplied"},
|
|
154
|
+
)
|
sphericolor/colors.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Color-space conversions and serialization."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from coloraide import Color as BaseColor
|
|
7
|
+
from coloraide.distance.delta_e_cam16 import DECAM16
|
|
8
|
+
from coloraide.spaces.cam16_ucs import CAM16LCD, CAM16SCD, CAM16UCS, CAM16JMh # type: ignore[attr-defined]
|
|
9
|
+
from numpy.typing import ArrayLike, NDArray
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Color(BaseColor):
|
|
13
|
+
"""Package-local ColorAide registry."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
Color.register([CAM16UCS(), CAM16SCD(), CAM16LCD(), DECAM16(), CAM16JMh()])
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def rgb_to_cam16ucs(rgb: ArrayLike) -> NDArray[np.float64]:
|
|
20
|
+
values = np.asarray(rgb, dtype=np.float64)
|
|
21
|
+
output = np.empty_like(values)
|
|
22
|
+
for index, color in enumerate(values):
|
|
23
|
+
converted = Color("srgb", color.tolist()).convert("cam16-ucs")
|
|
24
|
+
output[index] = converted.coords()
|
|
25
|
+
return output
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def cam16ucs_to_rgb(
|
|
29
|
+
cam16ucs: ArrayLike,
|
|
30
|
+
) -> tuple[NDArray[np.float64], int]:
|
|
31
|
+
values = np.asarray(cam16ucs, dtype=np.float64)
|
|
32
|
+
output = np.empty_like(values)
|
|
33
|
+
clipped = 0
|
|
34
|
+
for index, color in enumerate(values):
|
|
35
|
+
converted = np.asarray(
|
|
36
|
+
Color("cam16-ucs", color.tolist()).convert("srgb").coords(), dtype=np.float64
|
|
37
|
+
)
|
|
38
|
+
clipped += int(np.any((converted < 0.0) | (converted > 1.0)))
|
|
39
|
+
output[index] = np.clip(converted, 0.0, 1.0)
|
|
40
|
+
return output, clipped
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def rgb_to_hex(rgb: ArrayLike) -> NDArray[np.str_]:
|
|
44
|
+
values = np.rint(np.asarray(rgb, dtype=np.float64) * 255.0).astype(np.uint8)
|
|
45
|
+
return np.asarray(
|
|
46
|
+
[f"#{red:02x}{green:02x}{blue:02x}" for red, green, blue in values], dtype=np.str_
|
|
47
|
+
)
|
|
File without changes
|