Repository navigation
feat: CSS system colors map to the ANSI 16-color palette #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
natemoo-re
wants to merge
4
commits into
feat/hex-colors
Choose a base branch
from
feat/ansi16-system-colors
base: feat/hex-colors
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a3908f4
spec: CSS system colors map to the terminal's ANSI 16-color palette (…
natemoo-re 9c742ac
feat: CSS system colors render with the terminal's ANSI 16-color palette
natemoo-re 7ba0b65
spec: cross-field default keywords resolve to probed theme colors (§8.5)
natemoo-re d9caccc
feat: resolve cross-field default keywords against the probed theme
natemoo-re File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --- | ||
| "@bomb.sh/tty": minor | ||
| --- | ||
|
|
||
| Color fields accept the CSS system color keywords (`Canvas`, `CanvasText`, | ||
| `LinkText`, `Highlight`, `GrayText`, `Mark`, `AccentColor`, ...), | ||
| case-insensitively. Instead of a fixed RGB value, each one renders with the | ||
| terminal's own theme: the default foreground or background, or an entry of the | ||
| ANSI 16-color palette (`LinkText` is blue, `GrayText` is bright black, and so | ||
| on), so output follows the user's light or dark terminal theme. The | ||
| default-color keywords mean the default of the attribute they set, so | ||
| `text({ bg: "Canvas" })` paints the terminal default background over any | ||
| enclosing element background, while an omitted `bg` still keeps it. Named in | ||
| the other field instead, a default keyword now paints the terminal's actual | ||
| default color once it's known: `text({ bg: "CanvasText" })` renders the | ||
| terminal's real default foreground color as an opaque truecolor background, | ||
| learned from the terminal's OSC 10/11 theme report, falling back to the | ||
| default background until that report arrives. Packed `rgba()` colors and | ||
| other color strings render exactly as before. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,99 @@ | ||
| import { convert, parse } from "hueful"; | ||
|
|
||
| /** | ||
| * A §8.5 terminal-themed color: an ANSI palette index (0–15), or a default | ||
| * keyword naming the terminal default foreground ("fg-default") or | ||
| * background ("bg-default"). Which default it names is fixed by the | ||
| * keyword; whether it stays terminal-themed or resolves to a probed sRGB | ||
| * color depends on the field it's packed into (see resolveForField). | ||
| */ | ||
| export interface TerminalColor { | ||
| ansi: number | "fg-default" | "bg-default"; | ||
| } | ||
|
|
||
| /** An 8-bit RGB color, structurally compatible with terminfo.ts's Rgb. */ | ||
| export interface ThemeRgb { | ||
| r: number; | ||
| g: number; | ||
| b: number; | ||
| } | ||
|
|
||
| /** The theme colors `pack()` resolves cross-field default keywords against. */ | ||
| export interface ThemeColors { | ||
| readonly foreground?: ThemeRgb; | ||
| readonly background?: ThemeRgb; | ||
| } | ||
|
|
||
| export const systemColors = { | ||
| canvas: "bg-default", | ||
| buttonface: "bg-default", | ||
| field: "bg-default", | ||
| canvastext: "fg-default", | ||
| buttontext: "fg-default", | ||
| fieldtext: "fg-default", | ||
| activetext: 1, | ||
| mark: 3, | ||
| linktext: 4, | ||
| highlight: 4, | ||
| selecteditem: 4, | ||
| visitedtext: 5, | ||
| accentcolor: 6, | ||
| graytext: 8, | ||
| buttonborder: 8, | ||
| highlighttext: 15, | ||
| selecteditemtext: 15, | ||
| marktext: 0, | ||
| accentcolortext: 0, | ||
| } as const satisfies Record<string, TerminalColor["ansi"]>; | ||
|
|
||
| function byte(unit: number): number { | ||
| return Math.round(Math.min(Math.max(unit, 0), 1) * 255); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a §8.5 CSS color string to the packed ARGB number the transfer | ||
| * encoding carries, or `undefined` when the string is not a supported color. | ||
| * Channels clamp to the sRGB range, so out-of-gamut colors clip per channel. | ||
| * encoding carries, a terminal-themed color for a system color keyword, or | ||
| * `undefined` when the string is not a supported color. Channels clamp to | ||
| * the sRGB range, so out-of-gamut colors clip per channel. | ||
| */ | ||
| export function resolveColor(input: string): number | undefined { | ||
| export function resolveColor( | ||
| input: string, | ||
| ): number | TerminalColor | undefined { | ||
| let lower = input.toLowerCase(); | ||
| if (Object.hasOwn(systemColors, lower)) { | ||
| return { ansi: systemColors[lower as keyof typeof systemColors] }; | ||
| } | ||
| let parsed = parse(input); | ||
| if (parsed === null || parsed.space === "cmyk") return undefined; | ||
| let [r, g, b, alpha = 1] = convert(parsed.space, "rgb", parsed.value); | ||
| return (byte(alpha) << 24) | (byte(r / 255) << 16) | (byte(g / 255) << 8) | | ||
| byte(b / 255); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a terminal-themed color for the field it was packed into (§8.5). | ||
| * A same-field default keyword and a palette entry stay terminal-themed. A | ||
| * cross-field default keyword (a fg-default keyword in a bg field, or a | ||
| * bg-default keyword in a fg field) resolves to the probed theme color for | ||
| * the attribute it names, packed as an ordinary opaque ARGB number; absent | ||
| * that probe, it falls back to the field's own terminal default. | ||
| */ | ||
| export function resolveForField( | ||
| color: TerminalColor, | ||
| field: "fg" | "bg", | ||
| theme: ThemeColors | undefined, | ||
| ): number | TerminalColor { | ||
| let { ansi } = color; | ||
| if (ansi !== "fg-default" && ansi !== "bg-default") return color; | ||
| if ( | ||
| (ansi === "fg-default" && field === "fg") || | ||
| (ansi === "bg-default" && field === "bg") | ||
| ) { | ||
| return color; | ||
| } | ||
| let rgb = ansi === "fg-default" ? theme?.foreground : theme?.background; | ||
| if (rgb === undefined) { | ||
| return { ansi: field === "fg" ? "fg-default" : "bg-default" }; | ||
| } | ||
| return (255 << 24) | (rgb.r << 16) | (rgb.g << 8) | rgb.b; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changeset needs revision.
Changeset should start with an approved active present-tense verb. Changeset package scope does not match the affected packages confidently. Changeset does not name the affected public API confidently.
View the proposed replacement