From b707c03a19b16cdc7511dd33f3bd5015da140a0e Mon Sep 17 00:00:00 2001 From: Movoid Date: Tue, 10 Nov 2026 20:55:35 +0800 Subject: [PATCH] fix: preserve theme alpha in color transformations --- src/core/color.ts | 25 ++++++++++++++++++++++--- src/v2/types.ts | 20 +++++++++++--------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/core/color.ts b/src/core/color.ts index 8ac86bc..8f123c5 100644 --- a/src/core/color.ts +++ b/src/core/color.ts @@ -23,6 +23,25 @@ export function rgb(raw: unknown): { r: number; g: number; b: number } | null { return null } +/** Preserve alpha from both hex colours and the host's RGBA objects. */ +function alpha(raw: unknown): number { + if (typeof raw === "string" && raw.startsWith("#") && raw.length === 9) { + return parseInt(raw.slice(7), 16) + } + if (raw && typeof raw === "object") { + const a = (raw as { a?: unknown }).a + if (typeof a === "number" && Number.isFinite(a)) { + return Math.round(Math.max(0, Math.min(1, a)) * 255) + } + } + return 255 +} + +function toHex(r: number, g: number, b: number, a = 255): string { + const channels = a === 255 ? [r, g, b] : [r, g, b, a] + return "#" + channels.map(v => Math.max(0, Math.min(255, Math.round(v))).toString(16).padStart(2, "0")).join("") +} + /** HSL saturation of an RGB color (0–1). */ export function saturation(r: number, g: number, b: number): number { const max = Math.max(r, g, b) / 255 @@ -43,7 +62,7 @@ export function desaturateTo(raw: unknown, maxSat: number, fallback: string): st const sat = saturation(c.r, c.g, c.b) if (sat <= maxSat) { // already muted — return as hex - return "#" + [c.r, c.g, c.b].map((v) => v.toString(16).padStart(2, "0")).join("") + return toHex(c.r, c.g, c.b, alpha(raw)) } /** * Binary search for the optimal grey-mix ratio α (0…1). @@ -69,7 +88,7 @@ export function desaturateTo(raw: unknown, maxSat: number, fallback: string): st const nr = Math.round(c.r + (luma - c.r) * hi) const ng = Math.round(c.g + (luma - c.g) * hi) const nb = Math.round(c.b + (luma - c.b) * hi) - return "#" + [nr, ng, nb].map((v) => Math.max(0, Math.min(255, v)).toString(16).padStart(2, "0")).join("") + return toHex(nr, ng, nb, alpha(raw)) } /** Darken a hex colour by multiplying each channel by `factor` (0–1). */ @@ -79,7 +98,7 @@ export function dimColor(hex: string, factor = 0.5): string { const r = Math.round(c.r * factor) const g = Math.round(c.g * factor) const b = Math.round(c.b * factor) - return "#" + [r, g, b].map((v) => Math.max(0, Math.min(255, v)).toString(16).padStart(2, "0")).join("") + return toHex(r, g, b, alpha(hex)) } // Morandi fallbacks — used when a theme colour cannot be resolved diff --git a/src/v2/types.ts b/src/v2/types.ts index 3644ae8..b2e273f 100644 --- a/src/v2/types.ts +++ b/src/v2/types.ts @@ -1,3 +1,5 @@ +import type { ColorInput } from "@opentui/core" + /** * V2 (opencode2) TUI plugin API — 最小本地类型(实验版)。 * 运行时由 opencode2 提供,此处仅用于本地类型检查; @@ -13,21 +15,21 @@ export interface Theme { readonly hue: { /** V1 primary 的 V2 等价(v1-migrate.ts:interactive = hues.byToken.primary)。 * 取 300:更亮(暗背景下 400/500 偏深)。 */ - readonly interactive: { readonly 300: string } + readonly interactive: { readonly 300: ColorInput } /** 主题强调色别名。 */ - readonly accent: { readonly 500: string } + readonly accent: { readonly 500: ColorInput } } /** 文本色 token 存在两代命名:base/muted 与 default/subdued。宿主按其中一代提供, * 读取时以 base/muted 为准,故全部声明为可选。 */ readonly text: { - readonly base?: string - readonly muted?: string - readonly default?: string - readonly subdued?: string + readonly base?: ColorInput + readonly muted?: ColorInput + readonly default?: ColorInput + readonly subdued?: ColorInput readonly feedback: { - readonly success: { readonly base?: string; readonly default?: string } - readonly error: { readonly base?: string; readonly default?: string } - readonly warning: { readonly base?: string; readonly default?: string } + readonly success: { readonly base?: ColorInput; readonly default?: ColorInput } + readonly error: { readonly base?: ColorInput; readonly default?: ColorInput } + readonly warning: { readonly base?: ColorInput; readonly default?: ColorInput } } } }