Skip to content

Add TileLayerColorFilter plugin - #2381

Open
StrikeRougeAgent wants to merge 1 commit into
python-visualization:mainfrom
StrikeRougeAgent:feat/plugin-tile-layer-color-filter
Open

StrikeRougeAgent wants to merge 1 commit into
python-visualization:mainfrom
StrikeRougeAgent:feat/plugin-tile-layer-color-filter

Conversation

@StrikeRougeAgent

Copy link
Copy Markdown

Summary

Adds folium.plugins.TileLayerColorFilter, a thin wrapper around the
Leaflet.TileLayer.ColorFilter
plugin, so a tile layer can be painted through CSS filters:

m = folium.Map(tiles=None)
folium.plugins.TileLayerColorFilter(
    ["invert:100%", "hue-rotate:180deg", "brightness:95%", "contrast:90%"],
    name="dark",
).add_to(m)

Closes #1635, which asks for dark mode by wrapping exactly this plugin. A CSS
filter covers the wider request in that thread too: "grayscale:100%" for a
neutral background under a choropleth, or a contrast/saturate tweak on an
imagery basemap, without a second tile provider.

Files:

  • folium/plugins/tile_layer_color_filter.py — the plugin class
  • folium/plugins/__init__.py — export
  • tests/plugins/test_tile_layer_color_filter.py — 11 tests
  • docs/user_guide/plugins/tile_layer_color_filter.md plus its plugins.rst
    toctree entry and table row

Why this path

  1. Why wrap a plugin at all. folium has no way to post-process tile pixels.
    A CSS filter is the only client-side mechanism that works for any tile source,
    and this plugin is the maintained implementation of it (101 stars, v2.0.0
    released 2025-09).
  2. Why plugin version 2.0.0 and not 1.2.5. v1.2.5 (2018) exposes a separate
    L.tileLayer.colorFilter(url, {filter: "..."}) factory that only exists for
    Leaflet 1.x and has had no release since. v2.0.0 is current and still supports
    Leaflet 1.x through its -global build, which is what folium pins
    (leaflet@1.9.4). The script is pinned to an exact version and loaded from
    jsDelivr; note the npm package name is leaflet.tilelayer.colorfilter
    (dots) — there is no leaflet-tilelayer-colorfilter package.
  3. Why subclass TileLayer instead of emitting a custom factory. v2 patches
    L.TileLayer.prototype.onAdd and reads the colorFilter layer option, so the
    wrapper can keep folium's stock L.tileLayer(...) template and only has to
    get the plugin <script> into the page header before the map script. That is
    the same shape as the existing HeatMap(JSCSSMixin, Layer) and
    MarkerCluster(JSCSSMixin, Layer) plugins, and it keeps every TileLayer
    argument (tiles, min_zoom, max_zoom, attr, detect_retina, tms,
    opacity, controls) working unchanged.
  4. Why the filters are validated in Python. The plugin silently drops a
    filter it cannot parse, so a typo ("invert:100deg", "grey:100") produces a
    map that looks unfiltered with no error anywhere. The wrapper raises
    ValueError listing the valid filter names, accepts the plugin's short names
    (inv, gray, hue-rotation, ...) and appends the unit, so the rendered
    options are always canonical. Passing colorFilter=[...] as a TileLayer
    kwarg would technically work today, but it still needs the user to add the
    script link by hand and still fails silently.

Validation

RED (default branch dc7bf7c, before the change):

  • folium.plugins.TileLayerColorFilter → AttributeError: module 'folium.plugins' has no attribute 'TileLayerColorFilter'
  • a map built with TileLayer(..., colorFilter=["invert:100%", "hue-rotate:180deg"])
    renders the option but no plugin script, and in a browser the tile container is
    class="leaflet-layer " style="z-index: 1; opacity: 1;" — no filter.

GREEN (this branch), same map through TileLayerColorFilter, rendered in
headless Chromium 156 with Leaflet 1.9.4 from the CDN folium pins and real OSM
tiles:

class="leaflet-layer " style="z-index: 1; filter: invert(100%) hue-rotate(180deg) brightness(95%) contrast(90%); opacity: 1;"

Screenshots from those two runs: standard light OSM tiles before, dark tiles
(teal water, black land) after, tiles fully loaded in both cases.

Test suite — python -m pytest --ignore=tests/selenium --ignore=tests/playwright --ignore=tests/snapshots
(the Code Tests invocation):

tree result
dc7bf7c (before) 3 failed, 430 passed
this branch 3 failed, 442 passed

The 3 failures are tests/test_repr.py::test__repr_png_is_bytes,
::test_valid_png, ::test_valid_png_size, which need a Chrome driver and fail
the same way on the unmodified branch in this environment. The new tests cover
the rendered colorFilter options, that the plugin script is loaded after
Leaflet, that ordinary TileLayers get no plugin script and no colorFilter
key, alias/unit normalisation, and the validation errors.

Lint — ruff 0.17.5, black 26.10.1 and codespell (the exact
.pre-commit-config.yaml revs) all clean on the changed files.

Limitations

  • tests/snapshots, tests/selenium and the Sphinx/nbsphinx docs build were not
    run locally: the first two need a Chrome driver, the docs build needs the full
    docs stack. The two docs code cells were executed as plain Python instead.
  • The plugin's runtime updateColorFilter() method is not wrapped; filters are
    fixed at render time. Adding a JS-side updater would need a new folium element
    and is out of scope here.
  • CSS filters apply to the tile images only, not to markers, vector layers or
    controls drawn on top of them — that is the intended behaviour and is
    documented, but it means a "fully dark" map also needs dark control styling.
  • Filter rendering depends on browser CSS filter support (all current
    browsers).
  • AI assistance was used in preparing this contribution (analysis, patch, tests,
    docs).

Wraps Leaflet.TileLayer.ColorFilter so a tile layer can be painted with CSS
filters, which turns a single light basemap into a dark or grayscale one
instead of requiring a separate dark tile provider.

Refs python-visualization#1635

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dark mode feature by wrapping Leaflet.TileLayer.ColorFilter plugin

1 participant