Skip to content

Commit 228d471

Browse files
SiggyFtlambert03
andauthored
feat: Implement Google Earth Engine palette external (#82)
* Implement Google Earth Engine palette external * linting --------- Co-authored-by: Talley Lambert <[email protected]>
1 parent 9241912 commit 228d471

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ third-party colormap objects, including:
102102
- [plotly](https://plotly.com/python/)
103103
- [bokeh](https://docs.bokeh.org/en/latest/)
104104
- [altair](https://altair-viz.github.io/)
105+
- [earthengine-api](https://developers.google.com/earth-engine/guides/quickstart_python)
105106
- [pyqtgraph](https://www.pyqtgraph.org/)
106107

107108
See [documentation](https://cmap-docs.readthedocs.io/en/latest/colormaps/#usage-with-external-visualization-libraries)

docs/colormaps.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,13 @@ external visualization libraries. To that end, `cmap.Colormap` provides
368368

369369
Returns a list of hexadecimal color strings.
370370

371+
- [gee](https://developers.google.com/earth-engine/guides/quickstart_python)
372+
373+
```python
374+
Colormap("viridis").to_gee()
375+
```
376+
Returns a list of hexadecimal color strings (without a `#` prefix).
377+
371378
- [pyqtgraph](https://pyqtgraph.org/)
372379

373380
```python

src/cmap/_colormap.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,15 @@ def to_altair(self, N: int = 256) -> list[str]:
744744
"""
745745
return _external.to_altair(self, N=N)
746746

747+
def to_gee(self, N: int = 256) -> list[str]:
748+
"""Return a Google Earth Engine palette with N color samples from the colormap.
749+
750+
Suitable for passing to the palette parameter of Google Earth Engine
751+
visualizations. See:
752+
https://developers.google.com/earth-engine/guides/image_visualization
753+
"""
754+
return _external.to_gee(self, N=N)
755+
747756
def to_viscm(
748757
self, dpi: int = 100, dest: str | None = None
749758
) -> matplotlib.figure.Figure:

src/cmap/_external.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ def to_altair(cm: Colormap, N: int = 256) -> list[str]:
106106
return [color.hex for color in cm.iter_colors(N)]
107107

108108

109+
def to_gee(cm: Colormap, N: int = 256) -> list[str]:
110+
"""Return a Google Earth Engine palette with N color samples from the colormap.
111+
112+
Suitable for passing to the palette parameter of Google Earth Engine visualizations.
113+
114+
See details at
115+
https://developers.google.com/earth-engine/guides/image_visualization#color-palettes
116+
"""
117+
rgbs = [
118+
# drop the # symbol
119+
color.hex.lstrip("#")
120+
for color in cm.iter_colors(N)
121+
]
122+
return rgbs
123+
124+
109125
def to_pyqtgraph(cm: Colormap) -> PyqtgraphColorMap:
110126
"""Return a `pyqtgraph.Colormap`."""
111127
from pyqtgraph import ColorMap

tests/test_third_party.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ def test_altair() -> None:
147147
assert alt[-1] == "#0000FF"
148148

149149

150+
def test_gee() -> None:
151+
# These strings are used in the palette variables
152+
# You can't test this without an account
153+
cmap1 = Colormap(["red", "green", "blue"])
154+
alt = cmap1.to_gee()
155+
assert isinstance(alt, list) and all(isinstance(c, str) for c in alt)
156+
assert alt[0] == "FF0000"
157+
assert alt[-1] == "0000FF"
158+
159+
150160
def test_viscm(tmp_path: Path) -> None:
151161
pytest.importorskip("viscm")
152162
# NOT using importorskip here because there IS an error import viscm

0 commit comments

Comments
 (0)