Three changes to what is on the picture, and both drawings got all three. The line from an information box to its aircraft was drawn in the aircraft's own colour, which made it the same colour as that aircraft's trail: a straight solid line running out of an aeroplane, in the colour of the path behind the aeroplane, reads as more path, and on a busy picture that is a heading nobody flew. It has its own colour now, and is dashed. Qt measures a dash pattern in multiples of the pen's width, so each pass of the glow divides the pattern by its own width; without that the halo's dashes are three times the core's and the line comes out as beads. The animation had no such line at all, which only came out when the two were held up against each other: a label pushed into one of the outward rings by a crowd had nothing tying it to the aeroplane it was about. It has one now, walked along the line's own length rather than along whichever axis is longer, so that a nearly horizontal leader and a nearly vertical one get dashes of the same length and neither runs past the aircraft it points at. A red flag stands where the receiver was told it is standing, from the coordinates in the settings. The foot of the pole is the position and the pennant flies up and to the right, so nothing the flag is made of covers the place it points at. It is pure red in every theme: that is the one mark on the picture whose meaning must not change with the colours, and pure red is both the brightest red there is and the one furthest from every altitude colour -- a softer one sat close enough to a low aeroplane on the default map, and to a mid-altitude one on the red theme, to be taken for one. It is drawn only where a position was actually given, since a middle worked out from whatever flew past is not a place anybody is standing. And the range rings: faint discs at a quarter, a half and three quarters of the radius, concentric on the receiver and labelled with the distance. They are translucent and they stack, so the ground inside the innermost is lifted three times and the outer once, which gives a sense of how far away a thing is without measuring anything. An indexed picture cannot blend, so translucent there means moving the ground under the disc a step or two up its own ramp of shades, which keeps the coastline and the roads visible through it. Separate settings for the two, because a picture is studied and a window is glanced at. Two bugs found by the tests rather than by looking. The rings were built across the whole canvas instead of the part of it the map is drawn on, so they came out stretched by the height of the title bar -- four per cent, which is invisible and wrong. And a radius of zero killed the window: the projection collapses, every pixel maps to one point, and the graticule asks for lines across a span of nothing until Qt aborts. The program never passes a zero, but a caller could, and a crash is not an answer. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PsWPTweCT6pwxKngvVxcg
319 lines
13 KiB
Python
319 lines
13 KiB
Python
"""The colour themes, and the glow that goes with the vector ones.
|
|
|
|
A theme changes both drawings at once, because both read their colours out
|
|
of the same palette: the animation looks indices up in it directly and the
|
|
window asks it for a QColor. So the tests here are mostly about that
|
|
palette -- that it is rewritten rather than replaced, that every theme keeps
|
|
the aerodromes distinguishable from the aircraft, and that the halo goes
|
|
where light would go and nowhere else.
|
|
"""
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from bandsaunter import flightmap as fm
|
|
from bandsaunter import themes
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def back_to_night():
|
|
"""Every test leaves the program drawing the way it found it."""
|
|
yield
|
|
fm.set_theme(themes.DEFAULT_THEME)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Choosing one
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_every_theme_says_what_it_is():
|
|
for name, theme in themes.THEMES.items():
|
|
assert theme.name == name
|
|
assert theme.summary and not theme.summary.endswith(".")
|
|
assert len(theme.stops) >= 2
|
|
assert theme.stops[0][0] == 0.0
|
|
|
|
|
|
def test_a_theme_can_be_asked_for_by_name_or_by_what_it_looks_like():
|
|
assert themes.theme_named("phosphor").name == "phosphor"
|
|
assert themes.theme_named("green").name == "phosphor"
|
|
assert themes.theme_named("wargames").name == "digital"
|
|
assert themes.theme_named("BLUE").name == "digital"
|
|
assert themes.theme_named(" amber ").name == "amber"
|
|
|
|
|
|
def test_a_name_nobody_recognises_is_the_default_rather_than_a_refusal():
|
|
"""A theme is how the picture looks. Refusing to draw an evening's
|
|
flying because of a misspelt colour would be the wrong trade."""
|
|
assert themes.theme_named("puce").name == themes.DEFAULT_THEME
|
|
assert themes.theme_named("").name == themes.DEFAULT_THEME
|
|
assert themes.theme_named(None).name == themes.DEFAULT_THEME
|
|
|
|
|
|
def test_no_two_themes_share_a_name_or_an_alias():
|
|
seen = set()
|
|
for theme in themes.THEMES.values():
|
|
for word in (theme.name,) + tuple(theme.aliases):
|
|
assert word not in seen, word
|
|
seen.add(word)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# What setting one does to the palette
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_the_palette_is_written_over_rather_than_replaced():
|
|
"""Both drawings and every one of their helpers hold a reference to
|
|
this array. A new one would leave half the program painting in the
|
|
colours of the theme before."""
|
|
before = fm.PALETTE
|
|
fm.set_theme("phosphor")
|
|
assert fm.PALETTE is before
|
|
assert tuple(fm.PALETTE[fm.BG]) != (14, 16, 22)
|
|
|
|
|
|
def test_setting_a_theme_says_which_one_it_settled_on():
|
|
assert fm.set_theme("norad").name == "digital"
|
|
assert fm.set_theme("puce").name == themes.DEFAULT_THEME
|
|
|
|
|
|
def test_the_default_theme_draws_exactly_what_it_always_drew():
|
|
"""The colours this program has always used, unchanged: an existing
|
|
recording redrawn today has to come out the same picture."""
|
|
fm.set_theme("phosphor")
|
|
fm.set_theme("night")
|
|
assert tuple(fm.PALETTE[fm.BG]) == (14, 16, 22)
|
|
assert tuple(fm.PALETTE[fm.INK]) == (196, 204, 218)
|
|
assert tuple(fm.PALETTE[fm.RAMP]) == (252, 96, 72)
|
|
assert tuple(fm.PALETTE[fm.RAMP + fm.RAMP_STEPS - 1]) == (158, 142, 255)
|
|
|
|
|
|
def _lab(rgb):
|
|
c = np.asarray(rgb, float) / 255.0
|
|
c = np.where(c > 0.04045, ((c + 0.055) / 1.055) ** 2.4, c / 12.92)
|
|
m = np.array([[0.4124, 0.3576, 0.1805], [0.2126, 0.7152, 0.0722],
|
|
[0.0193, 0.1192, 0.9505]])
|
|
xyz = (c @ m.T) / np.array([0.9505, 1.0, 1.089])
|
|
f = np.where(xyz > 0.008856, np.cbrt(xyz), 7.787 * xyz + 16 / 116)
|
|
return np.array([116 * f[1] - 16, 500 * (f[0] - f[1]), 200 * (f[1] - f[2])])
|
|
|
|
|
|
def test_no_theme_lets_an_aerodrome_be_mistaken_for_an_aircraft():
|
|
"""The whole reason the aerodromes stopped being amber. Stated as the
|
|
distance rather than as the colour, so that a new theme cannot quietly
|
|
walk an aircraft back into the airports."""
|
|
for name in themes.THEMES:
|
|
fm.set_theme(name)
|
|
airport = _lab(fm.PALETTE[fm.AIRPORT])
|
|
apart = min(float(np.linalg.norm(airport - _lab(fm.PALETTE[fm.RAMP + i])))
|
|
for i in range(fm.RAMP_STEPS))
|
|
assert apart > 40, f"{name}: only {apart:.0f} units apart"
|
|
|
|
|
|
def test_a_phosphor_theme_reads_height_as_brightness():
|
|
"""One colour to spend, so it cannot be spent on hue. Low is dim and
|
|
high burns, which is the constraint those screens actually had."""
|
|
for name in ("digital", "phosphor", "amber", "red"):
|
|
theme = fm.set_theme(name)
|
|
assert theme.height_is_brightness
|
|
weights = [int(fm.PALETTE[fm.RAMP + i].astype(int).sum())
|
|
for i in range(fm.RAMP_STEPS)]
|
|
assert weights == sorted(weights), f"{name} is not monotonic"
|
|
assert weights[-1] > weights[0] * 3
|
|
|
|
|
|
def test_the_default_theme_reads_height_as_hue_instead():
|
|
fm.set_theme("night")
|
|
assert not fm.THEME.height_is_brightness
|
|
|
|
|
|
def test_every_theme_fills_the_palette_without_running_off_the_end():
|
|
for name in themes.THEMES:
|
|
fm.set_theme(name)
|
|
assert fm.PALETTE.shape == (256, 3)
|
|
assert fm.GLOW + 6 <= 256
|
|
# Nothing drawn with is left as the black the unused tail is.
|
|
for index in (fm.INK, fm.AIRPORT, fm.RAMP, fm.GROUND + 31):
|
|
assert fm.PALETTE[index].any(), (name, index)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# The glow
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _picture(width=80, height=60):
|
|
return np.full((height, width), fm.BG, dtype=np.uint8)
|
|
|
|
|
|
def test_the_default_theme_has_no_glow_at_all():
|
|
fm.set_theme("night")
|
|
img = _picture()
|
|
img[30, 10:70] = fm.RAMP + 20
|
|
assert np.array_equal(fm.bloom(img), img)
|
|
|
|
|
|
def test_a_line_on_a_vector_theme_gets_a_halo_either_side_of_it():
|
|
fm.set_theme("phosphor")
|
|
img = _picture()
|
|
img[30, 10:70] = fm.RAMP + 20
|
|
out = fm.bloom(img)
|
|
assert (out[30, 10:70] == fm.RAMP + 20).all(), "the core was painted over"
|
|
assert (out[29, 10:70] == fm.TRAIL + 20).all(), "no halo above the line"
|
|
assert (out[31, 10:70] == fm.TRAIL + 20).all(), "no halo below it"
|
|
assert (out[28, 10:70] == fm.OLD + 20).all(), "no second, fainter ring"
|
|
assert (out[27, 10:70] == fm.BG).all(), "the halo reaches too far"
|
|
|
|
|
|
def test_the_halo_is_the_colour_of_the_thing_that_cast_it():
|
|
"""A green aeroplane glows green and a red one red: the halo is the
|
|
aircraft's own dimmed colour, which is what a phosphor would spread."""
|
|
fm.set_theme("digital")
|
|
for step in (0, 12, 31):
|
|
img = _picture()
|
|
img[30, 10:70] = fm.RAMP + step
|
|
out = fm.bloom(img)
|
|
assert (out[29, 10:70] == fm.TRAIL + step).all()
|
|
|
|
|
|
def test_a_halo_never_paints_over_something_else_that_was_drawn():
|
|
"""A halo is what light does to the dark around a line. Painting it
|
|
over another line would be light doing something light does not do."""
|
|
fm.set_theme("phosphor")
|
|
img = _picture()
|
|
img[30, 10:70] = fm.RAMP + 20 # an aeroplane
|
|
img[29, 10:70] = fm.AIRPORT # an aerodrome right beside it
|
|
out = fm.bloom(img)
|
|
assert (out[29, 10:70] == fm.AIRPORT).all()
|
|
|
|
|
|
def test_the_halo_does_not_wrap_round_the_edge_of_the_picture():
|
|
"""Rolled and then cut: without the cut, a line down the left edge
|
|
would glow on the right edge of the picture."""
|
|
fm.set_theme("amber")
|
|
img = _picture()
|
|
img[:, 0] = fm.RAMP + 20
|
|
out = fm.bloom(img)
|
|
assert (out[:, 1] == fm.TRAIL + 20).all()
|
|
assert (out[:, -1] == fm.BG).all()
|
|
assert (out[:, -2] == fm.BG).all()
|
|
|
|
tall = _picture()
|
|
tall[0, :] = fm.AIRPORT
|
|
assert (fm.bloom(tall)[-1, :] == fm.BG).all()
|
|
|
|
|
|
def test_the_halo_goes_over_the_map_and_the_grid_but_not_the_aircraft():
|
|
fm.set_theme("digital")
|
|
img = np.full((60, 80), fm.GROUND + 10, dtype=np.uint8)
|
|
img[30, 40] = fm.INK
|
|
out = fm.bloom(img)
|
|
assert out[30, 41] != fm.GROUND + 10, "no halo over the map"
|
|
assert out[30, 40] == fm.INK
|
|
|
|
|
|
def test_the_nearer_ring_wins_where_two_meet():
|
|
"""Which is what happens on the tube as well."""
|
|
fm.set_theme("phosphor")
|
|
img = _picture()
|
|
img[30, 40] = fm.RAMP + 20
|
|
out = fm.bloom(img)
|
|
assert out[31, 40] == fm.TRAIL + 20 # near
|
|
assert out[32, 40] == fm.OLD + 20 # far
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# What else a vector theme changes
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_a_phosphor_theme_names_the_country_instead_of_drawing_its_flag():
|
|
"""A flag is half a dozen colours and a phosphor screen has one. Two
|
|
letters are what a display of the period would have done anyway."""
|
|
from bandsaunter.flags import FLAG_H, FLAG_W
|
|
|
|
fm.set_theme("night")
|
|
flagged = np.full((40, 60), fm.BG, dtype=np.uint8)
|
|
fm.draw_flag(flagged, 5, 5, "US")
|
|
patch = flagged[5:5 + FLAG_H, 5:5 + FLAG_W]
|
|
assert ((patch >= fm.FLAG) & (patch < fm.FLAG + 12)).any()
|
|
|
|
fm.set_theme("phosphor")
|
|
lettered = np.full((40, 60), fm.BG, dtype=np.uint8)
|
|
fm.draw_flag(lettered, 5, 5, "US")
|
|
patch = lettered[5:5 + FLAG_H, 5:5 + FLAG_W]
|
|
assert not ((patch >= fm.FLAG) & (patch < fm.FLAG + 12)).any()
|
|
assert (lettered == fm.DIM).any(), "the letters were not drawn either"
|
|
|
|
|
|
def test_a_vector_theme_pushes_the_map_underneath_well_back():
|
|
"""A tinted photograph of a county behind the vectors is the one thing
|
|
that stops a vector display looking like one."""
|
|
levels = np.full((8, 8), fm.GROUND_SHADES - 1, dtype=np.uint8)
|
|
fm.set_theme("night")
|
|
plain = int(fm.dim_ground(levels, 0.7).max())
|
|
fm.set_theme("digital")
|
|
quiet = int(fm.dim_ground(levels, 0.7).max())
|
|
assert quiet < plain * 0.6, f"{quiet} is not much darker than {plain}"
|
|
|
|
|
|
def test_the_brightness_setting_still_does_something_on_a_vector_theme():
|
|
levels = np.full((8, 8), fm.GROUND_SHADES - 1, dtype=np.uint8)
|
|
fm.set_theme("phosphor")
|
|
assert int(fm.dim_ground(levels, 1.0).max()) > \
|
|
int(fm.dim_ground(levels, 0.3).max())
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# The leader line, and the flag on the receiver
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_no_theme_lets_a_leader_line_be_mistaken_for_a_flight_path():
|
|
"""The whole reason it stopped being drawn in the aircraft's colour: a
|
|
straight line running out of an aeroplane, in the colour of the path
|
|
behind that aeroplane, reads as more path."""
|
|
for name in themes.THEMES:
|
|
fm.set_theme(name)
|
|
leader = _lab(fm.PALETTE[fm.LEADER])
|
|
apart = min(float(np.linalg.norm(leader - _lab(fm.PALETTE[fm.RAMP + i])))
|
|
for i in range(fm.RAMP_STEPS))
|
|
assert apart > 25, f"{name}: only {apart:.0f} units from a trail"
|
|
|
|
|
|
def test_the_receiver_flag_is_red_whatever_the_theme_is():
|
|
""""You are here" is the one mark on the picture whose meaning must not
|
|
change with the colours, so it does not take the theme's."""
|
|
for name in themes.THEMES:
|
|
fm.set_theme(name)
|
|
assert tuple(fm.PALETTE[fm.HOME]) == fm.HOME_RED
|
|
|
|
|
|
def test_the_flag_stands_clear_of_the_aircraft_on_every_theme():
|
|
"""Including the red one, which is the hard case and the reason the
|
|
flag is pure red rather than a softer one: a softened red sat close
|
|
enough to a low aeroplane on the default map, and to a mid-altitude one
|
|
on the red theme, to be taken for one."""
|
|
for name in themes.THEMES:
|
|
fm.set_theme(name)
|
|
red = _lab(fm.PALETTE[fm.HOME])
|
|
apart = min(float(np.linalg.norm(red - _lab(fm.PALETTE[fm.RAMP + i])))
|
|
for i in range(fm.RAMP_STEPS))
|
|
assert apart > 20, f"{name}: only {apart:.0f} units from an aircraft"
|
|
|
|
|
|
def test_the_flag_stands_on_the_spot_rather_than_covering_it():
|
|
"""The foot of the pole is the position. A blob would put the position
|
|
somewhere inside itself."""
|
|
img = np.full((40, 40), fm.BG, dtype=np.uint8)
|
|
fm.draw_home(img, 20, 34)
|
|
assert img[34, 20] == fm.HOME, "the pole does not stand on the spot"
|
|
assert img[35, 20] == fm.BG, "something is drawn below the position"
|
|
assert (img[34, :20] == fm.BG).all(), "the flag reaches left of the pole"
|
|
# The pennant flies up and to the right, and nothing else does.
|
|
top = 34 - fm.HOME_POLE
|
|
assert (img[top, 21:21 + fm.HOME_FLY] == fm.HOME).all()
|
|
assert (img[34 - 1, 21:] == fm.BG).all(), "the pennant hangs to the foot"
|
|
|
|
|
|
def test_the_flag_off_the_edge_of_the_picture_paints_nothing_absurd():
|
|
for x, y in ((-50, 20), (20, -50), (200, 20), (20, 200)):
|
|
img = np.full((40, 40), fm.BG, dtype=np.uint8)
|
|
fm.draw_home(img, x, y) # must not raise or wrap
|
|
assert img.shape == (40, 40)
|