Files
xscapes/internal/scape/palette_cube_test.go
donlucasandClaude Fable 5.1 e4faac8125 The sky and the sea are painted as one path through the palette, not rounded row by row
His pick from the four options was cube-path gradients, and only that. A
ramp is now the cheapest walk through the 240 fixed 256-colour entries from
its quantised start to its quantised end: squared CIE76 steps, an off-line
charge along the way, and a chroma-scaled charge for flattening a strong
channel lead into a stripe. Measured on the rendered frame at his geometry,
all 48 half-hours: hard edges sky 84 to 65, sea 120 to 99; half-hours with
a step of 30 or over 29 to 6; the largest step unchanged at 33, the cube's
own green step, which no path gets under. Two half-hours one edge worse.

The audit grew a -dump flag, a hard-edge column and a "before" frame per
hour. Its first before/after came out identical because it flipped the
global switch back on after the before frame; fixed, and the before run is
proven against the pre-change table.

He saw the page ("Sky and Sea Repainted") and said "Install it".

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-04 00:13:50 -07:00

305 lines
11 KiB
Go

package scape
import (
"testing"
"github.com/donlucasx/xscapes/internal/canvas"
"github.com/donlucasx/xscapes/internal/term"
)
// These tests exist because the defect they catch was invisible to every test
// in the repo and to every HTML preview, which render true RGB. It only shows
// up on the terminal Lucas actually uses.
func lumaOf(c term.RGB) float64 {
return 0.30*float64(c.R) + 0.59*float64(c.G) + 0.11*float64(c.B)
}
// daylight is the window the scape is looked at in: 07:00 to 19:00.
func daylight(t float64) bool { return t >= 7.0/24 && t <= 19.0/24 }
// The sky and the sea must still be COLOURED on a 256-colour terminal.
//
// Measured on the palette this replaces: across 48 half-hours SeaFar landed on
// the greyscale ramp at 40 of them and SkyTop at 36, so for most of a working
// day the two biggest regions on screen had no colour at all on Terminal.app --
// while every HTML preview in the repo showed them blue, because those render
// true RGB. The picture Lucas saw and the picture the tests saw were different
// pictures.
//
// The rule is not "never grey". A BRIGHT grey is haze and a real sky has one;
// the night is monochrome on purpose and the cube gives no alternative. What
// must not happen is a DARK grey standing in for a colour during the hours the
// scape is looked at.
func TestSkyAndSeaKeepTheirColourThroughTheWorkingDay(t *testing.T) {
type field struct {
name string
get func(Palette) term.RGB
}
fields := []field{
{"SkyTop", func(p Palette) term.RGB { return p.SkyTop }},
{"SkyHorizon", func(p Palette) term.RGB { return p.SkyHorizon }},
{"SeaFar", func(p Palette) term.RGB { return p.SeaFar }},
{"SeaNear", func(p Palette) term.RGB { return p.SeaNear }},
}
for _, f := range fields {
lost := 0
var worst float64 = 999
worstAt := 0.0
for i := 0; i < 48; i++ {
tod := float64(i) / 48
if !daylight(tod) {
continue
}
c := f.get(PaletteAt(tod))
idx := c.Index256()
shown := term.FromIndex256(idx)
if idx >= 232 && lumaOf(shown) < 140 {
lost++
if lumaOf(shown) < worst {
worst, worstAt = lumaOf(shown), tod
}
t.Logf("%s at %05.2f: asked for (%d,%d,%d), terminal shows grey %d",
f.name, tod*24, c.R, c.G, c.B, shown.R)
}
}
if lost > 0 {
t.Errorf("%s falls to a DARK grey at %d of the 25 daylight half-hours (worst: grey %.0f at %05.2f) -- the colour is gone on the target terminal",
f.name, lost, worst, worstAt*24)
}
}
}
// A gradient has to survive quantisation as a gradient.
//
// The sea's depth ramp used to collapse: at noon three different blues all
// landed on rgb(0,95,135) and eleven of the sea's fifteen rows were the same
// cell. Endpoints one cube step apart cannot do better than two bands, because
// the midpoint rounds back onto an end -- so the pair has to be at least two
// steps apart in some channel, and this is what checks it.
//
// It reads the rendered frame rather than recomputing the ramp, so a change to
// how the sea is painted is caught too, not just a change to the two colours.
func TestTheSeaShowsItsDepthOn256(t *testing.T) {
for _, tod := range []float64{0.375, 0.5, 0.625} {
c := canvas.New(120, 40, canvas.AlphaFar, canvas.AlphaMid, canvas.AlphaNear)
sh := NewShore(7, false)
sh.MoonX = 0.28
for i := 0; i < 12; i++ {
sh.Update(c, 2+float64(i)/20, Activity{
Working: true, Level: 0.6, TimeOfDay: tod, ContextUsed: 0.3})
}
hy := 0
drop := 0.0
var lum []float64
var idx []int
for y := 0; y < c.H; y++ {
bg := c.BGAt(2, y)
idx = append(idx, bg.Index256())
lum = append(lum, lumaOf(term.FromIndex256(bg.Index256())))
}
for y := 1; y < c.H*3/4; y++ {
if d := lum[y-1] - lum[y]; d > drop {
drop, hy = d, y
}
}
band := c.H
for y := c.H - 1; y > hy; y-- {
if idx[y] != idx[c.H-1] {
band = y + 1
break
}
}
seen := map[int]bool{}
run, worst := 0, 0
prev := -1
for y := hy; y < band; y++ {
seen[idx[y]] = true
if idx[y] == prev {
run++
} else {
run = 1
}
if run > worst {
worst = run
}
prev = idx[y]
}
rows := band - hy
t.Logf("tod %.3f: sea is %d rows, %d distinct colours, longest flat run %d",
tod, rows, len(seen), worst)
if len(seen) < 4 {
t.Errorf("tod %.3f: the sea shows only %d distinct colours over %d rows -- the depth ramp has collapsed",
tod, len(seen), rows)
}
if worst > rows/2 {
t.Errorf("tod %.3f: %d of the sea's %d rows are one colour -- that is a band, not a ramp",
tod, worst, rows)
}
}
}
// A blue with no green lead is a violet, and the terminal will hand you one.
//
// Chasing band count alone produced exactly that: #005faf to #d7d7ff renders
// ten distinct blues instead of eight and puts rgb(175,175,255) across the
// middle of the sky, where red has caught green up. On screen it is a lavender
// stripe in a blue sky, and no count of distinct colours can see it -- the band
// is as distinct as any other.
//
// It reads the RENDERED frame. The first version recomputed the ramp from the
// two palette colours, which made it blind to how the sky is actually painted:
// a change to the ramp's shape in paintBG moved nothing in it at all.
func TestTheSkyNeverGoesViolet(t *testing.T) {
for i := 0; i < 48; i++ {
tod := float64(i) / 48
if !daylight(tod) {
continue
}
c := canvas.New(80, 26, canvas.AlphaFar, canvas.AlphaMid, canvas.AlphaNear)
sh := NewShore(7, false)
sh.MoonX = 0.28
sh.Update(c, 3.0, Activity{Working: true, Level: 0.5, TimeOfDay: tod, ContextUsed: 0.3})
// The sky is everything above the biggest downward step in luma.
hy, drop := 0, 0.0
for y := 1; y < c.H*3/4; y++ {
if d := lumaOf(c.BGAt(2, y-1)) - lumaOf(c.BGAt(2, y)); d > drop {
drop, hy = d, y
}
}
for y := 0; y < hy; y++ {
want := c.BGAt(2, y)
// What the renderer actually paints, not what a quantiser called
// here would. Naming a function in a test is how a test ends up
// checking the path nothing uses.
_, _, got := c.ResolveAt(2, y, term.Profile256)
lo := got.R
if got.G < lo {
lo = got.G
}
// Only judge colours the terminal paints as STRONGLY blue. A warm
// dawn horizon has blue at its minimum and is not this test's
// business, and a pale blue-grey that loses a little green is a
// tint rather than a hue error. 60 is where the cast stops being a
// shade of the sky and starts being lavender.
if int(got.B)-int(lo) < 60 {
continue
}
if int(got.G)-int(got.R) < 20 {
t.Errorf("%05.2f, sky row %d of %d: asked for (%d,%d,%d), terminal shows (%d,%d,%d) -- blue with no green lead is violet",
tod*24, y, hy, want.R, want.G, want.B, got.R, got.G, got.B)
}
}
}
}
// The night must NOT be saturated on 256, and this test exists because the
// hue-preserving quantiser made it so and it shipped.
//
// Grey has no channel ordering, so it fails an ordering check by definition.
// Index256Keeping saw that, went looking for a cube colour that would pass, and
// in the dark the only candidates are the pure #0000xx column -- so a midnight
// sky came out in saturated navy over a teal sea, which is exactly the
// "electric royal blue that swallows the moon" that Shore.BlueSky records as
// rendered and rejected. Nothing caught it: every test was about colour being
// PRESENT, and none about it being absent on purpose.
//
// Washing out and turning the wrong colour are different failures, and only one
// of them is a bug.
func TestTheNightStaysMonochromeOn256(t *testing.T) {
chroma := func(c term.RGB) int {
mx, mn := int(c.R), int(c.R)
for _, v := range []int{int(c.G), int(c.B)} {
if v > mx {
mx = v
}
if v < mn {
mn = v
}
}
return mx - mn
}
for _, tod := range []float64{0.0, 0.02, 0.04, 0.06, 0.92, 0.96, 0.99} {
c := canvas.New(80, 26, canvas.AlphaFar, canvas.AlphaMid, canvas.AlphaNear)
sh := NewShore(7, false)
sh.MoonX = 0.28
sh.Update(c, 3.0, Activity{Working: true, Level: 0.5, TimeOfDay: tod, ContextUsed: 0.3})
// Sky and sea only. The beach below them is a warm brown by design --
// sandNight is #875f00, chroma 135 -- and measuring it would fail this
// test on the very thing it is meant to protect. The beach is found by
// its colour rather than by SandTop(), which sits a row above the dry
// sand and let the first brown row in.
_, _, floor := c.ResolveAt(2, c.H-1, term.Profile256)
beach := c.H
for y := c.H - 1; y > 0; y-- {
if _, _, bg := c.ResolveAt(2, y, term.Profile256); bg != floor {
beach = y + 1
break
}
}
worst, at := 0, 0
for y := 0; y < beach; y++ {
_, _, bg := c.ResolveAt(2, y, term.Profile256)
if ch := chroma(bg); ch > worst {
worst, at = ch, y
}
}
t.Logf("tod %.2f: the most saturated background row is %d, chroma %d", tod, at, worst)
// 60 is comfortably above the greyscale ramp, which is 0, and far below
// the pure-blue column, which runs 95 to 255.
if worst > 60 {
t.Errorf("tod %.2f: a night background row reads chroma %d at row %d -- the night has gone electric",
tod, worst, at)
}
}
}
// The sky's hue must not wander back and forth on the way down.
//
// A ramp can be perfectly monotonic in luma and still stripe: down a noon sky
// the greenness of the painted colour ran 95, 95, 95, 95, 135, 135, 135, 40,
// 40... -- three rows of cyan in the middle of a blue sky, then back to blue.
// It happens because the cube's first step is 95 wide and the rest are 40, so
// red, starting at 0, always crosses its levels later than green does. No band
// count can see it: the cyan band is exactly as distinct as the good ones.
//
// Perfect monotonicity is not available -- it would need red to start at 95,
// which measured as a paler sky with fewer than half the bands. So this bounds
// the wandering rather than forbidding it, and it is proven red against the
// unweighted quantiser, which scores 5 reversals at his window size.
//
// Read from the RENDERED frame at his geometry, 24 half-rows of sky. It used
// to call the quantiser on the palette directly, which stopped being what the
// renderer does the day the sky became a path (term.Ramp); a test naming a
// function checks the path nothing uses.
func TestTheSkyHueDoesNotWander(t *testing.T) {
for i := 0; i < 48; i++ {
tod := float64(i) / 48
if !daylight(tod) {
continue
}
c, hy, _ := skyAndSea(tod)
prev, dir, dips := -999, 0, 0
for _, q := range tonesDown(c, 2)[:2*hy] {
green := int(q.G) - int(q.R)
if prev != -999 && green != prev {
d := 1
if green < prev {
d = -1
}
if dir != 0 && d != dir {
dips++
}
dir = d
}
prev = green
}
if dips > 3 {
t.Errorf("%05.2f: the sky's hue reverses %d times down 24 rows -- that is a stripe, not a gradient",
tod*24, dips)
}
}
}