Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/core/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).
Expand All @@ -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). */
Expand All @@ -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
Expand Down
20 changes: 11 additions & 9 deletions src/v2/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ColorInput } from "@opentui/core"

/**
* V2 (opencode2) TUI plugin API — 最小本地类型(实验版)。
* 运行时由 opencode2 提供,此处仅用于本地类型检查;
Expand All @@ -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 }
}
}
}
Expand Down