starting janitorial work

This commit is contained in:
2026-02-06 13:41:44 -05:00
parent 920042ee2a
commit 1293d30225
4 changed files with 383 additions and 298 deletions

View File

@@ -1,36 +1,49 @@
import { RGBA, type TerminalColors } from "@opentui/core"
import { ansiToRgba } from "./ansi-to-rgba"
import { generateGrayScale, generateMutedTextColor, tint } from "./color-generation"
import type { ThemeJson } from "../types/theme-schema"
import { RGBA, type TerminalColors } from "@opentui/core";
import { ansiToRgba } from "./ansi-to-rgba";
import {
generateGrayScale,
generateMutedTextColor,
tint,
} from "./color-generation";
import type { ThemeJson } from "../types/theme-schema";
let cached: TerminalColors | null = null
let cached: TerminalColors | null = null;
export function clearPaletteCache() {
cached = null
cached = null;
}
export function detectSystemTheme(colors: TerminalColors) {
const bg = RGBA.fromHex(colors.defaultBackground ?? colors.palette[0] ?? "#000000")
const luminance = 0.299 * bg.r + 0.587 * bg.g + 0.114 * bg.b
const mode = luminance > 0.5 ? "light" : "dark"
return { mode, background: bg }
const bg = RGBA.fromHex(
colors.defaultBackground ?? colors.palette[0] ?? "#000000",
);
const luminance = 0.299 * bg.r + 0.587 * bg.g + 0.114 * bg.b;
const mode = luminance > 0.5 ? "light" : "dark";
return { mode, background: bg };
}
export function generateSystemTheme(colors: TerminalColors, mode: "dark" | "light"): ThemeJson {
cached = colors
const bg = RGBA.fromHex(colors.defaultBackground ?? colors.palette[0] ?? "#000000")
const fg = RGBA.fromHex(colors.defaultForeground ?? colors.palette[7] ?? "#ffffff")
const transparent = RGBA.fromInts(0, 0, 0, 0)
const isDark = mode === "dark"
export function generateSystemTheme(
colors: TerminalColors,
mode: "dark" | "light",
): ThemeJson {
cached = colors;
const bg = RGBA.fromHex(
colors.defaultBackground ?? colors.palette[0] ?? "#000000",
);
const fg = RGBA.fromHex(
colors.defaultForeground ?? colors.palette[7] ?? "#ffffff",
);
const transparent = RGBA.fromInts(0, 0, 0, 0);
const isDark = mode === "dark";
const col = (i: number) => {
const value = colors.palette[i]
if (value) return RGBA.fromHex(value)
return ansiToRgba(i)
}
const value = colors.palette[i];
if (value) return RGBA.fromHex(value);
return ansiToRgba(i);
};
const grays = generateGrayScale(bg, isDark)
const textMuted = generateMutedTextColor(bg, isDark)
const grays = generateGrayScale(bg, isDark);
const textMuted = generateMutedTextColor(bg, isDark);
const ansi = {
black: col(0),
@@ -43,13 +56,13 @@ export function generateSystemTheme(colors: TerminalColors, mode: "dark" | "ligh
white: col(7),
redBright: col(9),
greenBright: col(10),
}
};
const diffAlpha = isDark ? 0.22 : 0.14
const diffAddedBg = tint(bg, ansi.green, diffAlpha)
const diffRemovedBg = tint(bg, ansi.red, diffAlpha)
const diffAddedLineNumberBg = tint(grays[3], ansi.green, diffAlpha)
const diffRemovedLineNumberBg = tint(grays[3], ansi.red, diffAlpha)
const diffAlpha = isDark ? 0.22 : 0.14;
const diffAddedBg = tint(bg, ansi.green, diffAlpha);
const diffRemovedBg = tint(bg, ansi.red, diffAlpha);
const diffAddedLineNumberBg = tint(grays[3], ansi.green, diffAlpha);
const diffRemovedLineNumberBg = tint(grays[3], ansi.red, diffAlpha);
return {
theme: {
@@ -68,7 +81,7 @@ export function generateSystemTheme(colors: TerminalColors, mode: "dark" | "ligh
backgroundElement: grays[3],
backgroundMenu: grays[3],
borderSubtle: grays[6],
border: grays[7],
border: fg,
borderActive: grays[8],
diffAdded: ansi.green,
diffRemoved: ansi.red,
@@ -106,5 +119,5 @@ export function generateSystemTheme(colors: TerminalColors, mode: "dark" | "ligh
syntaxOperator: ansi.cyan,
syntaxPunctuation: fg,
},
}
};
}