diff --git a/bun.lock b/bun.lock index a0f5e06..8737706 100644 --- a/bun.lock +++ b/bun.lock @@ -6,6 +6,7 @@ "name": "sonora-website", "dependencies": { "flag-icons": "^7.5.0", + "vanilla-colorful": "0.7.2", }, "devDependencies": { "@sveltejs/adapter-static": "^3.0.0", @@ -342,6 +343,8 @@ "unenv": ["unenv@2.0.0-rc.24", "", { "dependencies": { "pathe": "^2.0.3" } }, "sha512-i7qRCmY42zmCwnYlh9H2SvLEypEFGye5iRmEMKjcGi7zk9UquigRjFtTLz0TYqr0ZGLZhaMHl/foy1bZR+Cwlw=="], + "vanilla-colorful": ["vanilla-colorful@0.7.2", "", {}, "sha512-z2YZusTFC6KnLERx1cgoIRX2CjPRP0W75N+3CC6gbvdX5Ch47rZkEMGO2Xnf+IEmi3RiFLxS18gayMA27iU7Kg=="], + "vite": ["vite@7.3.6", "", { "dependencies": { "esbuild": "^0.27.0 || ^0.28.0", "fdir": "^6.5.0", "picomatch": "^4.0.3", "postcss": "^8.5.6", "rollup": "^4.43.0", "tinyglobby": "^0.2.15" }, "optionalDependencies": { "fsevents": "~2.3.3" }, "peerDependencies": { "@types/node": "^20.19.0 || >=22.12.0", "jiti": ">=1.21.0", "less": "^4.0.0", "lightningcss": "^1.21.0", "sass": "^1.70.0", "sass-embedded": "^1.70.0", "stylus": ">=0.54.8", "sugarss": "^5.0.0", "terser": "^5.16.0", "tsx": "^4.8.1", "yaml": "^2.4.2" }, "optionalPeers": ["@types/node", "jiti", "less", "lightningcss", "sass", "sass-embedded", "stylus", "sugarss", "terser", "tsx", "yaml"], "bin": { "vite": "bin/vite.js" } }, "sha512-4XP60spRGjSZFf1qYH+dJIkK2znL3zQfl9KkOV9MkkRR/3Dls0dxaBsQPTloEc5BLXWPL9vsOxopxyKoMmDueg=="], "vitefu": ["vitefu@1.1.3", "", { "peerDependencies": { "vite": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0" }, "optionalPeers": ["vite"] }, "sha512-ub4okH7Z5KLjb6hDyjqrGXqWtWvoYdU3IGm/NorpgHncKoLTCfRIbvlhBm7r0YstIaQRYlp4yEbFqDcKSzXSSg=="], diff --git a/package.json b/package.json index 0775de9..7039e5c 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "wrangler": "^4.130.0" }, "dependencies": { - "flag-icons": "^7.5.0" + "flag-icons": "^7.5.0", + "vanilla-colorful": "0.7.2" } } diff --git a/src/app.html b/src/app.html index 92d3044..63c7842 100644 --- a/src/app.html +++ b/src/app.html @@ -15,7 +15,9 @@ root.classList.add('js'); try { - const scheme = localStorage.getItem('scheme'); + // the theme creator wears plain Dark or Light, so the site never competes with the theme + const plain = location.pathname.startsWith('/themes/creator'); + const scheme = localStorage.getItem('scheme') ?? (plain ? 'Dark' : null); if (scheme) { const slug = scheme .toLowerCase() @@ -30,14 +32,17 @@ 'rose-pine-dawn', 'solarized-light' ]; - root.dataset.scheme = slug; - root.dataset.theme = light.includes(slug) ? 'light' : 'dark'; + const bright = light.includes(slug); + root.dataset.scheme = plain ? (bright ? 'light' : 'dark') : slug; + root.dataset.theme = bright ? 'light' : 'dark'; } } catch {} try { const nav = performance.getEntriesByType('navigation')[0]; - if (location.hash && (!nav || nav.type === 'reload')) { + const shared = + location.pathname.startsWith('/themes/creator') && location.hash.startsWith('#theme='); + if (location.hash && !shared && (!nav || nav.type === 'reload')) { history.replaceState(history.state, '', location.pathname + location.search); } } catch {} diff --git a/src/lib/color.ts b/src/lib/color.ts new file mode 100644 index 0000000..829d7dc --- /dev/null +++ b/src/lib/color.ts @@ -0,0 +1,180 @@ +/** A colour as red, green and blue from 0 to 255 and alpha from 0 to 1. */ +export type Rgba = { r: number; g: number; b: number; a: number }; + +/** A colour as hue in degrees and saturation, lightness and alpha from 0 to 1. */ +export type Hsla = { h: number; s: number; l: number; a: number }; + +/** How far each of the theme creator's sliders moves every colour, from -100 to 100, hue in degrees. */ +export type Shift = { hue: number; saturation: number; lightness: number }; + +const HEX = /^#?([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i; + +const clamp = (value: number, low = 0, high = 1) => Math.min(Math.max(value, low), high); + +/** + * Reads a hex colour of 3, 4, 6 or 8 digits, with or without `#`, or CSS `rgb(r g b / a)`. + * Returns null for anything else. + */ +export function parse(value: string): Rgba | null { + const text = value.trim(); + const hex = text.match(HEX)?.[1]; + if (hex) { + const digits = hex.length <= 4 ? [...hex].map((digit) => digit + digit).join('') : hex; + const [r, g, b, a = 255] = (digits.match(/../g) ?? []).map((pair) => parseInt(pair, 16)); + return { r, g, b, a: a / 255 }; + } + const rgb = text.match(/^rgba?\(([^)]+)\)$/i)?.[1]; + if (!rgb) return null; + const [r, g, b, a = 1] = rgb + .split(/[\s,/]+/) + .filter(Boolean) + .map(Number); + if ([r, g, b, a].some(Number.isNaN)) return null; + return { r, g, b, a }; +} + +/** Whether `value` is a colour a theme file accepts: hex of 6 or 8 digits. */ +export function valid(value: string): boolean { + return /^#?(?:[0-9a-f]{6}|[0-9a-f]{8})$/i.test(value.trim()); +} + +/** A colour as lowercase `#rrggbb`, or `#rrggbbaa` when it is not opaque. */ +export function hex({ r, g, b, a }: Rgba): string { + const byte = (value: number) => + Math.round(clamp(value, 0, 255)) + .toString(16) + .padStart(2, '0'); + const alpha = Math.round(clamp(a) * 255); + return `#${byte(r)}${byte(g)}${byte(b)}${alpha < 255 ? byte(alpha) : ''}`; +} + +/** Any colour `parse` reads, written the way a theme file stores it. */ +export function normal(value: string): string { + const color = parse(value); + return color ? hex(color) : value; +} + +export function toHsl({ r, g, b, a }: Rgba): Hsla { + const red = r / 255; + const green = g / 255; + const blue = b / 255; + const top = Math.max(red, green, blue); + const low = Math.min(red, green, blue); + const l = (top + low) / 2; + const span = top - low; + if (span === 0) return { h: 0, s: 0, l, a }; + const s = span / (1 - Math.abs(2 * l - 1)); + const h = + top === red + ? ((green - blue) / span + (green < blue ? 6 : 0)) * 60 + : top === green + ? ((blue - red) / span + 2) * 60 + : ((red - green) / span + 4) * 60; + return { h, s, l, a }; +} + +export function fromHsl({ h, s, l, a }: Hsla): Rgba { + const chroma = (1 - Math.abs(2 * l - 1)) * s; + const turn = (((h % 360) + 360) % 360) / 60; + const second = chroma * (1 - Math.abs((turn % 2) - 1)); + const [red, green, blue] = + turn < 1 + ? [chroma, second, 0] + : turn < 2 + ? [second, chroma, 0] + : turn < 3 + ? [0, chroma, second] + : turn < 4 + ? [0, second, chroma] + : turn < 5 + ? [second, 0, chroma] + : [chroma, 0, second]; + const lift = l - chroma / 2; + return { r: (red + lift) * 255, g: (green + lift) * 255, b: (blue + lift) * 255, a }; +} + +/** + * Moves `value` by `shift`. Hue turns the colour wheel, saturation scales towards grey or full + * colour, and lightness moves towards black or white, so a grey stays grey and alpha stays put. + */ +export function adjust(value: string, shift: Shift): string { + const color = parse(value); + if (!color || (!shift.hue && !shift.saturation && !shift.lightness)) return value; + const { h, s, l, a } = toHsl(color); + return hex( + fromHsl({ + h: h + shift.hue, + s: scale(s, shift.saturation / 100), + l: towards(l, shift.lightness / 100), + a + }) + ); +} + +/** + * The colour that `adjust` turns into `value` under `shift`, so an edit made while the sliders + * are moved survives moving them again. It is exact except where a slider at its end has pinned + * every colour to the same saturation or lightness. + */ +export function unadjust(value: string, shift: Shift): string { + const color = parse(value); + if (!color || (!shift.hue && !shift.saturation && !shift.lightness)) return value; + const { h, s, l, a } = toHsl(color); + return hex( + fromHsl({ + h: h - shift.hue, + s: unscale(s, shift.saturation / 100), + l: untowards(l, shift.lightness / 100), + a + }) + ); +} + +/** Relative luminance as WCAG defines it. */ +export function luminance({ r, g, b }: Rgba): number { + const linear = (channel: number) => { + const value = channel / 255; + return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4; + }; + return 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b); +} + +/** `top` painted over the opaque `bottom`. The result is opaque. */ +export function over(top: Rgba, bottom: Rgba): Rgba { + const mix = (upper: number, lower: number) => upper * top.a + lower * (1 - top.a); + return { r: mix(top.r, bottom.r), g: mix(top.g, bottom.g), b: mix(top.b, bottom.b), a: 1 }; +} + +/** The WCAG contrast ratio between two opaque colours, from 1 to 21. */ +export function contrast(first: Rgba, second: Rgba): number { + const [light, dark] = [luminance(first), luminance(second)].sort((a, b) => b - a); + return (light + 0.05) / (dark + 0.05); +} + +/** Whether text on `value` should be light rather than dark. */ +export function dark(value: string): boolean { + const color = parse(value); + return !!color && luminance(color) < 0.18; +} + +function scale(value: number, amount: number): number { + return clamp(amount >= 0 ? value + (1 - value) * amount * value : value * (1 + amount)); +} + +function unscale(value: number, amount: number): number { + if (amount < 0) return amount === -1 ? value : clamp(value / (1 + amount)); + if (!amount) return value; + // solves value = x + (1 - x) * amount * x for x in 0..1 + const root = + (1 + amount - Math.sqrt(Math.max(0, (1 + amount) ** 2 - 4 * amount * value))) / (2 * amount); + return clamp(root); +} + +function towards(value: number, amount: number): number { + return clamp(amount >= 0 ? value + (1 - value) * amount : value * (1 + amount)); +} + +function untowards(value: number, amount: number): number { + if (amount >= 1 || amount <= -1) return value; + return clamp(amount >= 0 ? (value - amount) / (1 - amount) : value / (1 + amount)); +} diff --git a/src/lib/components/Checkbox.svelte b/src/lib/components/Checkbox.svelte new file mode 100644 index 0000000..7144208 --- /dev/null +++ b/src/lib/components/Checkbox.svelte @@ -0,0 +1,73 @@ + + + + + diff --git a/src/lib/components/ColorPicker.svelte b/src/lib/components/ColorPicker.svelte new file mode 100644 index 0000000..6bb8be4 --- /dev/null +++ b/src/lib/components/ColorPicker.svelte @@ -0,0 +1,66 @@ + + + + + diff --git a/src/lib/components/Preview.svelte b/src/lib/components/Preview.svelte index b59dbb8..352c2a7 100644 --- a/src/lib/components/Preview.svelte +++ b/src/lib/components/Preview.svelte @@ -87,7 +87,6 @@ .frame { width: max-content; max-width: 100%; - border: 1px solid var(--border); border-radius: 12px; overflow: hidden; background: var(--bg); @@ -101,8 +100,8 @@ .ghost { display: none; - width: 1172px; - height: 732px; + width: 1170px; + height: 730px; } @media (min-width: 1280px) { diff --git a/src/lib/components/SchemePicker.svelte b/src/lib/components/SchemePicker.svelte index 7bae52a..7fe3c76 100644 --- a/src/lib/components/SchemePicker.svelte +++ b/src/lib/components/SchemePicker.svelte @@ -70,9 +70,7 @@ class:on={scheme.current === name} onclick={() => pick(name)} > - - - + {name} {#if name === ADAPTIVE}from the cover{/if} @@ -153,22 +151,14 @@ color: var(--fg); } + /* the page colour at the top left and the accent at the bottom right, like the theme cards */ .swatch { - position: relative; - width: 22px; + width: 16px; height: 16px; flex: none; - border-radius: 5px; - box-shadow: inset 0 0 0 1px rgb(127 127 127 / 0.35); - } - - .swatch i { - position: absolute; - right: 4px; - bottom: 4px; - width: 6px; - height: 6px; - border-radius: 999px; + border-radius: 50%; + background: linear-gradient(135deg, var(--page) 50%, var(--accent) 50%); + box-shadow: inset 0 0 0 1px rgb(127 127 127 / 0.3); } .hint { diff --git a/src/lib/components/Specimens.svelte b/src/lib/components/Specimens.svelte new file mode 100644 index 0000000..14e2334 --- /dev/null +++ b/src/lib/components/Specimens.svelte @@ -0,0 +1,543 @@ + + +
+
+
+ +
+
+ +
+
+
+ {#each STATUS as status (status.token)} +
+ {@html status.icon} + {status.text} +
+ {/each} +
+ + Like +
+
+
+
+ +
+
+
+
+ +
+
+
+ Delete playlist? + + Cancel + Delete + +
+
+
+
+ +
+
+ + Play + Play + Delete + +
+
+ +
+
+
+ 1 · The Water Is Wide + 2 · Wild Mountain Thyme + 3 · Loch Lomond + 4 · The Lark in the Clear Air +
+
+
+ +
+
+
+ Search songs + + + + Home + +
+
+
+ +
+
+
+
+
+ Airs and Ballads + Traditional +
+ +
+
+
+ +
+
+
+ + + + Sign in to Sonora +
+
+
+
+ + diff --git a/src/lib/components/ThemePreview.svelte b/src/lib/components/ThemePreview.svelte index d99b548..fc4e464 100644 --- a/src/lib/components/ThemePreview.svelte +++ b/src/lib/components/ThemePreview.svelte @@ -99,6 +99,12 @@ {/each} {/if} + {#if chosen} + Edit + {/if} {/if} @@ -203,6 +209,7 @@ box-shadow: inset 0 0 0 1px rgb(127 127 127 / 0.3); } + .edit, .close { flex: none; height: 32px; diff --git a/src/lib/components/TokenMap.svelte b/src/lib/components/TokenMap.svelte new file mode 100644 index 0000000..beb6be9 --- /dev/null +++ b/src/lib/components/TokenMap.svelte @@ -0,0 +1,641 @@ + + +
+
+ +
+ +
{@render children?.()}
+ + + + {#each placed as label (label.token)} + + {/each} +
+ + diff --git a/src/lib/components/mock/AppMock.svelte b/src/lib/components/mock/AppMock.svelte index dd1a9dc..cb42512 100644 --- a/src/lib/components/mock/AppMock.svelte +++ b/src/lib/components/mock/AppMock.svelte @@ -4,6 +4,7 @@ import { player } from '$lib/mock/player.svelte'; import { reach } from '$lib/mock/packs.svelte'; import { dim, palette } from '$lib/mock/theme'; + import { dark as isDark } from '$lib/color'; import { route } from '$lib/mock/route.svelte'; import { radius, settings } from '$lib/mock/settings.svelte'; import { ADAPTIVE, scheme as siteScheme } from '$lib/scheme.svelte'; @@ -31,12 +32,21 @@ import Sidebar from './Sidebar.svelte'; import TitleBar from './TitleBar.svelte'; - /** Whether the panes stay still under the wheel until the visitor clicks in, for a window laid inline in a page the visitor is scrolling. */ - let { hold = false }: { hold?: boolean } = $props(); + let { + hold = false, + colors, + floor = 0.62 + }: { + /** Whether the panes stay still under the wheel until the visitor clicks in, for a window laid inline in a page the visitor is scrolling. */ + hold?: boolean; + /** Every colour token by name, which the window wears instead of the theme picked in its settings. */ + colors?: Record; + /** The least the window shrinks to before its stage scrolls sideways instead. */ + floor?: number; + } = $props(); const WIDTH = 1170; const HEIGHT = 730; - const FLOOR = 0.62; const FADE = 320; /** The sidebars' limits, as the desktop's, and the least the page between them keeps. */ const LEFT_MIN = 160; @@ -62,13 +72,27 @@ const at = $derived(route.now); - const scale = $derived(Math.min(Math.max(room / WIDTH, FLOOR), 1)); + const scale = $derived(Math.min(Math.max(room / WIDTH, floor), 1)); /** The adaptive theme wears the site's colour, so the window and the page around it always match. */ const tint = $derived(site.tint); - const tokens = $derived(palette(tint, settings.adaptive, settings.theme)); + const tokens = $derived( + colors + ? Object.fromEntries( + Object.entries(colors).map(([name, value]) => [`--m-${name.replaceAll('_', '-')}`, value]) + ) + : palette(tint, settings.adaptive, settings.theme) + ); const scheme = $derived( - settings.theme === 'System' ? undefined : dim(settings.theme) ? 'dark' : 'light' + colors + ? isDark(colors.background) + ? 'dark' + : 'light' + : settings.theme === 'System' + ? undefined + : dim(settings.theme) + ? 'dark' + : 'light' ); const style = $derived( Object.entries(tokens) @@ -195,6 +219,10 @@ menu.close(); }); + $effect(() => { + player.wake(); + }); + onDestroy(() => { player.release(); together.release(); @@ -225,29 +253,36 @@ {#if docked} {/if} - {#if at.screen === 'home'} - - {:else if at.screen === 'search'} - - {:else if at.screen === 'library'} - - {:else if at.screen === 'history'} - - {:else if at.screen === 'settings'} - route.go({ screen: 'equalizer' })} - /> - {:else if at.screen === 'equalizer'} - - {:else if at.screen === 'artist'} - - {:else if at.screen === 'playlist'} - - {:else} - - {/if} +
+ {#if at.screen === 'home'} + + {:else if at.screen === 'search'} + + {:else if at.screen === 'library'} + + {:else if at.screen === 'history'} + + {:else if at.screen === 'settings'} + route.go({ screen: 'equalizer' })} + /> + {:else if at.screen === 'equalizer'} + + {:else if at.screen === 'artist'} + + {:else if at.screen === 'playlist'} + + {:else} + + {/if} +
{#if right && tab === 'members'} {:else if right} @@ -324,6 +359,42 @@ display: flex; flex: 1; min-height: 0; + background: var(--m-sidebar); + } + + /* the page lies on the chrome as a sheet, edged and rounded wherever a sidebar or an opaque player bar meets it, square against the window */ + .sheet { + --corner: calc(min(var(--m-radius), 10px) * 1.5); + position: relative; + display: flex; + flex: 1; + min-width: 0; + min-height: 0; + overflow: hidden; + background: var(--m-background); + border-top: 1px solid var(--m-sidebar-border); + } + + .sheet.left { + border-left: 1px solid var(--m-sidebar-border); + border-top-left-radius: var(--corner); + } + + .sheet.right { + border-right: 1px solid var(--m-sidebar-border); + border-top-right-radius: var(--corner); + } + + .sheet.footed { + border-bottom: 1px solid var(--m-sidebar-border); + } + + .sheet.footed.left { + border-bottom-left-radius: var(--corner); + } + + .sheet.footed.right { + border-bottom-right-radius: var(--corner); } .dismiss { diff --git a/src/lib/components/mock/Aside.svelte b/src/lib/components/mock/Aside.svelte index 43144eb..320eb2f 100644 --- a/src/lib/components/mock/Aside.svelte +++ b/src/lib/components/mock/Aside.svelte @@ -377,8 +377,7 @@ flex-direction: column; width: var(--m-right, 254px); overflow: hidden; - background: var(--m-background); - border-left: 1px solid var(--m-border); + background: var(--m-sidebar); } .aside.stripped { @@ -388,7 +387,6 @@ min-height: 0; height: 100%; background: none; - border-left: 0; } .head { @@ -399,7 +397,6 @@ gap: 7px; height: 32px; padding: 0 7px; - border-bottom: 1px solid var(--m-border); } .eyebrow { diff --git a/src/lib/components/mock/Members.svelte b/src/lib/components/mock/Members.svelte index cba1783..726643a 100644 --- a/src/lib/components/mock/Members.svelte +++ b/src/lib/components/mock/Members.svelte @@ -78,8 +78,7 @@ flex-direction: column; width: var(--m-right, 254px); overflow: hidden; - background: var(--m-background); - border-left: 1px solid var(--m-border); + background: var(--m-sidebar); } .head { @@ -88,7 +87,6 @@ align-items: center; height: 32px; padding: 0 7px; - border-bottom: 1px solid var(--m-border); } .eyebrow { diff --git a/src/lib/components/mock/PlayerBar.svelte b/src/lib/components/mock/PlayerBar.svelte index 1f17d11..708e0b4 100644 --- a/src/lib/components/mock/PlayerBar.svelte +++ b/src/lib/components/mock/PlayerBar.svelte @@ -146,8 +146,8 @@ gap: 14px; height: 76px; padding: 0 17.5px; - background: var(--m-secondary); - border-top: 1px solid var(--m-border); + background: var(--m-sidebar); + border-top: 1px solid transparent; } .now { @@ -236,9 +236,11 @@ gap: 7px; } + /* glass over the page, the popover colour at 70% under a wide saturated blur, parted from the page by its own edge */ .frosted { - background: color-mix(in srgb, var(--m-secondary) 72%, transparent); - backdrop-filter: blur(24px) saturate(1.4); + background: color-mix(in srgb, var(--m-popover) 70%, transparent); + backdrop-filter: blur(18px) saturate(1.8); + border-top-color: var(--m-border); } .bar.compact { diff --git a/src/lib/components/mock/Sidebar.svelte b/src/lib/components/mock/Sidebar.svelte index 31cb989..bece4f5 100644 --- a/src/lib/components/mock/Sidebar.svelte +++ b/src/lib/components/mock/Sidebar.svelte @@ -92,7 +92,6 @@ scrollbar-width: thin; scrollbar-color: color-mix(in srgb, var(--m-muted-foreground) 45%, transparent) transparent; background: var(--m-sidebar); - border-right: 1px solid var(--m-sidebar-border); } .row { diff --git a/src/lib/components/mock/TitleBar.svelte b/src/lib/components/mock/TitleBar.svelte index 5971bc8..d732b12 100644 --- a/src/lib/components/mock/TitleBar.svelte +++ b/src/lib/components/mock/TitleBar.svelte @@ -157,8 +157,8 @@ flex: none; align-items: center; height: 36px; - background: var(--m-background); - border-bottom: 1px solid var(--m-title-bar-border); + background: var(--m-sidebar); + border-bottom: 1px solid transparent; } /* the sidebar toggle sits as close to the history buttons as they sit to each other */ diff --git a/src/lib/creator.ts b/src/lib/creator.ts new file mode 100644 index 0000000..6cf0086 --- /dev/null +++ b/src/lib/creator.ts @@ -0,0 +1,176 @@ +import { normal } from './color'; +import { themes as community, zip } from './data/themes'; +import { colorsOf, darkColors, FIELDS, themes as builtIn } from './mock/theme'; +import { + base64, + bytes, + check, + family, + format, + link, + read, + slug, + type Canonical +} from './theme-file'; + +/** A palette the theme creator can start from. */ +export type Source = { + id: string; + group: 'Built in' | 'Community'; + title: string; + /** Every token as theme file hex. */ + colors: Record; +}; + +/** One variant of a theme as the creator exports it, `label` being its part of the title. */ +export type Draft = { label: string; colors: Record }; + +/** A theme the visitor brought in, every variant with its colours filled in. */ +export type Imported = { name: string; author: string; variants: Draft[] }; + +/** A checked theme ready to save: its id and its files, sorted the way Sonora lists them. */ +export type Packed = { id: string; files: [string | null, Canonical][] }; + +const LINK = 'sonora://theme/'; +const UNREADABLE = 'This is neither a theme file nor a theme link.'; + +/** Sonora's own themes, then every variant in the community gallery. */ +export const sources: Source[] = [ + ...builtIn + .filter((title) => title !== 'System') + .map((title) => ({ + id: `built-in/${slug(title)}`, + group: 'Built in' as const, + title, + colors: hexes(colorsOf(title)) + })), + ...community.flatMap((theme) => + theme.variants.map((variant) => ({ + id: `community/${theme.id}/${variant.id}`, + group: 'Community' as const, + title: variant.title, + colors: hexes(variant.colors) + })) + ) +]; + +/** The palette the creator opens with. */ +export const DEFAULT_SOURCE = 'built-in/dark'; + +/** + * Reads a theme the visitor pasted: the text of a theme file, a `sonora://theme/` link or the + * part of one after it. A link that carries a family brings every variant. Throws with a reason + * the page can show. + */ +export function bring(text: string): Imported { + const trimmed = text.trim(); + let json = trimmed; + if (!trimmed.startsWith('{')) { + const payload = trimmed.startsWith(LINK) ? trimmed.slice(LINK.length) : trimmed; + try { + json = unbase64(payload); + } catch { + throw new Error(UNREADABLE); + } + } + let parsed: unknown; + try { + parsed = JSON.parse(json); + } catch { + throw new Error(UNREADABLE); + } + const fail = (reason: string): never => { + throw new Error(`${reason[0].toUpperCase()}${reason.slice(1)}.`); + }; + const variants = (parsed as { variants?: Record }).variants; + const files = (variants ? Object.values(variants) : [parsed]).map((one) => + read(JSON.stringify(one), fail) + ); + if (!files.length) fail('the link carries no theme'); + const named = + files.length > 1 + ? family( + 'theme', + files.map((one) => one.name) + ) + : { name: files[0].name, variants: [''] }; + return { + name: named.name, + author: files[0].author, + variants: files.map((one, index) => ({ + label: named.variants[index], + colors: Object.fromEntries( + FIELDS.map((token) => [token, normal(one.theme[token] ?? darkColors[token])]) + ) + })) + }; +} + +/** + * Checks the theme the way Sonora checks a link and returns its files. Every variant is titled + * with the theme's name and then its own label, which is how Sonora names a family back. Throws + * with a reason the page can show. + */ +export function pack(name: string, author: string, drafts: Draft[]): Packed { + const id = slug(name) || 'theme'; + const single = drafts.length === 1; + const entries = drafts.map((draft, index): [string | null, string] => { + const title = single ? name : `${name.trim()} ${draft.label.trim()}`; + const theme: Canonical = { + name: title.trim(), + author: author.trim(), + version: 1, + theme: Object.fromEntries(FIELDS.map((token) => [token, normal(draft.colors[token])])) + }; + const stem = single ? null : slug(draft.label) || `variant-${index + 1}`; + return [stem, JSON.stringify(theme)]; + }); + const files = check(id, entries, (reason) => { + throw new Error(`${reason[0].toUpperCase()}${reason.slice(1)}.`); + }); + return { id, files }; +} + +/** The `sonora://theme/` link that installs a packed theme, or null when it is too long. */ +export function install({ id, files }: Packed): string | null { + try { + return link(id, files, (reason) => { + throw new Error(reason); + }); + } catch { + return null; + } +} + +/** What a theme link carries after `sonora://theme/`, which the creator's own link carries too. */ +export function payload({ id, files }: Packed): string { + const body = + files[0][0] === null ? files[0][1] : { family: id, variants: Object.fromEntries(files) }; + return base64(bytes(JSON.stringify(body))) + .replace(/\+/g, '-') + .replace(/\//g, '_') + .replace(/=+$/, ''); +} + +/** The file to save: the theme file itself, or a zip of the folder for a family. */ +export function archive({ id, files }: Packed): { name: string; blob: Blob } { + if (files[0][0] === null) { + return { + name: `${id}.json`, + blob: new Blob([format(files[0][1])], { type: 'application/json' }) + }; + } + const data = zip(files.map(([stem, file]) => [`${id}/${stem}.json`, format(file)])); + return { name: `${id}.zip`, blob: new Blob([data as BlobPart], { type: 'application/zip' }) }; +} + +/** Every token of `colors` as theme file hex. */ +function hexes(colors: Record): Record { + return Object.fromEntries(FIELDS.map((token) => [token, normal(colors[token])])); +} + +function unbase64(text: string): string { + const padded = text.replace(/-/g, '+').replace(/_/g, '/'); + const binary = atob(padded + '='.repeat((4 - (padded.length % 4)) % 4)); + return new TextDecoder().decode(Uint8Array.from(binary, (char) => char.charCodeAt(0))); +} diff --git a/src/lib/data/themes.ts b/src/lib/data/themes.ts index d8fdf9a..ad7800e 100644 --- a/src/lib/data/themes.ts +++ b/src/lib/data/themes.ts @@ -160,7 +160,7 @@ function css(hex: string): string { } /** A zip archive that stores `entries`, each a path and its text, without compressing them. */ -function zip(entries: [string, string][]): Uint8Array { +export function zip(entries: [string, string][]): Uint8Array { const parts: Uint8Array[] = []; const directory: Uint8Array[] = []; let offset = 0; diff --git a/src/lib/mock/player.svelte.ts b/src/lib/mock/player.svelte.ts index a3aaf35..bb3c61c 100644 --- a/src/lib/mock/player.svelte.ts +++ b/src/lib/mock/player.svelte.ts @@ -3,6 +3,8 @@ import { album, catalogue, origin, similar, tracks, upcoming } from '$lib/mock/a const albumOrder = tracks.map((track) => track.id); const playlistOrder = upcoming.map((track) => track.id); +/** The track the mock opens on, already playing from the Quiet Hours playlist. */ +const OPENING = 'x1'; function after(id: string) { const at = albumOrder.indexOf(id); @@ -18,14 +20,16 @@ function scramble(ids: string[]) { return out; } -let current = $state('t2'); -let past = $state(['t5', 't1']); -let source = $state(playlistOrder); -let queued = $state([...playlistOrder]); +let current = $state(OPENING); +let past = $state(['g1', 'p1', 's1']); +let source = $state(playlistOrder.slice(playlistOrder.indexOf(OPENING) + 1)); +let queued = $state([...source]); let from = $state(origin); let suggested = $state(similar.map((track) => track.id)); -let playing = $state(false); -let elapsed = $state(56); +let playing = $state(true); +let elapsed = $state(7); +/** Whether the visitor has yet to touch playback. Until then the opening track loops, so the site keeps its colours. */ +let untouched = true; let volume = $state(0.7); let shuffle = $state(false); let repeat = $state(0); @@ -105,7 +109,7 @@ function tick(now: number) { elapsed = along; return; } - if (repeat === 2) { + if (repeat === 2 || untouched) { land(0); return; } @@ -171,13 +175,21 @@ export const player = { else liked.add(id); }, + /** Starts the clock of a track that is already playing, as the window mounts in the browser. */ + wake() { + if (!playing || ticker) return; + wind(); + ticker = requestAnimationFrame(tick); + }, resume() { + untouched = false; if (playing) return; playing = true; wind(); if (!ticker) ticker = requestAnimationFrame(tick); }, pause() { + untouched = false; playing = false; stop(); }, @@ -212,6 +224,7 @@ export const player = { this.resume(); }, previous() { + untouched = false; if (elapsed > 3) { land(0); return; @@ -227,6 +240,7 @@ export const player = { this.select(albumOrder[0]); }, seek(fraction: number) { + untouched = false; land(fraction * this.track.length); }, setVolume(level: number) { @@ -245,10 +259,12 @@ export const player = { this.resume(); }, toggleShuffle() { + untouched = false; shuffle = !shuffle; queued = shuffle ? scramble(queued) : [...source]; }, cycleRepeat() { + untouched = false; repeat = (repeat + 1) % 3; }, toggleRadio() { diff --git a/src/lib/mock/route.svelte.ts b/src/lib/mock/route.svelte.ts index 97af157..8d7f7d9 100644 --- a/src/lib/mock/route.svelte.ts +++ b/src/lib/mock/route.svelte.ts @@ -1,6 +1,6 @@ export type Stop = { screen: string; tab?: string; id?: string }; -let trail: Stop[] = $state([{ screen: 'album', id: 'airs' }]); +let trail: Stop[] = $state([{ screen: 'playlist', id: 'quiet-hours' }]); let at = $state(0); const same = (one: Stop, other: Stop) => diff --git a/src/lib/mock/theme.ts b/src/lib/mock/theme.ts index e109a8b..f28d6d3 100644 --- a/src/lib/mock/theme.ts +++ b/src/lib/mock/theme.ts @@ -36,7 +36,11 @@ export const FIELDS = [ 'table_row_border', 'table_hover', 'table_active', - 'table_active_border' + 'table_active_border', + 'status_error', + 'status_success', + 'status_warning', + 'status_info' ]; const SURFACES = new Set([ @@ -62,38 +66,38 @@ const TEXTS = new Set([ ]); const palettes: Record = { - dark: '0a0a0a fafafa 50505066 262626 0000008c fafafa 909090 171717 36363666 4d4d4d66 fafafa 171717 e5e5e5b3 7f1d1d fef2f2 8b2020b3 141414 fafafa f5f5f5 1d4ed8 0a0a0a 50505066 262626 262626 171717cc 8a8a8a 262626b3 50505033 1e40af33 1d4ed8', + dark: '0b0b0b fafafa 50505066 262626 0000008c fafafa 919191 171717 36363666 4d4d4d66 fafafa 171717 e5e5e5b3 7f1d1d fef2f2 8b2020b3 141414 fafafa f5f5f5 1d4ed8 070707 50505066 181818 262626 171717cc 8a8a8a 262626b3 50505033 1e40af33 1d4ed8 ff6369 4ade80 fbbf24 60a5fa', light: - 'fafafa 171717 9b9b9b66 e5e5e5 0000008c fafafa 606060 f5f5f5 d5d5d566 b7b7b766 171717 fafafa 262626b3 b91c1c fef2f2 991b1bb3 ffffff 171717 262626 2563eb f5f5f5 cdcdcd66 d4d4d4 d4d4d4 f5f5f5e6 666666 d4d4d4b3 cdcdcd33 2563eb1f 2563eb', + 'fafafa 171717 9b9b9b66 e5e5e5 0000008c fafafa 5e5e5e f5f5f5 d5d5d566 b7b7b766 171717 fafafa 262626b3 b91c1c fef2f2 991b1bc1 ffffff 171717 262626 2563eb ededed cdcdcd66 e4e4e4 d4d4d4 f5f5f5e6 666666 d4d4d4b3 cdcdcd33 2563eb1f 2563eb dc2626 16a34a d97706 2563eb', 'catppuccin-latte': - 'eff1f5 4c4f69 acb0be66 ccd0da 0000008c eff1f5 6c6f85 e6e9ef ccd0da66 bcc0cc66 8839ef eff1f5 7430cbb3 d20f39 eff1f5 b20d30b3 eff1f5 4c4f69 8839ef 8839ef eff1f5 bcc0cc66 ccd0da ccd0da e6e9efe6 6c6f85 ccd0dab3 ccd0da66 8839ef33 8839ef', + 'eff1f5 484b64 acb0be66 ced2db 0000008c eff1f5 53556d e6e9ef ccd0da66 bcc0cc66 8839ef eff1f5 7430cbd5 d20f39 eff1f5 b20d30c6 eff1f5 484b64 8839ef 8839ef e6e9ef bcc0cc66 ccd0da ccd0da e6e9efe6 5c5f77 ccd0dab3 ccd0da66 8839ef33 8839ef d20f39 3f9e2a c27b19 1e66f5', 'catppuccin-frappe': - '303446 c6d0f5 62688066 51576d 0000008c c6d0f5 a5adce 414559 51576d66 62688066 ca9ee6 232634 d7b6ecb3 e78284 232634 eda1a3b3 292c3c c6d0f5 ca9ee6 ca9ee6 303446 62688066 414559 414559 292c3ce6 949cbb 414559b3 51576d66 ca9ee633 ca9ee6', + '303446 c6d0f5 62688066 494e63 0000008c c6d0f5 b5bfe2 414559 51576d66 62688066 ca9ee6 232634 d7b6ecb3 e78284 232634 eda1a3b5 292c3c c6d0f5 ca9ee6 ca9ee6 292c3c 62688066 414559 414559 292c3ce6 949cbb 414559b3 51576d66 ca9ee633 ca9ee6 ff6369 4ade80 fbbf24 60a5fa', 'catppuccin-macchiato': - '24273a cad3f5 5b607866 494d64 0000008c cad3f5 a5adcb 363a4f 494d6466 5b607866 c6a0f6 181926 d4b8f8b3 ed8796 181926 f2a5b0b3 1e2030 cad3f5 c6a0f6 c6a0f6 24273a 5b607866 363a4f 363a4f 1e2030e6 939ab7 363a4fb3 494d6466 c6a0f633 c6a0f6', + '24273a cad3f5 5b607866 3f4258 0000008c cad3f5 a6afcd 363a4f 494d6466 5b607866 c6a0f6 181926 d4b8f8b3 ed8796 181926 f2a5b0b3 1e2030 cad3f5 c6a0f6 c6a0f6 1e2030 5b607866 363a4f 363a4f 1e2030e6 939ab7 363a4fb3 494d6466 c6a0f633 c6a0f6 ff6369 4ade80 fbbf24 60a5fa', 'catppuccin-mocha': - '1e1e2e cdd6f4 585b7066 45475a 0000008c cdd6f4 a6adc8 313244 45475a66 585b7066 cba6f7 11111b d8bcf9b3 f38ba8 11111b f6a8beb3 181825 cdd6f4 cba6f7 cba6f7 1e1e2e 585b7066 313244 313244 181825e6 9399b2 313244b3 45475a66 cba6f733 cba6f7', + '1e1e2e cdd6f4 585b7066 3f4153 0000008c cdd6f4 a6adc8 313244 45475a66 585b7066 cba6f7 11111b d8bcf9b3 f38ba8 11111b f6a8beb3 181825 cdd6f4 cba6f7 cba6f7 181825 585b7066 313244 313244 181825e6 9399b2 313244b3 45475a66 cba6f733 cba6f7 ff6369 4ade80 fbbf24 60a5fa', dracula: - '282a36 f8f8f2 6272a466 44475a 0000008c f8f8f2 b4bccf 343746 44475a66 6272a466 bd93f9 282a36 ceaefab3 ff5555 282a36 ff8080b3 21222c f8f8f2 bd93f9 bd93f9 282a36 6272a466 343746 343746 21222ce6 b4bccf 343746b3 44475a66 bd93f933 bd93f9', + '282a36 f8f8f2 6272a466 44475a 0000008c f8f8f2 b4bccf 343746 44475a66 6272a466 bd93f9 282a36 ceaefab8 ff5555 282a36 ff8080d7 21222c f8f8f2 bd93f9 bd93f9 21222c 6272a466 343746 343746 21222ce6 b4bccf 343746b3 44475a66 bd93f933 bd93f9 ff6369 4ade80 fbbf24 60a5fa', 'gruvbox-dark': - '282828 ebdbb2 665c5466 3c3836 0000008c ebdbb2 a89984 32302f 50494566 665c5466 fe8019 282828 fea052b3 fb4934 282828 fc7667b3 32302f ebdbb2 fe8019 fe8019 282828 665c5466 3c3836 3c3836 282828e6 928374 3c3836b3 50494566 fe801933 fe8019', + '282828 ebdbb2 665c5466 3c3836 0000008c ebdbb2 c0b197 32302f 50494566 665c5466 fe8019 282828 fea052b9 fb4934 1d2021 fc7667cc 32302f ebdbb2 fe8019 fe8019 1d2021 665c5466 3c3836 3c3836 282828e6 a89984 3c3836b3 50494566 fe801933 fe8019 ff6369 4ade80 fbbf24 60a5fa', 'gruvbox-dark-hard': - '1d2021 ebdbb2 665c5466 3c3836 0000008c ebdbb2 a89984 282828 50494566 665c5466 fe8019 1d2021 fea052b3 fb4934 1d2021 fc7667b3 282828 ebdbb2 fe8019 fe8019 1d2021 665c5466 3c3836 3c3836 1d2021e6 928374 3c3836b3 50494566 fe801933 fe8019', + '1d2021 ebdbb2 665c5466 3b3735 0000008c ebdbb2 b5a893 282828 50494566 665c5457 fe8019 1d2021 fea052b3 fb4934 1d2021 fc7667cf 282828 ebdbb2 fe8019 fe8019 141617 665c5466 3c3836 3c3836 1d2021e6 a89984 3c3836b3 50494566 fe801933 fe8019 ff6369 4ade80 fbbf24 60a5fa', 'gruvbox-light': - 'fbf1c7 3c3836 a8998466 ebdbb2 0000008c ebdbb2 7c6f64 f2e5bc d5c4a166 bdae9366 af3a03 fbf1c7 953103b3 9d0006 fbf1c7 850005b3 fbf1c7 3c3836 af3a03 af3a03 fbf1c7 bdae9366 d5c4a1 d5c4a1 f2e5bce6 7c6f64 d5c4a1b3 d5c4a166 af3a0333 af3a03', + 'fbf1c7 3c3836 a8998466 ebdbb2 0000008c ebdbb2 5b524b f2e5bc d5c4a166 bdae9366 af3a03 fbf1c7 953103cc 9d0006 fbf1c7 850005b3 fbf1c7 3c3836 af3a03 af3a03 f2e5bc bdae9366 d5c4a1 d5c4a1 f2e5bce6 665c54 d5c4a1b3 d5c4a166 af3a0333 af3a03 9d0006 79740e b57614 076678', 'gruvbox-light-hard': - 'f9f5d7 3c3836 a8998466 ebdbb2 0000008c ebdbb2 7c6f64 f2e5bc d5c4a166 bdae9366 af3a03 f9f5d7 953103b3 9d0006 f9f5d7 850005b3 f9f5d7 3c3836 af3a03 af3a03 f9f5d7 bdae9366 d5c4a1 d5c4a1 f2e5bce6 7c6f64 d5c4a1b3 d5c4a166 af3a0333 af3a03', - nord: '2e3440 d8dee9 4c566a66 434c5e 0000008c eceff4 a0a8b6 3b4252 4c566a66 616a7d66 88c0d0 2e3440 a6d0dcb3 bf616a eceff4 cf888fb3 3b4252 d8dee9 88c0d0 88c0d0 2e3440 4c566a66 3b4252 3b4252 2e3440e6 a0a8b6 3b4252b3 4c566a66 88c0d033 88c0d0', + 'f9f5d7 3c3836 a8998466 ebdbb2 0000008c ebdbb2 5b524c f2e5bc d5c4a166 bdae9366 af3a03 f9f5d7 953103c9 9d0006 f9f5d7 850005b3 f9f5d7 3c3836 af3a03 af3a03 f2e5bc bdae9366 d5c4a1 d5c4a1 f2e5bce6 665c54 d5c4a1b3 d5c4a166 af3a0333 af3a03 9d0006 79740e b57614 076678', + nord: '2e3440 edf0f5 4c566a66 434c5e 0000008c eceff4 cbd0d8 3b4252 4c566a66 616a7d66 88c0d0 2e3440 a6d0dcb3 bf616a 0b0b0b cf888fb3 3b4252 edf0f5 88c0d0 88c0d0 272c36 4c566a66 3b4252 3b4252 2e3440e6 a0a8b6 3b4252b3 4c566a66 88c0d033 88c0d0 ff6369 4ade80 fbbf24 60a5fa', 'rose-pine-main': - '191724 e0def4 524f6766 403d52 0000008c e0def4 908caa 1f1d2e 403d5266 524f6766 c4a7e7 191724 d3bdedb3 eb6f92 191724 f093adb3 26233a e0def4 c4a7e7 c4a7e7 191724 524f6766 403d52 403d52 1f1d2ee6 908caa 403d52b3 403d5266 c4a7e733 c4a7e7', + '191724 e0def4 524f6766 383549 0000008c e0def4 aeacc3 1f1d2e 403d5266 524f6766 c4a7e7 191724 d3bdedb3 eb6f92 191724 f093adb3 26233a e0def4 c4a7e7 c4a7e7 16141f 524f6766 403d52 403d52 1f1d2ee6 908caa 403d52b3 403d5266 c4a7e733 c4a7e7 ff6369 4ade80 fbbf24 60a5fa', 'rose-pine-moon': - '232136 e0def4 56526e66 44415a 0000008c e0def4 908caa 2a273f 44415a66 56526e66 c4a7e7 232136 d3bdedb3 eb6f92 232136 f093adb3 393552 e0def4 c4a7e7 c4a7e7 232136 56526e66 44415a 44415a 2a273fe6 908caa 44415ab3 44415a66 c4a7e733 c4a7e7', + '232136 eeedf9 56526e66 403d56 0000008c e0def4 c4c2d3 2a273f 44415a66 56526e66 c4a7e7 232136 d3bdedb3 eb6f92 232136 f093adbd 393552 eeedf9 c4a7e7 c4a7e7 1f1d30 56526e66 44415a 44415a 2a273fe6 908caa 44415ab3 44415a66 c4a7e733 c4a7e7 ff6369 4ade80 fbbf24 60a5fa', 'rose-pine-dawn': - 'faf4ed 575279 cecacd66 dfdad9 0000008c faf4ed 797593 fffaf3 dfdad966 cecacd66 907aa9 fffaf3 7a6890b3 b4637a fffaf3 995468b3 f2e9e1 575279 907aa9 907aa9 faf4ed cecacd66 dfdad9 dfdad9 fffaf3e6 797593 dfdad9b3 dfdad966 907aa933 907aa9', + 'faf4ed 514c71 cecacd66 dfdad9 0000008c faf4ed 5d5976 fffaf3 dfdad966 cecacd66 82699e fffaf3 675779d9 af5971 fffaf3 894b5dd9 f2e9e1 514c71 907aa9 907aa9 f2e9e1 cecacd66 dfdad9 dfdad9 fffaf3e6 67637f dfdad9b3 dfdad966 907aa933 907aa9 b4637a 286983 be7614 538f9a', 'solarized-dark': - '002b36 93a1a1 586e7566 1f4751 0000008c eee8d5 839496 073642 586e7566 657b8366 268bd2 002b36 5ca8ddb3 dc322f fdf6e3 e56563b3 073642 93a1a1 268bd2 268bd2 002b36 586e7566 073642 073642 002b36e6 839496 073642b3 586e7566 268bd233 268bd2', + '002b36 b2bcbc 586e7566 163f49 0000008c eee8d5 a0acac 073642 586e7533 657b8333 268bd2 00212b 5ca8ddc4 da2825 fdf6e3 e56563b3 073642 b2bcbc 268bd2 268bd2 00212b 586e7553 073642 073642 002b36e6 839496 073642b3 586e753d 268bd22a 268bd2 ff6369 4ade80 fbbf24 60a5fa', 'solarized-light': - 'fdf6e3 586e75 93a1a166 d3d3c5 0000008c fdf6e3 657b83 eee8d5 93a1a166 83949666 268bd2 fdf6e3 2076b2b3 dc322f fdf6e3 bb2a28b3 fdf6e3 586e75 268bd2 268bd2 fdf6e3 93a1a166 eee8d5 eee8d5 eee8d5e6 657b83 eee8d5b3 93a1a166 268bd233 268bd2' + 'fdf6e3 405055 93a1a166 dcdacb 0000008c fdf6e3 4b5b60 eee8d5 93a1a133 83949636 2076b2 fdf6e3 1b6597df da2825 fdf6e3 bb2a28dc fdf6e3 405055 268bd2 268bd2 eee8d5 93a1a155 e4ddc8 eee8d5 eee8d5e6 55676d eee8d5b3 93a1a13f 268bd22d 268bd2 dc322f 849800 b48800 268bd2' }; const kinds: Record = { diff --git a/src/lib/readability.ts b/src/lib/readability.ts new file mode 100644 index 0000000..6dd500d --- /dev/null +++ b/src/lib/readability.ts @@ -0,0 +1,60 @@ +import { contrast, over, parse, type Rgba } from './color'; +import { darkColors } from './mock/theme'; +import { pairings, type Pairing } from './theme-tokens'; + +/** + * How well theme colours read where Sonora draws them, shared by the theme creator, the submit + * form and the worker that files submissions. It imports nothing through `$lib`, since the + * worker bundles it on its own. + */ + +/** A pairing as it reads with one theme's colours. */ +export type Reading = Pairing & { ratio: number; level: 'good' | 'low' | 'bad' }; + +/** Below this text does not read. A theme with such text is refused, while shapes only warn. */ +export const UNREADABLE = 3; +/** What text needs to read comfortably. */ +const TEXT = 4.5; + +const BLACK: Rgba = { r: 0, g: 0, b: 0, a: 1 }; + +/** + * Every pairing of `colors` with its contrast, worst first. Tokens the theme leaves out come + * from the Dark theme, as they do in Sonora, and translucent colours are laid over what they + * cover before they are measured. + */ +export function readings(colors: Record): Reading[] { + const color = (token: string): Rgba => parse(colors[token] ?? darkColors[token]) ?? BLACK; + const flat = (token: string): Rgba => + token === 'background' ? over(color(token), BLACK) : over(color(token), flat('background')); + return pairings + .map((pairing): Reading => { + const ground = pairing.over ? over(color(pairing.on), flat(pairing.over)) : flat(pairing.on); + const ratio = contrast(over(color(pairing.front), ground), ground); + const need = pairing.kind === 'text' ? TEXT : UNREADABLE; + const level = + ratio >= need ? 'good' : ratio >= UNREADABLE || pairing.kind === 'graphic' ? 'low' : 'bad'; + return { ...pairing, ratio, level }; + }) + .toSorted((a, b) => a.ratio - b.ratio); +} + +/** The text pairings of `colors` that do not read at all. */ +export function unreadable(colors: Record): Reading[] { + return readings(colors).filter((reading) => reading.level === 'bad'); +} + +/** A contrast ratio as people write it, such as `2.25:1`. */ +export function ratio(value: number): string { + return `${value < 10 ? value.toFixed(2).replace(/\.?0+$/, '') : Math.round(value)}:1`; +} + +/** Why a theme is refused, naming its worst pair, or null when every pair reads. */ +export function refusal(name: string, colors: Record): string | null { + const bad = unreadable(colors); + if (!bad.length) return null; + const [worst] = bad; + const surface = worst.over && worst.over !== 'background' ? ` over ${worst.over}` : ''; + const more = bad.length > 1 ? ` and ${bad.length - 1} more` : ''; + return `${name} is unreadable: ${worst.front} on ${worst.on}${surface} is ${ratio(worst.ratio)}${more}. Text needs at least ${UNREADABLE}:1`; +} diff --git a/src/lib/scheme.svelte.ts b/src/lib/scheme.svelte.ts index c6c9d04..8d37afa 100644 --- a/src/lib/scheme.svelte.ts +++ b/src/lib/scheme.svelte.ts @@ -84,6 +84,20 @@ export function swatch(name: string) { } let current = $state(ADAPTIVE); +/** Whether `current` holds the saved scheme or a choice made since, so storage is read only once. */ +let synced = false; +/** Whether a page has asked for plain Dark or Light in place of the chosen scheme. */ +let pinned = $state(false); + +/** The plain Dark or Light that stands in for `name` on a page that must not compete with what it shows. */ +export const plain = (name: string) => (lit(name) ? 'Light' : 'Dark'); + +/** Puts `name` on the page root, where the stylesheet and the light rules read it. */ +function wear(name: string) { + const root = document.documentElement; + root.dataset.scheme = slug(name); + root.dataset.theme = lit(name) ? 'light' : 'dark'; +} function stored() { try { @@ -108,7 +122,30 @@ export const scheme = { }, sync() { - if (browser) current = stored(); + if (!browser || synced) return; + current = stored(); + synced = true; + }, + + /** Whether the page wears plain Dark or Light in place of the chosen scheme. */ + get pinned() { + return pinned; + }, + + /** + * Wears plain Dark or Light, matching how light the chosen scheme is, until `unpin`. The chosen + * scheme stays saved, so other pages keep it. A page loaded directly mounts before the layout + * syncs, so the saved scheme is read here first. + */ + pin() { + pinned = true; + this.sync(); + wear(plain(current)); + }, + + unpin() { + pinned = false; + wear(current); }, /** Puts on `name`, crossfading the whole page where the browser can and the visitor allows motion. */ @@ -121,8 +158,8 @@ export const scheme = { const root = document.documentElement; const apply = async () => { current = name; - root.dataset.scheme = slug(name); - root.dataset.theme = lit(name) ? 'light' : 'dark'; + synced = true; + wear(pinned ? plain(name) : name); try { if (name === ADAPTIVE) localStorage.removeItem(KEY); diff --git a/src/lib/theme-tokens.ts b/src/lib/theme-tokens.ts new file mode 100644 index 0000000..2ef7a7c --- /dev/null +++ b/src/lib/theme-tokens.ts @@ -0,0 +1,115 @@ +/** A group of colour tokens that paint one part of the window. */ +export type TokenGroup = { area: string; tokens: string[] }; + +/** + * A pair of colours Sonora draws one on the other. `over` is the opaque colour under a + * translucent `on`. `text` pairs need 4.5:1 to read, graphic ones 3:1 to be seen. + */ +export type Pairing = { front: string; on: string; over?: string; kind: 'text' | 'graphic' }; + +/** + * Every colour token Sonora draws, grouped the way the docs and the theme creator list them. + * `table_active_border` is left out: theme files may still set it, but Sonora no longer draws it. + */ +export const groups: TokenGroup[] = [ + { + area: 'Page and text', + tokens: ['background', 'foreground', 'muted_foreground', 'muted', 'border'] + }, + { + area: 'Buttons', + tokens: [ + 'secondary', + 'secondary_hover', + 'secondary_active', + 'primary', + 'primary_foreground', + 'primary_hover', + 'danger', + 'danger_foreground', + 'danger_hover' + ] + }, + { + area: 'Chrome', + tokens: ['sidebar', 'sidebar_accent', 'sidebar_border', 'title_bar_border'] + }, + { + area: 'Menus and overlays', + tokens: ['popover', 'popover_foreground', 'overlay', 'overlay_foreground'] + }, + { + area: 'Lists and tables', + tokens: [ + 'table_head', + 'table_head_foreground', + 'table_row_border', + 'table_hover', + 'table_active' + ] + }, + { + area: 'Accents', + tokens: [ + 'progress_bar', + 'selection', + 'status_error', + 'status_success', + 'status_warning', + 'status_info' + ] + } +]; + +/** The colour pairs Sonora draws one on the other, which a theme has to keep apart. */ +export const pairings: Pairing[] = [ + { front: 'foreground', on: 'background', kind: 'text' }, + { front: 'muted_foreground', on: 'background', kind: 'text' }, + { front: 'foreground', on: 'sidebar', kind: 'text' }, + { front: 'muted_foreground', on: 'sidebar', kind: 'text' }, + { front: 'foreground', on: 'sidebar_accent', over: 'sidebar', kind: 'text' }, + { front: 'foreground', on: 'secondary', over: 'background', kind: 'text' }, + { front: 'foreground', on: 'secondary', over: 'popover', kind: 'text' }, + { front: 'foreground', on: 'secondary_hover', over: 'background', kind: 'text' }, + { front: 'foreground', on: 'secondary_active', over: 'background', kind: 'text' }, + { front: 'muted_foreground', on: 'secondary_active', over: 'background', kind: 'text' }, + { front: 'popover_foreground', on: 'secondary_active', over: 'popover', kind: 'text' }, + { front: 'foreground', on: 'muted', over: 'background', kind: 'text' }, + { front: 'foreground', on: 'table_hover', over: 'background', kind: 'text' }, + { front: 'foreground', on: 'table_active', over: 'background', kind: 'text' }, + { front: 'table_head_foreground', on: 'table_head', over: 'background', kind: 'text' }, + { front: 'primary_foreground', on: 'primary', over: 'background', kind: 'text' }, + { front: 'primary_foreground', on: 'primary_hover', over: 'background', kind: 'text' }, + { front: 'danger_foreground', on: 'danger', over: 'background', kind: 'text' }, + { front: 'danger_foreground', on: 'danger', over: 'popover', kind: 'text' }, + { front: 'danger_foreground', on: 'danger_hover', over: 'background', kind: 'text' }, + { front: 'primary_foreground', on: 'primary_hover', over: 'popover', kind: 'text' }, + { front: 'danger_foreground', on: 'danger_hover', over: 'popover', kind: 'text' }, + { front: 'popover_foreground', on: 'popover', over: 'background', kind: 'text' }, + { front: 'popover_foreground', on: 'secondary_hover', over: 'popover', kind: 'text' }, + { front: 'muted_foreground', on: 'muted', over: 'background', kind: 'text' }, + { front: 'muted_foreground', on: 'muted', over: 'sidebar', kind: 'text' }, + { front: 'muted_foreground', on: 'secondary', over: 'background', kind: 'text' }, + { front: 'muted_foreground', on: 'popover', over: 'background', kind: 'text' }, + { front: 'muted_foreground', on: 'muted', over: 'popover', kind: 'text' }, + { front: 'muted_foreground', on: 'secondary', over: 'popover', kind: 'text' }, + { front: 'muted_foreground', on: 'secondary', over: 'sidebar', kind: 'text' }, + { front: 'muted_foreground', on: 'table_active', over: 'background', kind: 'text' }, + { front: 'muted_foreground', on: 'table_hover', over: 'background', kind: 'text' }, + { front: 'muted_foreground', on: 'table_active', over: 'popover', kind: 'text' }, + { front: 'muted_foreground', on: 'table_hover', over: 'popover', kind: 'text' }, + { front: 'muted_foreground', on: 'secondary_active', over: 'popover', kind: 'text' }, + { front: 'muted_foreground', on: 'secondary_hover', over: 'popover', kind: 'text' }, + { front: 'muted_foreground', on: 'sidebar_accent', over: 'sidebar', kind: 'text' }, + { front: 'muted_foreground', on: 'table_active', over: 'sidebar', kind: 'text' }, + { front: 'muted_foreground', on: 'table_hover', over: 'sidebar', kind: 'text' }, + { front: 'muted_foreground', on: 'secondary_hover', over: 'background', kind: 'text' }, + { front: 'muted_foreground', on: 'secondary_active', over: 'sidebar', kind: 'text' }, + { front: 'muted_foreground', on: 'secondary_hover', over: 'sidebar', kind: 'text' }, + { front: 'primary', on: 'background', kind: 'graphic' }, + { front: 'progress_bar', on: 'sidebar', kind: 'graphic' }, + { front: 'status_error', on: 'popover', kind: 'graphic' }, + { front: 'status_success', on: 'popover', kind: 'graphic' }, + { front: 'status_warning', on: 'popover', kind: 'graphic' }, + { front: 'status_info', on: 'popover', kind: 'graphic' } +]; diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index a93d3e3..d1b381c 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,4 +1,5 @@ @@ -119,7 +71,8 @@

- To share your theme, submit it. + The theme creator shows where each color goes. To share your theme, + submit it.

Variants

@@ -138,12 +91,12 @@

Color tokens

- {#each tokens as group (group.area)} + {#each groups as group (group.area)} diff --git a/src/routes/themes/+page.svelte b/src/routes/themes/+page.svelte index a8d7273..af206af 100644 --- a/src/routes/themes/+page.svelte +++ b/src/routes/themes/+page.svelte @@ -87,12 +87,17 @@
{group.area} - {#each group.names as name, index (name)}{name}{index < - group.names.length - 1 + {#each group.tokens as name, index (name)}{name}{index < + group.tokens.length - 1 ? ' ' : ''}{/each}