From e311e434a8afd1a2810b647ab108c09072210595 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Mon, 23 Nov 2026 17:25:27 -0500 Subject: [PATCH] fix(web): home shelves fill the window and their text stays readable on any cover A wide card or genre band's wash keeps its cover's hue but caps its lightness and saturation, so a pale or loud cover no longer washes the card too light for its text; the faintest text on it is 80% parchment, and a test checks 4.5:1 against the lightest wash allowed. A genre's name keeps clear of its fan of covers. Shelves size their tiles to the window: each shows a whole number of tiles that fill its width, a short shelf grows its tiles up to a cap, and the arrows page by whole tiles. On a very wide screen the page stops at 140rem and centres. Co-Authored-By: Claude Opus 5.5 --- .../lib/components/HorizontalScrollRow.svelte | 33 ++++++++++- web/src/lib/components/PlaylistCard.svelte | 23 +++++--- web/src/lib/playlists/shelf.test.ts | 53 ++++++++++++++++++ web/src/lib/playlists/shelf.ts | 55 +++++++++++++++++++ web/src/routes/+page.svelte | 37 ++++++++----- 5 files changed, 178 insertions(+), 23 deletions(-) diff --git a/web/src/lib/components/HorizontalScrollRow.svelte b/web/src/lib/components/HorizontalScrollRow.svelte index c8c1c4fa..d5a3dc1d 100644 --- a/web/src/lib/components/HorizontalScrollRow.svelte +++ b/web/src/lib/components/HorizontalScrollRow.svelte @@ -1,12 +1,14 @@
@@ -105,7 +134,7 @@ data-testid="scroll-container" class="scroller overflow-x-auto" > -
+
{#each rows as row, rowIdx (rowIdx)}
{#each row as it, colIdx (colIdx)} diff --git a/web/src/lib/components/PlaylistCard.svelte b/web/src/lib/components/PlaylistCard.svelte index 8032b946..5d7b68da 100644 --- a/web/src/lib/components/PlaylistCard.svelte +++ b/web/src/lib/components/PlaylistCard.svelte @@ -7,7 +7,7 @@ import { getPlaylist, systemShuffle, refreshSystem } from '#lib/api/playlists.js'; import { playlistTrackToRef } from '#lib/playlists/playlistTrackToRef.js'; import { systemPlaylistRefetch } from '#lib/playlists/systemRefetch.js'; - import { refreshedLabel, songsLikeArtist } from '#lib/playlists/shelf.js'; + import { refreshedLabel, songsLikeArtist, washTone } from '#lib/playlists/shelf.js'; import { coverUrl } from '#lib/media/covers.js'; import { dominantColorFromUrl, rgbToCssString } from '#lib/media/dominantColor.js'; import { errCode } from '#lib/api/errors.js'; @@ -50,7 +50,8 @@ const lead = $derived(coverUrls[0] ?? playlist.cover_url ?? ''); // The wash behind a wide card or band is the lead cover's main colour, - // darkened so light text reads on it in either theme. Until the colour is + // toned down (washTone) and darkened so light text reads on it in either + // theme, whatever the cover. Until the colour is // known (or when there is no cover) the wash is a neutral slate. let washRgb = $state('rgb(44 49 58)'); $effect(() => { @@ -58,7 +59,7 @@ const url = lead; if (!url) return; dominantColorFromUrl(url).then((rgb) => { - if (rgb && url === lead) washRgb = rgbToCssString(rgb); + if (rgb && url === lead) washRgb = rgbToCssString(washTone(rgb)); }); }); @@ -289,7 +290,7 @@ {/each}
-
{playlist.name.replace(/\s+mix$/i, '')}
+
{playlist.name.replace(/\s+mix$/i, '')}
{#if showMeta}
{plural(playlist.track_count, 'track', 'tracks')}{refreshed ? ` · ${refreshed}` : ''}
{/if} @@ -372,8 +373,13 @@ ); } .on-wash { color: var(--fs-parchment); } - .on-wash-2 { color: color-mix(in srgb, var(--fs-parchment) 82%, transparent); } - .on-wash-3 { color: color-mix(in srgb, var(--fs-parchment) 68%, transparent); } + /* Fainter text stays at 90% and 80%: at those strengths it still clears + 4.5:1 on the lightest wash washTone allows (shelf.test.ts checks it). */ + .on-wash-2 { color: color-mix(in srgb, var(--fs-parchment) 90%, transparent); } + .on-wash-3 { color: color-mix(in srgb, var(--fs-parchment) 80%, transparent); } + /* The genre name keeps clear of the fan: the fan is three 3.75rem covers + overlapping by half (7.5rem) plus its 0.75rem inset. */ + .band-name { max-width: calc(100% - 8.75rem); overflow-wrap: anywhere; } .fan { position: absolute; right: 0.75rem; @@ -382,13 +388,14 @@ display: flex; } .fan img { - width: 4.5rem; + width: 3.75rem; aspect-ratio: 1; object-fit: cover; border-radius: 6px; box-shadow: 0 6px 16px rgb(0 0 0 / 0.45); - margin-left: -2.25rem; + margin-left: -1.875rem; } + .fan img:first-child { margin-left: 0; } .fan img:nth-child(1) { transform: rotate(-8deg) translateY(4px); } .fan img:nth-child(2) { transform: translateY(-4px); z-index: 1; } .fan img:nth-child(3) { transform: rotate(8deg) translateY(4px); z-index: 2; } diff --git a/web/src/lib/playlists/shelf.test.ts b/web/src/lib/playlists/shelf.test.ts index a0357c11..5008f343 100644 --- a/web/src/lib/playlists/shelf.test.ts +++ b/web/src/lib/playlists/shelf.test.ts @@ -96,3 +96,56 @@ test('songsLikeArtist drops the shelf-wide prefix', () => { expect(songsLikeArtist(pl({ name: 'Songs like Low Tide Choir' }))).toBe('Low Tide Choir'); expect(songsLikeArtist(pl({ name: 'Something else' }))).toBe('Something else'); }); + +describe('washTone', () => { + // WCAG relative luminance and contrast, to check the promise in the doc. + const lum = ({ r, g, b }: { r: number; g: number; b: number }) => { + const c = (v: number) => { + const s = v / 255; + return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4; + }; + return 0.2126 * c(r) + 0.7152 * c(g) + 0.0722 * c(b); + }; + // The card mixes the tone 62% toward #0e1013 at its lightest end, and its + // faintest text is parchment at 80% over that. + const mix = (a: { r: number; g: number; b: number }, t: number, b = { r: 14, g: 16, b: 19 }) => ({ + r: a.r * t + b.r * (1 - t), + g: a.g * t + b.g * (1 - t), + b: a.b * t + b.b * (1 - t) + }); + const parchment = { r: 232, g: 228, b: 216 }; + + test.each([ + ['beige', { r: 200, g: 190, b: 160 }], + ['near white', { r: 245, g: 245, b: 245 }], + ['acid yellow', { r: 240, g: 240, b: 20 }], + ['light grey', { r: 170, g: 175, b: 180 }] + ])('a %s cover still leaves the faintest text at 4.5:1', (_, cover) => { + const ground = mix(washTone(cover), 0.62); + const text = mix(parchment, 0.8, ground); + const ratio = (lum(text) + 0.05) / (lum(ground) + 0.05); + expect(ratio).toBeGreaterThanOrEqual(4.5); + }); + + test('a dark cover keeps its own colour', () => { + expect(washTone({ r: 30, g: 60, b: 90 })).toEqual({ r: 30, g: 60, b: 90 }); + }); +}); + +describe('shelfTileWidth', () => { + test('a long shelf shows whole tiles that fill its width', () => { + const w = shelfTileWidth(1000, 20, 150, 200, 16); + const cols = Math.floor((1000 + 16) / (150 + 16)); + expect(cols * w + (cols - 1) * 16).toBeLessThanOrEqual(1000); + expect(cols * w + (cols - 1) * 16).toBeGreaterThan(1000 - cols); + }); + + test('a short shelf grows its tiles up to the max', () => { + expect(shelfTileWidth(1000, 2, 150, 200, 16)).toBe(200); + expect(shelfTileWidth(400, 2, 150, 300, 16)).toBe(192); + }); + + test('a narrow window still gets one tile at the min', () => { + expect(shelfTileWidth(100, 5, 150, 200, 16)).toBe(150); + }); +}); diff --git a/web/src/lib/playlists/shelf.ts b/web/src/lib/playlists/shelf.ts index 55325c41..048db68d 100644 --- a/web/src/lib/playlists/shelf.ts +++ b/web/src/lib/playlists/shelf.ts @@ -71,3 +71,58 @@ export function shelfMeta(playlists: Playlist[], now: Date = new Date()): string export function songsLikeArtist(p: Playlist): string { return p.name.replace(/^Songs like\s+/i, ''); } + +// The colour a wide card or genre band is washed in, from its lead cover's +// main colour. A light or loud cover would wash the card too light for the +// light text on it, so the tone keeps the cover's hue but caps its lightness +// and saturation. The card's gradient then darkens it further, and its text +// clears 4.5:1 on any cover. +export function washTone({ r, g, b }: { r: number; g: number; b: number }): { r: number; g: number; b: number } { + const [h, s, l] = toHsl(r / 255, g / 255, b / 255); + return fromHsl(h, Math.min(s, 0.6), Math.min(l, 0.3)); +} + +function toHsl(r: number, g: number, b: number): [number, number, number] { + const max = Math.max(r, g, b); + const min = Math.min(r, g, b); + const l = (max + min) / 2; + if (max === min) return [0, 0, l]; + const d = max - min; + const s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + let h: number; + if (max === r) h = (g - b) / d + (g < b ? 6 : 0); + else if (max === g) h = (b - r) / d + 2; + else h = (r - g) / d + 4; + return [h / 6, s, l]; +} + +function fromHsl(h: number, s: number, l: number): { r: number; g: number; b: number } { + const q = l < 0.5 ? l * (1 + s) : l + s - l * s; + const p = 2 * l - q; + const channel = (t: number) => { + if (t < 0) t += 1; + if (t > 1) t -= 1; + if (t < 1 / 6) return p + (q - p) * 6 * t; + if (t < 1 / 2) return q; + if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; + return p; + }; + return { + r: Math.round(channel(h + 1 / 3) * 255), + g: Math.round(channel(h) * 255), + b: Math.round(channel(h - 1 / 3) * 255) + }; +} + +// How wide each tile on a shelf is, so the shelf shows a whole number of +// tiles that fill its width: as many columns as fit at `min` pixels, each +// then grown to share the width. A shelf with fewer items than columns +// gives each item up to `max` instead, so a short shelf is not a row of +// tiny tiles in a wide window. +export function shelfTileWidth(width: number, count: number, min: number, max: number, gap: number): number { + if (width <= 0) return min; + let cols = Math.max(1, Math.floor((width + gap) / (min + gap))); + if (count > 0 && count < cols) cols = count; + const w = (width - (cols - 1) * gap) / cols; + return Math.floor(Math.min(Math.max(w, min), count > 0 && count <= cols ? max : Infinity)); +} diff --git a/web/src/routes/+page.svelte b/web/src/routes/+page.svelte index 8a0919c2..b75d8d69 100644 --- a/web/src/routes/+page.svelte +++ b/web/src/routes/+page.svelte @@ -170,19 +170,22 @@ {pageTitle('Home')} -
+ +
{#snippet item(rowItem: PlaylistRowItem)} -
+
{#if rowItem.kind === 'real'} {#snippet item(rowItem: PlaylistRowItem)} -
+
{#if rowItem.kind === 'real'} {#snippet item(playlist: Playlist)} -
+
{#snippet item(album: import('#lib/api/types.js').AlbumRef)} -
+
{/snippet} {/if} @@ -284,10 +290,11 @@ {#snippet item(artist: import('#lib/api/types.js').ArtistRef)} -
+
{/snippet}
{/if} @@ -305,10 +312,11 @@ {#snippet item(album: import('#lib/api/types.js').AlbumRef)} -
+
{/snippet}
{/if} @@ -328,13 +336,14 @@ {#snippet item(album: import('#lib/api/types.js').AlbumRef)} -
+ hierarchy: its tiles start smaller (144px against + 176px), so the freshest albums above lead. --> +
{/snippet}
{/if} @@ -342,10 +351,11 @@ {#snippet item(artist: import('#lib/api/types.js').ArtistRef)} -
+
{/snippet}
{/if} @@ -387,10 +397,11 @@ {#snippet item(artist: import('#lib/api/types.js').ArtistRef)} -
+
{/snippet}
{/if}