Files
MeshTerm/tests/test_node_spectrum.py
T
Jean-Pierre MartineauandClaude Opus 5.5 598e53b488 Node hues share one OKLab lightness, so every name and chip reads alike
The spectrum was an HSV wheel at one saturation and value, which is not
one lightness to the eye: blue names sat at OKLab L 0.55 and yellow at
0.93. A chip's fill is its label at CHIP_LIGHTNESS of that lightness, so
chip contrast followed the label, from APCA Lc 19 on blue to 64 on
yellow.

Each byte now keeps its HSV hue angle and chroma at OKLab L 0.72, with
the chroma fitted where the gamut cannot hold it. JP chose 0.72 on a
ladder of wheels from 0.70 to 0.85. Every name reads equally light and
every chip lands at Lc 34 to 41.

The 256 colours are written out as _NODE_SPECTRUM, because computing
them costs about 14 ms here and _IDENTITY_STYLES needs all of them at
import, which would slow every start on the handhelds. A test holds the
table to _spectrum_hex, and checks one lightness, 256 distinct hues, no
console-slot collision, and that each hue angle is kept.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 01:05:13 -04:00

48 lines
2.0 KiB
Python

# SPDX-License-Identifier: Apache-2.0
"""Tests for the spectrum of node hues in ``ui/theme.py``: one lightness for each hue."""
from __future__ import annotations
import pytest
from meshterm.ui import oklab, theme
def test_the_written_spectrum_is_the_calculated_one() -> None:
"""The fixed table agrees with :func:`theme._spectrum_hex` for each first key byte.
If this test fails, a constant of the spectrum changed and the table did not. Write the
table again from ``_spectrum_hex``.
"""
assert len(theme._NODE_SPECTRUM) == 256
assert list(theme._NODE_SPECTRUM) == [theme._spectrum_hex(byte) for byte in range(256)]
def test_every_hue_has_the_same_lightness() -> None:
"""Each hue has :data:`theme._NODE_LIGHTNESS` to the eye, within the rounding to 8 bits."""
for colour in theme._NODE_SPECTRUM:
assert oklab.from_hex(colour)[0] == pytest.approx(theme._NODE_LIGHTNESS, abs=0.005)
def test_every_hue_is_its_own_and_none_is_a_console_slot() -> None:
"""The 256 hues are different, and the spectrum never makes a colour of the console.
``is_identity_style`` holds both vocabularies in one set. Thus a spectrum hue that is
also a console slot would make the two vocabularies collide.
"""
assert len(set(theme._NODE_SPECTRUM)) == 256
assert not set(theme._NODE_SPECTRUM) & set(theme._NODE_SLOT_HEXES)
def test_a_node_keeps_the_hue_angle_of_the_hsv_wheel() -> None:
"""Only the lightness and the chroma change. The hue angle of each byte is as before."""
import colorsys
import math
for byte in range(0, 256, 8):
r, g, b = colorsys.hsv_to_rgb(byte / 256, theme._NODE_HUE_SAT, theme._NODE_HUE_VAL)
_, a0, b0 = oklab.from_hex(f"#{round(r * 255):02x}{round(g * 255):02x}{round(b * 255):02x}")
_, a1, b1 = oklab.from_hex(theme._NODE_SPECTRUM[byte])
turn = math.remainder(math.atan2(b1, a1) - math.atan2(b0, a0), math.tau)
assert abs(math.degrees(turn)) < 3, f"{byte:02x}"