diff --git a/bundles/registry.json b/bundles/registry.json index bdb7f991..72a12a01 100644 --- a/bundles/registry.json +++ b/bundles/registry.json @@ -689,7 +689,7 @@ { "id": "ryoku-zed-theme", "name": "Ryoku for Zed", - "version": "1.2.0", + "version": "1.2.1", "path": "bundles/ryoku-zed-theme", "author": "Ryoku Team", "upstream": "https://github.com/Ryoku-dev/ryostore/tree/main/bundles/ryoku-zed-theme", @@ -712,7 +712,7 @@ "assets/ryoku-blue.png" ], "manifest": "bundle.json", - "manifestSha256": "1c78bdd582669d295ad3353e85a20b4690ca89b81973ed40d99c12952c823e5f", + "manifestSha256": "f26d235f7867bd21a419a80596ffa2abcd3f88db5b22470196131608e03820c0", "components": [ { "type": "package", diff --git a/bundles/ryoku-zed-theme/bundle.json b/bundles/ryoku-zed-theme/bundle.json index f3b6d39d..fa60cb8a 100644 --- a/bundles/ryoku-zed-theme/bundle.json +++ b/bundles/ryoku-zed-theme/bundle.json @@ -2,7 +2,7 @@ "schema": 1, "id": "ryoku-zed-theme", "category": "bundles", - "version": "1.2.0", + "version": "1.2.1", "destination": "ryoku/bundles/ryoku-zed-theme", "name": "Ryoku for Zed", "description": "Keep Zed's UI and syntax colors in step with Ryoku's wallpaper palette, including monochrome wallpapers.", @@ -104,8 +104,8 @@ "source": "runtime/scripts/theme.ts", "destination": "runtime/scripts/theme.ts", "mode": "0644", - "size": 12406, - "sha256": "0618c3c2f3ea142a885ee0d4a2e77a2ce1e1358973c0029e4c9a13805547f968", + "size": 15954, + "sha256": "6d4ec206efe735ac7a4ac26840e6ab0af255acf7c507327ac7525d94140f2339", "install": false }, { diff --git a/bundles/ryoku-zed-theme/runtime/scripts/theme.ts b/bundles/ryoku-zed-theme/runtime/scripts/theme.ts index a048177e..b643e35e 100644 --- a/bundles/ryoku-zed-theme/runtime/scripts/theme.ts +++ b/bundles/ryoku-zed-theme/runtime/scripts/theme.ts @@ -107,6 +107,8 @@ const syntaxHueOffsets: Record = { "text.literal": 40, "emphasis.strong": 0, "constructor": 280, + "variant": 280, + "enum": 240, "boolean": 160, "constant": 120, "function": 0, @@ -221,6 +223,52 @@ function syntheticMonochromeVariants(fallback: Json): Json[] { }); } +function surfaceLuminance(style: Json): number | null { + const bg = style["editor.background"] ?? style.background; + const rgb = hex(bg); + return rgb ? luminance(rgb) : null; +} + +// Ryoku's palette plane can render a single (dark) scheme into both template +// variants, leaving the "light" theme with a dark surface. When a variant +// arrives with the wrong surface for its appearance, rebuild it from the +// opposite variant: hue and chroma are preserved and only lightness is +// mirrored across the source theme's own background-to-text range, then +// improve() re-enforces WCAG contrast on the result. +function deriveOppositeVariant(source: Json, appearance: "light" | "dark"): Json { + const theme = structuredClone(source); + theme.appearance = appearance; + const style = theme.style as Json; + const srcStyle = source.style as Json; + const bgL = toOklch((srcStyle["editor.background"] ?? srcStyle.background) as string)?.l ?? 0.2; + const textL = toOklch(srcStyle.text as string)?.l ?? (appearance === "light" ? 0.9 : 0.2); + const targetBg = appearance === "light" ? 0.97 : 0.17; + const targetText = appearance === "light" ? 0.24 : 0.93; + const direction = textL >= bgL ? 1 : -1; + const span = Math.max(0.05, Math.abs(textL - bgL)); + const remap = (color: unknown): unknown => { + if (typeof color !== "string") return color; + const alpha = color.match(/^#?[\da-f]{6}([\da-f]{2})?$/i)?.[1]; + const parsed = toOklch(color); + if (parsed === null || !/^#?[\da-f]{6}([\da-f]{2})?$/i.test(color)) return color; + const rel = Math.min(1.25, Math.max(-0.25, ((parsed.l - bgL) * direction) / span)); + const l = Math.min(0.99, Math.max(0.02, targetBg + rel * (targetText - targetBg))); + const mapped = fromOklch({ l, c: parsed.c, h: parsed.h }); + return alpha ? `${mapped}${alpha}` : mapped; + }; + for (const [key, value] of Object.entries(style)) { + if (typeof value === "string") style[key] = remap(value); + else if (value && typeof value === "object" && !Array.isArray(value) && typeof (value as Json).color === "string") + (value as Json).color = remap((value as Json).color); + } + if (Array.isArray(style.accents)) style.accents = style.accents.map(remap); + if (Array.isArray(style.players)) for (const player of style.players as Json[]) { + if (!player) continue; + for (const key of ["cursor", "background", "selection"]) if (player[key]) player[key] = remap(player[key]); + } + return theme; +} + function checkHueSeparation(theme: Json) { const style = theme.style as Json; const categories = ["function", "string", "number", "constant", "boolean", "keyword", "type", "constructor", "operator"]; @@ -237,6 +285,7 @@ function checkHueSeparation(theme: Json) { } let family: Json; +const syncNotes: string[] = []; if (mode === "bootstrap") { family = await readJson(checkedIn); } else if (mode === "verify-monochrome") { @@ -254,6 +303,21 @@ if (mode === "bootstrap") { name: `Ryoku Dynamic ${theme.appearance === "light" ? "Light" : "Dark"}`, })); family.$schema = "https://zed.dev/schema/themes/v0.2.0.json"; + const dark = family.themes.find((theme: Json) => theme.appearance === "dark"); + const light = family.themes.find((theme: Json) => theme.appearance === "light"); + if (dark && light) { + const lightLum = surfaceLuminance(light.style as Json); + const darkLum = surfaceLuminance(dark.style as Json); + if (lightLum !== null && lightLum < 0.45) { + family.themes = family.themes.map((theme: Json) => + theme === light ? { ...deriveOppositeVariant(dark, "light"), name: light.name } : theme); + syncNotes.push(`light variant derived from dark (source rendered a dark surface, luminance ${lightLum.toFixed(3)})`); + } else if (darkLum !== null && darkLum >= 0.45) { + family.themes = family.themes.map((theme: Json) => + theme === dark ? { ...deriveOppositeVariant(light, "dark"), name: dark.name } : theme); + syncNotes.push(`dark variant derived from light (source rendered a light surface, luminance ${darkLum.toFixed(3)})`); + } + } } let report = ""; @@ -275,4 +339,5 @@ if (mode === "bootstrap") { await mkdir(dirname(outputPath), { recursive: true }); await writeFile(outputPath, `${JSON.stringify(family, null, 2)}\n`); console.log(`Wrote ${outputPath}\n${report.trim()}`); + for (const note of syncNotes) console.log(`note: ${note}`); }