mirror of
https://github.com/donlucasx/xscapes.git
synced 2026-10-20 08:43:32 +02:00
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>
276 lines
11 KiB
Go
276 lines
11 KiB
Go
// gradientaudit measures the 256-colour sky and sea gradients hour by hour, at
|
|
// a given geometry, and writes what it found as a table and as a page.
|
|
//
|
|
// He asked for smoother gradients on Terminal.app and for a review before
|
|
// anything changes. This is the review's evidence: for each hour, how many
|
|
// distinct 256-colour tones the sky and the sea actually carry, how tall each
|
|
// band of one tone is, and the largest perceptual jump between neighbouring
|
|
// rows -- measured on the RENDERED frame, quantised the way the terminal
|
|
// shows it, at the size of his window, not on the palette's nominal colours.
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"math"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/donlucasx/xscapes/internal/canvas"
|
|
"github.com/donlucasx/xscapes/internal/scape"
|
|
"github.com/donlucasx/xscapes/internal/term"
|
|
)
|
|
|
|
func main() {
|
|
w := flag.Int("w", 124, "scape width")
|
|
h := flag.Int("h", 27, "scape height (the rows the scape gets at his 62-row window)")
|
|
out := flag.String("html", "", "write the hour-by-hour page here")
|
|
steps := flag.Int("steps", 48, "samples across the day (48 = every half hour)")
|
|
dump := flag.String("dump", "", "print every half-row tone of the sky and sea at these clock hours, e.g. 5,12,16.5,20.5")
|
|
flag.Parse()
|
|
|
|
type hourStat struct {
|
|
tod float64
|
|
skyRows, seaRows int
|
|
skyBands, seaBands []band
|
|
skyMaxDE, seaMaxDE float64
|
|
skyMaxAt, seaMaxAt int
|
|
moon term.RGB
|
|
moonIdx int
|
|
frame256, frameTC string
|
|
frameBefore string
|
|
skyHard, seaHard int // steps of 20 or over, half-row to half-row
|
|
tones []term.RGB
|
|
sandTop int
|
|
}
|
|
var stats []hourStat
|
|
var pal canvas.HTMLPalette
|
|
|
|
for i := 0; i < *steps; i++ {
|
|
tod := float64(i) / float64(*steps)
|
|
c := canvas.New(*w, *h, canvas.AlphaFar, canvas.AlphaMid, canvas.AlphaNear)
|
|
sh := scape.NewShore(7, false)
|
|
sh.MoonX = 0.28
|
|
act := scape.Activity{Working: true, Level: 0.55, TimeOfDay: tod, ContextUsed: 0.3}
|
|
for k := 0; k < 14; k++ {
|
|
sh.Update(c, 3.0+float64(k)/20, act)
|
|
}
|
|
// Column 2: clear of the companion and the litter, open sea and sky.
|
|
// Half-row resolution: a split cell (U+2580) carries two tones.
|
|
var tones []term.RGB
|
|
for y := 0; y < c.H; y++ {
|
|
ch, fg, bg := c.ResolveAt(2, y, term.Profile256)
|
|
up, dn := bg, bg
|
|
if ch == '▀' {
|
|
up = fg
|
|
}
|
|
tones = append(tones, up, dn)
|
|
}
|
|
// The horizon is the biggest downward luma step in the upper part;
|
|
// the sand begins where the shore says it does.
|
|
hy := 0
|
|
drop := 0.0
|
|
for y := 1; y < c.H*3/4; y++ {
|
|
if d := luma(c.BGAt(2, y-1)) - luma(c.BGAt(2, y)); d > drop {
|
|
drop, hy = d, y
|
|
}
|
|
}
|
|
sandTop := sh.SandTop()
|
|
if sandTop <= hy || sandTop > c.H {
|
|
sandTop = c.H
|
|
}
|
|
st := hourStat{tod: tod, skyRows: hy, seaRows: sandTop - hy, tones: tones, sandTop: sandTop}
|
|
st.skyBands, st.skyMaxDE, st.skyMaxAt = bands(tones[:2*hy])
|
|
st.seaBands, st.seaMaxDE, st.seaMaxAt = bands(tones[2*hy : 2*sandTop])
|
|
st.seaMaxAt += hy * 2
|
|
st.skyHard = hardEdges(tones[:2*hy])
|
|
st.seaHard = hardEdges(tones[2*hy : 2*sandTop])
|
|
mx, my := sh.MoonPos()
|
|
if mx >= 0 && my >= 0 && my < c.H && mx < c.W {
|
|
_, fg, _ := c.ResolveAt(mx, my, term.Profile256)
|
|
st.moon = fg
|
|
st.moonIdx = fg.Index256()
|
|
}
|
|
st.frame256 = c.HTMLFragmentClassed(6, term.Profile256, &pal)
|
|
st.frameTC = c.HTMLFragmentClassed(6, term.ProfileTrueColor, &pal)
|
|
// The same frame the way it was quantised before the ramps became
|
|
// paths: every row rounded on its own. Kept on the page so the change
|
|
// is judged against what it replaced, not against memory.
|
|
//
|
|
// Restore what was set, not "true": with XSCAPES_RAMP=0 the whole
|
|
// run is meant to be the before, and the first version of this line
|
|
// switched the paths ON for the other 47 hours, so a before/after
|
|
// comparison of the table came out identical. An instrument that
|
|
// changes the thing it measures reports whatever it was set to.
|
|
was := term.Ramps
|
|
term.Ramps = false
|
|
st.frameBefore = c.HTMLFragmentClassed(6, term.Profile256, &pal)
|
|
term.Ramps = was
|
|
stats = append(stats, st)
|
|
}
|
|
|
|
// The table.
|
|
fmt.Printf("%-6s %-6s %-9s %-8s %-5s %-6s %-9s %-8s %-5s %-10s\n", "time", "sky", "sky tones", "maxΔE@", "hard", "sea", "sea tones", "maxΔE@", "hard", "body 256")
|
|
for _, st := range stats {
|
|
fmt.Printf("%-6s %-6s %-9s %-8s %-5s %-6s %-9s %-8s %-5s %-10s\n",
|
|
clock(st.tod),
|
|
fmt.Sprintf("%d", st.skyRows), fmt.Sprintf("%d %s", len(st.skyBands), runs(st.skyBands)),
|
|
fmt.Sprintf("%.0f@%d", st.skyMaxDE, st.skyMaxAt/2+1), fmt.Sprintf("%d", st.skyHard),
|
|
fmt.Sprintf("%d", st.seaRows), fmt.Sprintf("%d %s", len(st.seaBands), runs(st.seaBands)),
|
|
fmt.Sprintf("%.0f@%d", st.seaMaxDE, st.seaMaxAt/2+1), fmt.Sprintf("%d", st.seaHard),
|
|
fmt.Sprintf("#%02x%02x%02x/%d", st.moon.R, st.moon.G, st.moon.B, st.moonIdx))
|
|
}
|
|
|
|
if *dump != "" {
|
|
want := map[string]bool{}
|
|
for _, f := range strings.Split(*dump, ",") {
|
|
var h float64
|
|
fmt.Sscanf(strings.TrimSpace(f), "%g", &h)
|
|
want[clock(h/24)] = true
|
|
}
|
|
for _, st := range stats {
|
|
if !want[clock(st.tod)] {
|
|
continue
|
|
}
|
|
fmt.Printf("\n== %s sky rows 0-%d, sea rows %d-%d (half-rows; dE to the row above) ==\n", clock(st.tod), st.skyRows-1, st.skyRows, st.sandTop-1)
|
|
for i, c := range st.tones[:2*st.sandTop] {
|
|
region := "sky"
|
|
if i >= 2*st.skyRows {
|
|
region = "sea"
|
|
}
|
|
de := 0.0
|
|
if i > 0 {
|
|
de = deltaE(st.tones[i-1], c)
|
|
}
|
|
mark := ""
|
|
if de >= 20 {
|
|
mark = " <-- edge"
|
|
}
|
|
fmt.Printf("%s %4.1f #%02x%02x%02x %3d dE %4.1f%s\n", region, float64(i)/2, c.R, c.G, c.B, c.Index256(), de, mark)
|
|
}
|
|
}
|
|
}
|
|
if *out == "" {
|
|
return
|
|
}
|
|
var b strings.Builder
|
|
b.WriteString(`<!doctype html><html><head><meta charset="utf-8"><title>xscapes gradients by the hour</title><style>
|
|
body{margin:0;background:#0b0b10;color:#c9c9d4;font:14px/1.5 -apple-system,Helvetica,Arial,sans-serif;padding:28px}
|
|
h1{font-size:20px;margin:0 0 6px}p{max-width:80ch;color:#8a8a96;margin:0 0 18px}
|
|
.hour{display:grid;grid-template-columns:64px 1fr 1fr 1fr 250px;gap:12px;align-items:start;margin-bottom:14px;padding-bottom:12px;border-bottom:1px solid #1c1c24}
|
|
.t{font-size:18px;font-weight:600;color:#f0f0f4}.lbl{font-size:10px;letter-spacing:.1em;text-transform:uppercase;color:#55555f;margin-bottom:4px}
|
|
pre{margin:0;font-family:Menlo,monospace;line-height:1.0;letter-spacing:0;display:inline-block}
|
|
.win{background:#000;border:1px solid #22222a;border-radius:4px;padding:4px 6px;overflow:hidden}
|
|
.m{font-size:12px;color:#8a8a96}.m b{color:#e8c27a;font-weight:600}
|
|
.sw{display:inline-block;width:14px;height:14px;border-radius:2px;vertical-align:middle;margin-right:2px;border:1px solid #333}
|
|
` + "</style></head><body>")
|
|
b.WriteString(`<h1>xscapes: the sky and the sea, hour by hour, as Terminal.app shows them</h1>
|
|
<p>First: the 256-colour frame as it was, every row rounded to the nearest palette entry on its own. Second: the 256-colour frame now, the sky and the sea each painted as one path through the palette. Third: the truecolor frame the palette asks for. Right: what the new 256 sky and sea carry in column 2, at half-row resolution: how many distinct tones, how tall each band of one tone is (top to bottom), how many steps between neighbouring half-rows are hard edges (CIE76 ΔE of 20 or over), and the largest. Under 5 is barely visible. Rendered at ` + fmt.Sprintf("%dx%d", *w, *h) + `, the scape your 124x62 window gets.</p>`)
|
|
for _, st := range stats {
|
|
b.WriteString(`<div class="hour"><div class="t">` + clock(st.tod) + `</div>`)
|
|
b.WriteString(`<div><div class="lbl">256 before, rounded row by row</div><div class="win">` + st.frameBefore + `</div></div>`)
|
|
b.WriteString(`<div><div class="lbl">256 now, one path through the palette</div><div class="win">` + st.frame256 + `</div></div>`)
|
|
b.WriteString(`<div><div class="lbl">truecolor, what the palette asks for</div><div class="win">` + st.frameTC + `</div></div>`)
|
|
b.WriteString(`<div class="m"><div class="lbl">sky now, ` + fmt.Sprintf("%d rows", st.skyRows) + `</div>`)
|
|
b.WriteString(swatches(st.skyBands) + fmt.Sprintf(`<br><b>%d</b> tones, bands %s<br>hard edges <b>%d</b>, largest <b>%.0f</b> at row %d<br><br>`, len(st.skyBands), runs(st.skyBands), st.skyHard, st.skyMaxDE, st.skyMaxAt/2+1))
|
|
b.WriteString(`<div class="lbl">sea now, ` + fmt.Sprintf("%d rows", st.seaRows) + `</div>`)
|
|
b.WriteString(swatches(st.seaBands) + fmt.Sprintf(`<br><b>%d</b> tones, bands %s<br>hard edges <b>%d</b>, largest <b>%.0f</b> at row %d<br><br>`, len(st.seaBands), runs(st.seaBands), st.seaHard, st.seaMaxDE, st.seaMaxAt/2+1))
|
|
b.WriteString(fmt.Sprintf(`<div class="lbl">sun / moon</div><span class="sw" style="background:#%02x%02x%02x"></span> 256 index %d`, st.moon.R, st.moon.G, st.moon.B, st.moonIdx))
|
|
b.WriteString(`</div></div>`)
|
|
}
|
|
b.WriteString(`<style>` + pal.CSS() + `</style></body></html>`)
|
|
if err := os.WriteFile(*out, []byte(b.String()), 0o644); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println(*out)
|
|
}
|
|
|
|
// bands folds a column of half-row tones into runs of one tone, and finds
|
|
// the largest perceptual step between neighbours.
|
|
// band is a run of half-rows carrying one tone.
|
|
type band struct {
|
|
c term.RGB
|
|
rows int
|
|
}
|
|
|
|
func bands(tones []term.RGB) (out []band, maxDE float64, at int) {
|
|
for i, c := range tones {
|
|
if len(out) > 0 && out[len(out)-1].c == c {
|
|
out[len(out)-1].rows++
|
|
continue
|
|
}
|
|
if i > 0 {
|
|
if d := deltaE(tones[i-1], c); d > maxDE {
|
|
maxDE, at = d, i
|
|
}
|
|
}
|
|
out = append(out, band{c, 1})
|
|
}
|
|
return out, maxDE, at
|
|
}
|
|
|
|
// hardEdges counts the steps between neighbouring half-rows that the eye
|
|
// reads as an edge rather than a shade: CIE76 of 20 or over.
|
|
func hardEdges(tones []term.RGB) int {
|
|
n := 0
|
|
for i := 1; i < len(tones); i++ {
|
|
if tones[i] != tones[i-1] && deltaE(tones[i-1], tones[i]) >= 20 {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
|
|
func runs(bs []band) string {
|
|
var parts []string
|
|
for _, b := range bs {
|
|
parts = append(parts, fmt.Sprintf("%.1f", float64(b.rows)/2))
|
|
}
|
|
return "[" + strings.Join(parts, " ") + "]"
|
|
}
|
|
|
|
func swatches(bs []band) string {
|
|
var b strings.Builder
|
|
for _, x := range bs {
|
|
fmt.Fprintf(&b, `<span class="sw" style="background:#%02x%02x%02x;width:%dpx"></span>`, x.c.R, x.c.G, x.c.B, 6+x.rows*4)
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func clock(tod float64) string {
|
|
m := int(math.Round(tod * 24 * 60))
|
|
return fmt.Sprintf("%02d:%02d", (m/60)%24, m%60)
|
|
}
|
|
|
|
func luma(c term.RGB) float64 { return 0.30*float64(c.R) + 0.59*float64(c.G) + 0.11*float64(c.B) }
|
|
|
|
// deltaE is CIE76 between two sRGB colours: good enough to rank steps.
|
|
func deltaE(a, b term.RGB) float64 {
|
|
l1, a1, b1 := lab(a)
|
|
l2, a2, b2 := lab(b)
|
|
return math.Sqrt((l1-l2)*(l1-l2) + (a1-a2)*(a1-a2) + (b1-b2)*(b1-b2))
|
|
}
|
|
|
|
func lab(c term.RGB) (float64, float64, float64) {
|
|
lin := func(v uint8) float64 {
|
|
f := float64(v) / 255
|
|
if f <= 0.04045 {
|
|
return f / 12.92
|
|
}
|
|
return math.Pow((f+0.055)/1.055, 2.4)
|
|
}
|
|
r, g, bl := lin(c.R), lin(c.G), lin(c.B)
|
|
x := (0.4124*r + 0.3576*g + 0.1805*bl) / 0.95047
|
|
y := (0.2126*r + 0.7152*g + 0.0722*bl) / 1.0
|
|
z := (0.0193*r + 0.1192*g + 0.9505*bl) / 1.08883
|
|
f := func(t float64) float64 {
|
|
if t > 0.008856 {
|
|
return math.Cbrt(t)
|
|
}
|
|
return 7.787*t + 16.0/116
|
|
}
|
|
fx, fy, fz := f(x), f(y), f(z)
|
|
return 116*fy - 16, 500 * (fx - fy), 200 * (fy - fz)
|
|
}
|