From 31b27a0852856a6f6b3a509be9cbd442d14bf934 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Mon, 23 Nov 2026 16:23:35 +0100 Subject: [PATCH] Night map palette instead of inverting it: dark blue-gray ground, roads a step lighter, muted ochre motorways, light-gray labels on dark halos, switched live with setPaintProperty (NightMap); raster overlays dimmed, own track/point colors kept; the UI panels keep the invert filter for now --- TODO.adoc | 3 +- public/css/style.css | 17 ++-- public/js/map.js | 10 ++- public/js/map_themes.js | 185 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 204 insertions(+), 11 deletions(-) diff --git a/TODO.adoc b/TODO.adoc index 38082b3..e8f9a19 100644 --- a/TODO.adoc +++ b/TODO.adoc @@ -92,7 +92,8 @@ plan from — the server does the heavy lifting. The UI is built for a mouse tod * [ ] A touch substitute for the Alt+click debug inspector (e.g. long-press) * [x] Side panels as an overlay in portrait instead of a pinned column — under 640 px the pin is ignored and the panel floats over the map at full width (a pinned 300 px panel left the phone a sliver of map); a real bottom sheet still open * [ ] Driving mode (field test in the car, 2026-11): tiny icons and lists can't be hit while driving — a separate big-button UI for the few things needed on the road (navigate/follow, night mode, zoom, layer presets) rather than enlarging every planning panel -* [x] Night mode — topbar button cycles auto (sun below the horizon at the map center) / on / off; for now one invert+dim filter over the whole page, a real dark map palette later if the inverted colors don't hold up +* [x] Night mode — topbar button cycles auto (sun below the horizon at the map center) / on / off. The map has its own night palette (`NightMap` in `public/js/map_themes.js`, switched live with setPaintProperty): dark blue-gray ground, roads a step lighter, muted ochre motorways, light-gray labels on dark halos; raster overlays dimmed, our own track/point colors kept +** [ ] The rest of the UI is still one invert+dim filter — CSS color tokens and a real dark set instead * [x] Tracker as a top-down vehicle (car / motorbike / bicycle from its icon) rotated by the GNSS course, kept while standing still; "Navigate" (tracker card or Overlays status) follows it heading-up, tilted 60°, vehicle in the lower third — panning by hand ends it == NPS parks & maps diff --git a/public/css/style.css b/public/css/style.css index cc67d9a..1d2365a 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -3137,18 +3137,21 @@ body.side-pinned:has(.basemap-panel:not([hidden]), .gpx-panel:not([hidden])) .pr background: #c0392b; } -/* Night mode (map.js): the whole page inverted and dimmed, map included, - the already-black topbar and photos inverted back. - ponytail: one filter instead of a dark map style + dark copy of this - stylesheet; colors come out approximate (hue kept, lightness flipped) — - a real dark Park Tiles palette in map_themes.js if that's not enough. */ +/* Night mode (map.js): the map gets its own night palette (NightMap in + map_themes.js), so its canvas and markers are inverted back here; the + rest of the UI is still the whole page inverted and dimmed, with the + already-black topbar and photos inverted back. + ponytail: the UI part is one filter, not a dark copy of this stylesheet; + colors come out approximate — CSS color tokens + a dark set later. */ html.night { - filter: invert(1) hue-rotate(180deg) brightness(0.75); + filter: invert(1) hue-rotate(180deg) brightness(0.8); background: #fff; } html.night .topbar, -html.night img { +html.night img, +html.night #map canvas, +html.night .maplibregl-marker { filter: invert(1) hue-rotate(180deg); } diff --git a/public/js/map.js b/public/js/map.js index 32ec965..5941dac 100644 --- a/public/js/map.js +++ b/public/js/map.js @@ -23,7 +23,7 @@ import { getPosition } from "/vendor/suncalc/suncalc.js"; import { notifySidePanelOpen, onlyOneSidePanelOpen, setupSidePanelModes } from "/js/panels.js"; import { isLoggedIn } from "/js/auth.js"; import { DebugControl } from "/js/debug.js"; -import { patchOnlineStyle } from "/js/map_themes.js"; +import { patchOnlineStyle, NightMap } from "/js/map_themes.js"; import { offerSharedFiles } from "/js/share_import.js"; import HomeButton from "/vendor/npmap-home-button/home-button.esm.js"; import OverviewMap from "/vendor/npmap-overview/overview.esm.js"; @@ -326,9 +326,10 @@ class RotationLockControl { } } +const initialStyle = onlineStyle(); const map = new MaplibreMap({ container: "map", - style: onlineStyle(), + style: initialStyle, center: CENTER, zoom: ZOOM, // MapLibre's built-in Hash control: keeps #map=zoom/lat/lon in the URL in @@ -471,6 +472,8 @@ setInterval(updateSun, 60000); // topbar button cycles the three, remembered per device. const NIGHT_MODES = { auto: ["◐", "Night mode: automatic (after sunset)"], on: ["☾", "Night mode: on"], off: ["☀", "Night mode: off"] }; const nightButton = document.querySelector(".topbar__night"); +const nightMap = new NightMap(map); +nightMap.base(initialStyle); let nightMode = "auto"; try { nightMode = localStorage.getItem("nightMode") || "auto"; @@ -479,6 +482,7 @@ const updateNight = () => { const { lng, lat } = map.getCenter(); const dark = nightMode === "on" || (nightMode === "auto" && getPosition(new Date(), lat, lng).altitude < 0); document.documentElement.classList.toggle("night", dark); + nightMap.set(dark); [nightButton.textContent, nightButton.title] = NIGHT_MODES[nightMode] || NIGHT_MODES.auto; }; nightButton.addEventListener("click", () => { @@ -540,7 +544,7 @@ map.addControl(mapLayersButton, "top-left"); function applyStyle(name) { layerToggle.setActive(name); - map.setStyle(name === "offline" ? offlineStyle() : onlineStyle()); + map.setStyle(nightMap.base(name === "offline" ? offlineStyle() : onlineStyle())); } // If a local PMTiles file is present, prefer it as the default style — its diff --git a/public/js/map_themes.js b/public/js/map_themes.js index 8cad8a3..150922a 100644 --- a/public/js/map_themes.js +++ b/public/js/map_themes.js @@ -111,3 +111,188 @@ function insertTrackOnlyLayer(style) { style.layers.splice(index + 1, 0, trackOnly); return style; } + +// --- Night palette ---------------------------------------------------------- +// What car navigations do instead of inverting: a dark blue-gray ground (not +// black), low contrast, roads a step lighter than the ground, motorways a +// muted ochre, light-gray labels on dark halos; rasters dimmed, not inverted. +// Switched live with setPaintProperty (no setStyle), so overlays stay put. +// The main layers have hand-picked colors; every other base-style color goes +// through nightColor(), and overlays only get their rasters dimmed and their +// labels' halos darkened — their own colors are the point of them. + +const NIGHT_GROUND = "#1d2329"; +const NIGHT_LABEL = { "text-color": "#a8b0b8", "text-halo-color": NIGHT_GROUND }; +const NIGHT_WATER_LABEL = { "text-color": "#6f8fa3", "text-halo-color": NIGHT_GROUND }; +const NIGHT_CASING = { "line-color": "#121619" }; + +const NIGHT_PATCH = { + background: { "background-color": NIGHT_GROUND }, + landuse_residential: { "fill-color": "#20262c" }, + landcover_wood: { "fill-color": "#1f2a24" }, + landcover_grass: { "fill-color": "#222a25" }, + landcover_wetland: { "fill-color": "#1f2a2a" }, + landcover_ice: { "fill-color": "#2a3036" }, + landcover_sand: { "fill-color": "#2a2823" }, + water: { "fill-color": "#12202b" }, + waterway_river: { "line-color": "#1a3040" }, + waterway_other: { "line-color": "#1a3040" }, + park: { "fill-color": "#1f2b22", "fill-outline-color": "#1f2b22" }, + park_outline: { "line-color": "#4f6b3a" }, + park_tintband: { "line-color": "#2b3a26" }, + building: { "fill-color": "#262c32", "fill-outline-color": "#30373e" }, + boundary_2: { "line-color": "#4a4f55" }, + boundary_3: { "line-color": "#3a3f45" }, + road_motorway: { "line-color": "#6b5a35" }, + road_trunk_primary: { "line-color": "#6b5a35" }, + road_secondary_tertiary: { "line-color": "#4a5058" }, + road_link: { "line-color": "#4a5058" }, + road_motorway_link: { "line-color": "#4a5058" }, + road_minor: { "line-color": "#3a4047" }, + road_service_track: { "line-color": "#343a40" }, + road_track_only: { "line-color": "#7a6440" }, + road_path_pedestrian: { "line-color": "#5a5650" }, + road_motorway_casing: NIGHT_CASING, + road_trunk_primary_casing: NIGHT_CASING, + road_secondary_tertiary_casing: NIGHT_CASING, + road_link_casing: NIGHT_CASING, + road_motorway_link_casing: NIGHT_CASING, + road_minor_casing: NIGHT_CASING, + road_service_track_casing: NIGHT_CASING, + water_name_point_label: NIGHT_WATER_LABEL, + water_name_line_label: NIGHT_WATER_LABEL, + waterway_line_label: NIGHT_WATER_LABEL, + "highway-name-major": NIGHT_LABEL, + "highway-name-minor": NIGHT_LABEL, + "highway-name-path": NIGHT_LABEL, +}; + +// tunnel_*/bridge_* layers reuse their surface road's colors. +function nightPatchFor(id) { + const match = id.match(/^(tunnel|bridge)_(.+)$/); + if (!match) return NIGHT_PATCH[id]; + const alias = { street: "minor", street_casing: "minor_casing" }[match[2]] ?? match[2]; + return NIGHT_PATCH[`road_${alias}`]; +} + +// The color paint properties per layer type, and how each darkens. +const COLOR_PROPS = { + background: { "background-color": "area" }, + fill: { "fill-color": "area", "fill-outline-color": "area" }, + line: { "line-color": "area" }, + "fill-extrusion": { "fill-extrusion-color": "area" }, + circle: { "circle-color": "area", "circle-stroke-color": "area" }, + symbol: { "text-color": "text", "text-halo-color": "halo" }, + "color-relief": { "color-relief-color": "area" }, +}; + +let probe = null; + +// Any CSS color → [r, g, b, a] (the browser parses it), null if not a color. +function parseColor(value) { + probe ??= document.createElement("canvas").getContext("2d"); + probe.fillStyle = "#010203"; + probe.fillStyle = value; + const out = probe.fillStyle; + if (out === "#010203" && value.toLowerCase() !== "#010203") return null; + const hex = out.match(/^#(..)(..)(..)$/); + if (hex) return [...hex.slice(1).map((h) => parseInt(h, 16)), 1]; + const [r, g, b, a = 1] = out.match(/\(([^)]+)\)/)[1].split(",").map(Number); + return [r, g, b, a]; +} + +function toHsl([r, g, b]) { + [r, g, b] = [r / 255, g / 255, b / 255]; + const max = Math.max(r, g, b); + const min = Math.min(r, g, b); + const l = (max + min) / 2; + if (max === min) return [0, 0, l]; + const d = max - min; + const s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + const h = max === r ? (g - b) / d + (g < b ? 6 : 0) : max === g ? (b - r) / d + 2 : (r - g) / d + 4; + return [h * 60, s, l]; +} + +// The fallback for layers NIGHT_PATCH doesn't list: lightness flipped and +// squeezed into the dark end, colors desaturated; dark text goes light, +// halos become the ground. (Roads need the hand-picked colors: flipping +// would draw white roads darker than a cream ground.) +export function nightColor(value, role) { + const rgba = typeof value === "string" && parseColor(value); + if (!rgba) return value; + const alpha = rgba[3]; + if (role === "halo") return `rgba(29, 35, 41, ${alpha})`; + const [h, s, l] = toHsl(rgba); + const night = role === "text" ? [s * 0.5, 0.55 + (1 - l) * 0.25] : [s * 0.55, 0.08 + (1 - l) * 0.3]; + return `hsla(${h.toFixed(1)}, ${(night[0] * 100).toFixed(1)}%, ${(night[1] * 100).toFixed(1)}%, ${alpha})`; +} + +// Colors inside expressions too (interpolate stops, match outputs) and in +// legacy {stops: [[zoom, color], …]} functions, which Liberty still uses. +function nightValue(value, role) { + if (Array.isArray(value)) return value.map((v) => nightValue(v, role)); + if (value?.stops) return { ...value, stops: value.stops.map(([zoom, v]) => [zoom, nightValue(v, role)]) }; + return nightColor(value, role); +} + +export class NightMap { + constructor(map) { + this._map = map; + this._on = false; + this._base = new Set(); + this._day = new Map(); // layer id -> its day values of what night changed + map.on("styledata", () => this._on && this._apply()); + } + + // The base style's layers (passed through on its way to setStyle). + base(style) { + this._base = new Set(style.layers.map((layer) => layer.id)); + this._day.clear(); + return style; + } + + set(on) { + if (on === this._on) return; + this._on = on; + if (on) this._apply(); + else this._restore(); + } + + _apply() { + const ids = this._map.getLayersOrder(); + for (const id of this._day.keys()) if (!ids.includes(id)) this._day.delete(id); // removed overlays + for (const id of ids) { + if (this._day.has(id)) continue; + const night = this._nightFor(id); + const day = Object.fromEntries(Object.keys(night).map((prop) => [prop, this._map.getPaintProperty(id, prop)])); + try { + for (const [prop, value] of Object.entries(night)) this._map.setPaintProperty(id, prop, value); + } catch { + return; // a style still loading: its own styledata comes once it's done + } + this._day.set(id, day); + } + } + + _restore() { + for (const [id, props] of this._day) { + if (this._map.getLayer(id)) for (const [prop, value] of Object.entries(props)) this._map.setPaintProperty(id, prop, value); + } + this._day.clear(); + } + + _nightFor(id) { + const type = this._map.getLayer(id).type; + if (type === "raster") return { "raster-brightness-max": 0.6, "raster-saturation": -0.3 }; + if (type === "hillshade") return { "hillshade-highlight-color": "rgba(255, 255, 255, 0.06)" }; + const base = this._base.has(id); + if (!base && type !== "symbol" && type !== "color-relief") return {}; + const night = { ...(base && nightPatchFor(id)) }; + for (const [prop, role] of Object.entries(COLOR_PROPS[type] || {})) { + if (prop in night) continue; + const day = this._map.getPaintProperty(id, prop); + if (day !== undefined) night[prop] = nightValue(day, role); + } + return night; + } +}