From a9091006ebf07e1da9c30c2bbd32bc379d8c39a3 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Tue, 7 Jul 2026 18:41:24 -0400 Subject: [PATCH] Brighten map markers where nodes pile on one cell A braille cell shows a single glyph, so nodes landing on the same spot hid the crowd behind one marker. Count the nodes per cell and wash the surviving glyph toward white on a log2 ramp, so a bright marker reads as a busy cluster. Shape still encodes node type; only brightness encodes count. A lone node keeps its exact base colour. Co-Authored-By: Claude Opus 4.8 --- meshtools/ui/map_render.py | 34 +++++++++++++++++++++++++++++++++- tests/test_map.py | 26 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/meshtools/ui/map_render.py b/meshtools/ui/map_render.py index 6e51c53..4218dbb 100644 --- a/meshtools/ui/map_render.py +++ b/meshtools/ui/map_render.py @@ -13,6 +13,8 @@ important feature visible where things overlap (rivers over water, major roads o from __future__ import annotations +import math +from collections import Counter from dataclasses import dataclass, field from typing import Optional @@ -306,10 +308,36 @@ def _marker_style(marker: MapMarker) -> tuple[str, str]: return _NODE +#: How many piled nodes it takes for a marker's glyph to reach full brightness. +_PILE_FULL = 8 + + +def _pile_color(color: RGB, count: int, *, cap: int = _PILE_FULL) -> RGB: + """Brighten a marker's colour by how many nodes share its cell. + + One cell can only show a single glyph, so where many nodes fall on the same spot the + map would otherwise hide the crowd behind one marker. Instead we keep the top node's + glyph and wash its colour toward white as the pile grows, so a bright marker reads as + a busy cluster. The ramp is logarithmic (2 nodes → a clear lift, saturating around + ``cap``) so it stays informative without a lone extra node looking crowded. + """ + if count <= 1: + return color + t = min(1.0, math.log2(count) / math.log2(cap)) + r, g, b = color + return ( + round(r + (255 - r) * t), + round(g + (255 - g) * t), + round(b + (255 - b) * t), + ) + + def _draw_nodes(canvas: MapCanvas, viewport: Viewport, markers: list[MapMarker]) -> None: """Overlay mesh nodes: every glyph, then labels by importance until they collide. Glyphs are drawn lowest-priority first so self/repeaters land on top of leaf nodes. + Where several nodes share a cell the surviving glyph is brightened by the pile size + (:func:`_pile_color`) so crowded spots glow rather than silently hiding the crowd. Labels are then placed highest-priority first — self, then repeaters, then leaf nodes — each only if it fits without overlapping. So on a crowded map the important labels win the available space and the rest show as a bare marker (no overlap). @@ -322,9 +350,13 @@ def _draw_nodes(canvas: MapCanvas, viewport: Viewport, markers: list[MapMarker]) continue placed.append((marker, ix, iy)) + # A cell is (dot_x >> 1, dot_y >> 2); count how many nodes land on each. + pile = Counter((ix >> 1, iy >> 2) for _, ix, iy in placed) + for marker, ix, iy in sorted(placed, key=lambda p: p[0]._rank()): glyph, color = _marker_style(marker) - canvas.marker(ix, iy, glyph, parse_hex(color)) + count = pile[(ix >> 1, iy >> 2)] + canvas.marker(ix, iy, glyph, _pile_color(parse_hex(color), count)) for marker, ix, iy in sorted(placed, key=lambda p: -p[0]._rank()): _, color = _marker_style(marker) diff --git a/tests/test_map.py b/tests/test_map.py index 3e78ba0..2f55e16 100644 --- a/tests/test_map.py +++ b/tests/test_map.py @@ -231,6 +231,32 @@ def test_render_map_drops_crowded_labels_favouring_repeaters() -> None: assert ("NODEONE" in out) != ("NODETWO" in out) +def _glyph_color(lines: list[str], glyph: str) -> tuple[int, int, int]: + """Extract the truecolour ``(r, g, b)`` the given glyph was rendered with.""" + m = re.search( + r"38;2;(\d+);(\d+);(\d+)m(?:\x1b\[1m)?" + re.escape(glyph), "".join(lines) + ) + assert m, f"glyph {glyph!r} not found with a colour" + return int(m.group(1)), int(m.group(2)), int(m.group(3)) + + +def test_render_map_brightens_piled_markers() -> None: + """A cell many nodes share renders its glyph brighter than a lone node's.""" + from meshtools.ui.map_render import _NODE, MapMarker, render_map + + base = parse_hex(_NODE[1]) + vp = Viewport(45.50, -73.57, 14, 60, 40) + + lone = render_map(vp, {}, [MapMarker("solo", 45.50, -73.57)]) + assert _glyph_color(lone, "●") == base # a single node keeps its base colour + + crowd = [MapMarker(f"n{i}", 45.50, -73.57) for i in range(8)] + piled = _glyph_color(render_map(vp, {}, crowd), "●") + # Washed toward white: every channel is brighter than the base cyan. + assert all(p > b for p, b in zip(piled, base)) + assert piled != base + + def test_render_map_works_without_basemap() -> None: """With no tiles the nodes still render on a blank grid (the offline fallback).""" from meshtools.ui.map_render import MapMarker, render_map