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
This commit is contained in:
1 parent
80d0039148
commit
31b27a0852
4 files changed
+204
-11
No files matched your search
+7
-3
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user