@siiway/ui-core 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +14 -1
  3. package/dist/boot.d.ts +17 -0
  4. package/dist/boot.d.ts.map +1 -0
  5. package/dist/boot.js +29 -0
  6. package/dist/boot.js.map +1 -0
  7. package/dist/duration.d.ts +27 -0
  8. package/dist/duration.d.ts.map +1 -0
  9. package/dist/duration.js +87 -0
  10. package/dist/duration.js.map +1 -0
  11. package/dist/i18n/en.d.ts +3 -0
  12. package/dist/i18n/en.d.ts.map +1 -0
  13. package/dist/i18n/en.js +53 -0
  14. package/dist/i18n/en.js.map +1 -0
  15. package/dist/i18n/index.d.ts +11 -0
  16. package/dist/i18n/index.d.ts.map +1 -0
  17. package/dist/i18n/index.js +16 -0
  18. package/dist/i18n/index.js.map +1 -0
  19. package/dist/i18n/types.d.ts +66 -0
  20. package/dist/i18n/types.d.ts.map +1 -0
  21. package/dist/i18n/types.js +2 -0
  22. package/dist/i18n/types.js.map +1 -0
  23. package/dist/i18n/zh.d.ts +3 -0
  24. package/dist/i18n/zh.d.ts.map +1 -0
  25. package/dist/i18n/zh.js +53 -0
  26. package/dist/i18n/zh.js.map +1 -0
  27. package/dist/index.d.ts +12 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +12 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/scheme.d.ts +11 -0
  32. package/dist/scheme.d.ts.map +1 -0
  33. package/dist/scheme.js +13 -0
  34. package/dist/scheme.js.map +1 -0
  35. package/dist/theme/brand.d.ts +17 -0
  36. package/dist/theme/brand.d.ts.map +1 -0
  37. package/dist/theme/brand.js +25 -0
  38. package/dist/theme/brand.js.map +1 -0
  39. package/dist/theme/color.d.ts +26 -0
  40. package/dist/theme/color.d.ts.map +1 -0
  41. package/dist/theme/color.js +148 -0
  42. package/dist/theme/color.js.map +1 -0
  43. package/dist/theme/overrides.d.ts +12 -0
  44. package/dist/theme/overrides.d.ts.map +1 -0
  45. package/dist/theme/overrides.js +12 -0
  46. package/dist/theme/overrides.js.map +1 -0
  47. package/dist/tokens/canvas.d.ts +8 -0
  48. package/dist/tokens/canvas.d.ts.map +1 -0
  49. package/dist/tokens/canvas.js +7 -0
  50. package/dist/tokens/canvas.js.map +1 -0
  51. package/dist/tokens/neutral.d.ts +55 -0
  52. package/dist/tokens/neutral.d.ts.map +1 -0
  53. package/dist/tokens/neutral.js +110 -0
  54. package/dist/tokens/neutral.js.map +1 -0
  55. package/dist/tokens/radii.d.ts +8 -0
  56. package/dist/tokens/radii.d.ts.map +1 -0
  57. package/dist/tokens/radii.js +8 -0
  58. package/dist/tokens/radii.js.map +1 -0
  59. package/dist/tokens/shadows.d.ts +14 -0
  60. package/dist/tokens/shadows.d.ts.map +1 -0
  61. package/dist/tokens/shadows.js +21 -0
  62. package/dist/tokens/shadows.js.map +1 -0
  63. package/package.json +25 -2
  64. package/src/boot.ts +40 -0
  65. package/src/duration.ts +119 -0
  66. package/src/i18n/en.ts +63 -0
  67. package/src/i18n/index.ts +24 -0
  68. package/src/i18n/types.ts +76 -0
  69. package/src/i18n/zh.ts +62 -0
  70. package/src/index.ts +42 -0
  71. package/src/scheme.ts +19 -0
  72. package/src/theme/brand.ts +32 -0
  73. package/src/theme/color.ts +147 -0
  74. package/src/theme/overrides.ts +15 -0
  75. package/src/tokens/canvas.ts +8 -0
  76. package/src/tokens/neutral.ts +116 -0
  77. package/src/tokens/radii.ts +7 -0
  78. package/src/tokens/shadows.ts +27 -0
@@ -0,0 +1,119 @@
1
+ // Parse durations people type, like "60min", "1 hour", "30 days", into whole
2
+ // minutes or days, depending on the field's storage unit. A bare number is in
3
+ // the field's unit. The unit words come from the library's strings
4
+ // (`UiStrings.durationUnits`), so each language reads its own; English by
5
+ // default.
6
+
7
+ import { en } from "./i18n/en.js";
8
+
9
+ export type DurationUnit = "minutes" | "days";
10
+
11
+ /** The units a duration can be typed in. A month is 30 days, a year 365. */
12
+ export type DurationUnitKey = "s" | "m" | "h" | "d" | "w" | "mo" | "y";
13
+
14
+ /** One language's words for each unit, matched ignoring case. */
15
+ export type DurationUnitWords = Record<DurationUnitKey, readonly string[]>;
16
+
17
+ /**
18
+ * Parse a duration string. Returns the integer count in the field's
19
+ * `defaultUnit`, or `null` for an empty string ("use the site default"),
20
+ * or a `string` error code for an unparseable input.
21
+ */
22
+ export type ParseResult =
23
+ | { kind: "ok"; value: number }
24
+ | { kind: "blank" }
25
+ | { kind: "error"; reason: "format" | "non_positive" | "unknown_unit" };
26
+
27
+ function unitFor(
28
+ word: string,
29
+ units: DurationUnitWords | readonly DurationUnitWords[],
30
+ ): DurationUnitKey | null {
31
+ const tables =
32
+ "s" in units ? [units as DurationUnitWords] : (units as readonly DurationUnitWords[]);
33
+ for (const table of tables) {
34
+ for (const key of Object.keys(table) as DurationUnitKey[]) {
35
+ if (table[key].some((name) => name.toLowerCase() === word)) return key;
36
+ }
37
+ }
38
+ return null;
39
+ }
40
+
41
+ /** Reads `input` with the unit words of `units`, one language or several. */
42
+ export function parseDuration(
43
+ input: string,
44
+ defaultUnit: DurationUnit,
45
+ units: DurationUnitWords | readonly DurationUnitWords[] = en.durationUnits,
46
+ ): ParseResult {
47
+ const trimmed = input.trim();
48
+ if (!trimmed) return { kind: "blank" };
49
+
50
+ // Strict: a number, optional whitespace, optional unit word (letters, in
51
+ // any script). Reject anything else (commas, multiple numbers, units
52
+ // without a number, etc.).
53
+ const match = trimmed.match(/^(\d+(?:\.\d+)?)\s*(\p{L}*)$/u);
54
+ if (!match) return { kind: "error", reason: "format" };
55
+
56
+ const num = parseFloat(match[1]!);
57
+ if (!Number.isFinite(num) || num <= 0) {
58
+ return { kind: "error", reason: "non_positive" };
59
+ }
60
+
61
+ const unitRaw = (match[2] ?? "").toLowerCase();
62
+ const canonical = unitRaw === "" ? null : unitFor(unitRaw, units);
63
+ if (unitRaw !== "" && !canonical) {
64
+ return { kind: "error", reason: "unknown_unit" };
65
+ }
66
+
67
+ // Convert to minutes first, then down to the field's unit.
68
+ let totalMinutes: number;
69
+ switch (canonical) {
70
+ case "s":
71
+ totalMinutes = num / 60;
72
+ break;
73
+ case "m":
74
+ totalMinutes = num;
75
+ break;
76
+ case "h":
77
+ totalMinutes = num * 60;
78
+ break;
79
+ case "d":
80
+ totalMinutes = num * 60 * 24;
81
+ break;
82
+ case "w":
83
+ totalMinutes = num * 60 * 24 * 7;
84
+ break;
85
+ case "mo":
86
+ totalMinutes = num * 60 * 24 * 30;
87
+ break;
88
+ case "y":
89
+ totalMinutes = num * 60 * 24 * 365;
90
+ break;
91
+ case null:
92
+ // No unit — interpret in the field's default unit.
93
+ totalMinutes = defaultUnit === "minutes" ? num : num * 60 * 24;
94
+ break;
95
+ default: {
96
+ // Exhaustiveness check.
97
+ const _exhaustive: never = canonical;
98
+ void _exhaustive;
99
+ totalMinutes = num;
100
+ }
101
+ }
102
+
103
+ const value =
104
+ defaultUnit === "minutes" ? Math.round(totalMinutes) : Math.round(totalMinutes / (60 * 24));
105
+
106
+ // Round-down for very small inputs (e.g. "30s" in a days-unit field) but
107
+ // never let a positive duration round to 0 — that would mint zero-TTL
108
+ // tokens. Clamp to 1 of the field's unit.
109
+ if (value < 1) return { kind: "ok", value: 1 };
110
+ return { kind: "ok", value };
111
+ }
112
+
113
+ /**
114
+ * Format an integer count with its unit, in English. The duration field
115
+ * shows the app's language instead (`UiStrings.duration`).
116
+ */
117
+ export function formatDuration(value: number, defaultUnit: DurationUnit): string {
118
+ return en.duration(value, defaultUnit);
119
+ }
package/src/i18n/en.ts ADDED
@@ -0,0 +1,63 @@
1
+ import type { UiStrings } from "./types.js";
2
+
3
+ export const en: UiStrings = {
4
+ cancel: "Cancel",
5
+ clear: "Clear",
6
+ close: "Close",
7
+ required: "Required",
8
+
9
+ copy: "Copy",
10
+ copied: "Copied!",
11
+ copyFailed: "Copy failed",
12
+
13
+ saveChanges: "Save changes",
14
+ discardChanges: "Discard changes",
15
+
16
+ showPassword: "Show password",
17
+ hidePassword: "Hide password",
18
+
19
+ previous: "Previous",
20
+ next: "Next",
21
+ pageOf: (page, total) => `${page} / ${total}`,
22
+ totalItems: (count) => (count === 1 ? `${count} item` : `${count} items`),
23
+ pageNumber: "Page",
24
+ goToPage: "Go to page",
25
+ go: "Go",
26
+
27
+ pickColor: "Pick a color",
28
+ hexColor: "Hex color",
29
+ optionalHexColor: "Optional hex color",
30
+ invalidHexColor: "Enter a three- or six-digit hex color.",
31
+ colorArea: "Color saturation and brightness",
32
+ saturation: "Saturation",
33
+ brightness: "Brightness",
34
+ hue: "Hue",
35
+
36
+ theme: "Theme",
37
+ themeSystem: "System",
38
+ themeLight: "Light",
39
+ themeDark: "Dark",
40
+
41
+ search: "Search",
42
+ commandMove: "to move",
43
+ commandOpen: "to open",
44
+ commandClose: "to close",
45
+
46
+ onThisPage: "On this page",
47
+ openNavigation: "Open navigation",
48
+ primaryNavigation: "Primary",
49
+ duration: (value, unit) =>
50
+ unit === "minutes" ? `${value} min` : value === 1 ? "1 day" : `${value} days`,
51
+ durationUnits: {
52
+ s: ["s", "sec", "secs", "second", "seconds"],
53
+ m: ["m", "min", "mins", "minute", "minutes"],
54
+ h: ["h", "hr", "hrs", "hour", "hours"],
55
+ d: ["d", "day", "days"],
56
+ w: ["w", "wk", "wks", "week", "weeks"],
57
+ mo: ["mo", "mon", "month", "months"],
58
+ y: ["y", "yr", "yrs", "year", "years"],
59
+ },
60
+ durationInvalid: "Invalid format: try “60” or “1 hour”",
61
+ durationNotPositive: "Must be a positive duration",
62
+ durationUnknownUnit: "Unknown time unit",
63
+ };
@@ -0,0 +1,24 @@
1
+ import { en } from "./en.js";
2
+ import type { UiLocale, UiStrings } from "./types.js";
3
+ import { zh } from "./zh.js";
4
+
5
+ export type { UiLocale, UiStrings } from "./types.js";
6
+
7
+ export const UI_STRINGS: Record<UiLocale, UiStrings> = { en, zh };
8
+
9
+ /**
10
+ * The library locale for a BCP-47 tag: any `zh` tag (`zh`, `zh-CN`,
11
+ * `zh-Hans-CN`) is `zh`, everything else is `en`.
12
+ */
13
+ export function uiLocale(locale: string | null | undefined): UiLocale {
14
+ return locale?.trim().toLowerCase().split(/[-_]/)[0] === "zh" ? "zh" : "en";
15
+ }
16
+
17
+ /** The strings for a BCP-47 tag, with any of them replaced by `overrides`. */
18
+ export function pickStrings(
19
+ locale: string | null | undefined,
20
+ overrides?: Partial<UiStrings>,
21
+ ): UiStrings {
22
+ const base = UI_STRINGS[uiLocale(locale)];
23
+ return overrides ? { ...base, ...overrides } : base;
24
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * The text the library's own components show: button labels, feedback,
3
+ * `aria-label`s and placeholders. Everything else comes in as props, already
4
+ * translated by the app. Each locale satisfies this type, so a key missing
5
+ * from one fails the typecheck.
6
+ */
7
+ import type { DurationUnit, DurationUnitWords } from "../duration.js";
8
+
9
+ export interface UiStrings {
10
+ cancel: string;
11
+ clear: string;
12
+ close: string;
13
+ required: string;
14
+
15
+ copy: string;
16
+ copied: string;
17
+ copyFailed: string;
18
+
19
+ saveChanges: string;
20
+ discardChanges: string;
21
+
22
+ showPassword: string;
23
+ hidePassword: string;
24
+
25
+ previous: string;
26
+ next: string;
27
+ /** Pagination position, e.g. "2 / 7". */
28
+ pageOf: (page: number, total: number) => string;
29
+ totalItems: (count: number) => string;
30
+ /** Placeholder of the go-to-page field. */
31
+ pageNumber: string;
32
+ goToPage: string;
33
+ go: string;
34
+
35
+ pickColor: string;
36
+ hexColor: string;
37
+ optionalHexColor: string;
38
+ invalidHexColor: string;
39
+ colorArea: string;
40
+ saturation: string;
41
+ brightness: string;
42
+ hue: string;
43
+
44
+ /** Label of the theme switch. */
45
+ theme: string;
46
+ themeSystem: string;
47
+ themeLight: string;
48
+ themeDark: string;
49
+
50
+ /** The search button's label and the command palette's field name. */
51
+ search: string;
52
+ /** The command palette's key hints, each after its key: "↑ ↓ to move". */
53
+ commandMove: string;
54
+ commandOpen: string;
55
+ commandClose: string;
56
+
57
+ /** The outline's heading. */
58
+ onThisPage: string;
59
+ /** The top bar's menu button, which opens the navigation on a narrow screen. */
60
+ openNavigation: string;
61
+ /** Names the top bar's links for screen readers. */
62
+ primaryNavigation: string;
63
+
64
+ /** A duration as the duration field shows it, e.g. "30 days". It must read
65
+ * back with `parseDuration`. */
66
+ duration: (value: number, unit: DurationUnit) => string;
67
+ /** This language's words for each unit, which the duration field reads. */
68
+ durationUnits: DurationUnitWords;
69
+ /** The duration field's errors. */
70
+ durationInvalid: string;
71
+ durationNotPositive: string;
72
+ durationUnknownUnit: string;
73
+ }
74
+
75
+ /** Locales the library ships strings for. */
76
+ export type UiLocale = "en" | "zh";
package/src/i18n/zh.ts ADDED
@@ -0,0 +1,62 @@
1
+ import type { UiStrings } from "./types.js";
2
+
3
+ export const zh: UiStrings = {
4
+ cancel: "取消",
5
+ clear: "清除",
6
+ close: "关闭",
7
+ required: "必填",
8
+
9
+ copy: "复制",
10
+ copied: "已复制!",
11
+ copyFailed: "复制失败",
12
+
13
+ saveChanges: "保存更改",
14
+ discardChanges: "放弃更改",
15
+
16
+ showPassword: "显示密码",
17
+ hidePassword: "隐藏密码",
18
+
19
+ previous: "上一页",
20
+ next: "下一页",
21
+ pageOf: (page, total) => `${page} / ${total}`,
22
+ totalItems: (count) => `共 ${count} 项`,
23
+ pageNumber: "页数",
24
+ goToPage: "跳转到页",
25
+ go: "跳转",
26
+
27
+ pickColor: "选择颜色",
28
+ hexColor: "十六进制颜色",
29
+ optionalHexColor: "可选的十六进制颜色",
30
+ invalidHexColor: "请输入三位或六位十六进制色值。",
31
+ colorArea: "颜色饱和度和亮度",
32
+ saturation: "饱和度",
33
+ brightness: "亮度",
34
+ hue: "色相",
35
+
36
+ theme: "主题",
37
+ themeSystem: "跟随系统",
38
+ themeLight: "浅色",
39
+ themeDark: "深色",
40
+
41
+ search: "搜索",
42
+ commandMove: "移动",
43
+ commandOpen: "打开",
44
+ commandClose: "关闭",
45
+
46
+ onThisPage: "目录",
47
+ openNavigation: "打开导航",
48
+ primaryNavigation: "主导航",
49
+ duration: (value, unit) => (unit === "minutes" ? `${value} 分钟` : `${value} 天`),
50
+ durationUnits: {
51
+ s: ["秒", "秒钟"],
52
+ m: ["分", "分钟"],
53
+ h: ["时", "小时", "个小时"],
54
+ d: ["天", "日"],
55
+ w: ["周", "星期", "个星期"],
56
+ mo: ["月", "个月"],
57
+ y: ["年"],
58
+ },
59
+ durationInvalid: "格式无效,试试“60”或“1 小时”",
60
+ durationNotPositive: "时长必须大于零",
61
+ durationUnknownUnit: "无法识别的时间单位",
62
+ };
package/src/index.ts ADDED
@@ -0,0 +1,42 @@
1
+ export {
2
+ THEME_STORAGE_KEY,
3
+ isThemeMode,
4
+ resolveScheme,
5
+ type Scheme,
6
+ type ThemeMode,
7
+ } from "./scheme.js";
8
+ export { schemeBootScript, type SchemeBootOptions } from "./boot.js";
9
+
10
+ export { NEUTRAL, type NeutralTokens } from "./tokens/neutral.js";
11
+ export { RADII } from "./tokens/radii.js";
12
+ export { SHADOWS, type ShadowTokens } from "./tokens/shadows.js";
13
+ export { CANVAS } from "./tokens/canvas.js";
14
+
15
+ export {
16
+ normalizeHexColor,
17
+ normalizeAccent,
18
+ hexToHsl,
19
+ hslToHex,
20
+ hexToHsv,
21
+ hsvToHex,
22
+ type HsvColor,
23
+ } from "./theme/color.js";
24
+ export {
25
+ DEFAULT_ACCENT,
26
+ BRAND_SHADES,
27
+ brandRamp,
28
+ type BrandShade,
29
+ type BrandRamp,
30
+ } from "./theme/brand.js";
31
+ export { themeOverrides, type ThemeOverrides } from "./theme/overrides.js";
32
+
33
+ export { UI_STRINGS, uiLocale, pickStrings, type UiLocale, type UiStrings } from "./i18n/index.js";
34
+
35
+ export {
36
+ parseDuration,
37
+ formatDuration,
38
+ type DurationUnit,
39
+ type DurationUnitKey,
40
+ type DurationUnitWords,
41
+ type ParseResult,
42
+ } from "./duration.js";
package/src/scheme.ts ADDED
@@ -0,0 +1,19 @@
1
+ /** A resolved colour scheme: what the page is drawn in right now. */
2
+ export type Scheme = "light" | "dark";
3
+
4
+ /** A person's colour-scheme choice: a fixed scheme, or follow the OS. */
5
+ export type ThemeMode = "system" | Scheme;
6
+
7
+ /** Where the choice is kept by default (`localStorage`), by `useThemeMode` and `schemeBootScript`. */
8
+ export const THEME_STORAGE_KEY = "siiway-theme";
9
+
10
+ /** Whether a value, such as one read back from storage, is a ThemeMode. */
11
+ export function isThemeMode(value: unknown): value is ThemeMode {
12
+ return value === "system" || value === "light" || value === "dark";
13
+ }
14
+
15
+ /** The scheme a mode draws in: `system` follows the OS. */
16
+ export function resolveScheme(mode: ThemeMode, systemDark: boolean): Scheme {
17
+ if (mode !== "system") return mode;
18
+ return systemDark ? "dark" : "light";
19
+ }
@@ -0,0 +1,32 @@
1
+ import { hexToHsl, hslToHex, normalizeAccent } from "./color.js";
2
+
3
+ /** Fluent's default blue, used when no accent is configured. */
4
+ export const DEFAULT_ACCENT = "#0078d4";
5
+
6
+ export const BRAND_SHADES = [
7
+ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160,
8
+ ] as const;
9
+
10
+ export type BrandShade = (typeof BRAND_SHADES)[number];
11
+
12
+ /** Sixteen shades of one hue, darkest (10) to lightest (160). Fluent calls this BrandVariants. */
13
+ export type BrandRamp = Record<BrandShade, string>;
14
+
15
+ /**
16
+ * Builds the brand ramp around an accent colour.
17
+ *
18
+ * Fluent ramps run dark → light: light themes tint surfaces with the high
19
+ * shades and dark themes lift foregrounds with them. The accent is anchored
20
+ * at shade 80, which the light theme uses for primary buttons, links and
21
+ * focus, so those stay exactly the colour that was configured. Each step is
22
+ * 7 points of lightness, clamped to 5–96.
23
+ */
24
+ export function brandRamp(accent?: string | null): BrandRamp {
25
+ const [h, s, l] = hexToHsl(normalizeAccent(accent, DEFAULT_ACCENT));
26
+ const ramp = {} as BrandRamp;
27
+ BRAND_SHADES.forEach((shade, i) => {
28
+ const lightness = Math.max(5, Math.min(96, l + (i - 7) * 7));
29
+ ramp[shade] = hslToHex(h, s, lightness);
30
+ });
31
+ return ramp;
32
+ }
@@ -0,0 +1,147 @@
1
+ // Colour conversions for the brand ramp (HSL) and the colour picker (HSV).
2
+
3
+ const HEX_COLOR = /^#?([0-9a-f]{6})$/i;
4
+ const SHORT_HEX_COLOR = /^#?([0-9a-f]{3})$/i;
5
+ const ACCENT_HEX = /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
6
+
7
+ /**
8
+ * Normalizes a colour a person typed: three or six hex digits, `#` optional.
9
+ * Returns lowercase `#rrggbb`, or null when the input isn't one.
10
+ */
11
+ export function normalizeHexColor(value: string): string | null {
12
+ const input = value.trim();
13
+ const full = HEX_COLOR.exec(input);
14
+ if (full) return `#${full[1]!.toLowerCase()}`;
15
+
16
+ const short = SHORT_HEX_COLOR.exec(input);
17
+ if (!short) return null;
18
+
19
+ return `#${[...short[1]!].map((digit) => digit.repeat(2)).join("")}`.toLowerCase();
20
+ }
21
+
22
+ /**
23
+ * Normalizes a configured accent: `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa`.
24
+ * Alpha has no meaning for a brand ramp, so it is dropped. Anything else, or
25
+ * nothing, gives `fallback`.
26
+ */
27
+ export function normalizeAccent(value: string | null | undefined, fallback: string): string {
28
+ if (!value) return fallback;
29
+ const m = ACCENT_HEX.exec(value.trim());
30
+ if (!m) return fallback;
31
+ const digits = m[1]!.toLowerCase();
32
+ if (digits.length <= 4) {
33
+ return `#${digits
34
+ .slice(0, 3)
35
+ .split("")
36
+ .map((c) => c + c)
37
+ .join("")}`;
38
+ }
39
+ return `#${digits.slice(0, 6)}`;
40
+ }
41
+
42
+ /** `#rrggbb` → [hue 0–360, saturation 0–100, lightness 0–100], unrounded. */
43
+ export function hexToHsl(hex: string): [number, number, number] {
44
+ const r = parseInt(hex.slice(1, 3), 16) / 255;
45
+ const g = parseInt(hex.slice(3, 5), 16) / 255;
46
+ const b = parseInt(hex.slice(5, 7), 16) / 255;
47
+ const max = Math.max(r, g, b);
48
+ const min = Math.min(r, g, b);
49
+ let h = 0;
50
+ let s = 0;
51
+ const l = (max + min) / 2;
52
+ if (max !== min) {
53
+ const d = max - min;
54
+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
55
+ if (max === r) h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
56
+ else if (max === g) h = ((b - r) / d + 2) / 6;
57
+ else h = ((r - g) / d + 4) / 6;
58
+ }
59
+ return [h * 360, s * 100, l * 100];
60
+ }
61
+
62
+ /** Hue 0–360, saturation 0–100, lightness 0–100 → lowercase `#rrggbb`. */
63
+ export function hslToHex(h: number, s: number, l: number): string {
64
+ const hN = h / 360;
65
+ const sN = s / 100;
66
+ const lN = l / 100;
67
+ const hue2rgb = (p: number, q: number, t: number) => {
68
+ if (t < 0) t += 1;
69
+ if (t > 1) t -= 1;
70
+ if (t < 1 / 6) return p + (q - p) * 6 * t;
71
+ if (t < 1 / 2) return q;
72
+ if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
73
+ return p;
74
+ };
75
+ let r: number, g: number, b: number;
76
+ if (sN === 0) {
77
+ r = g = b = lN;
78
+ } else {
79
+ const q = lN < 0.5 ? lN * (1 + sN) : lN + sN - lN * sN;
80
+ const p = 2 * lN - q;
81
+ r = hue2rgb(p, q, hN + 1 / 3);
82
+ g = hue2rgb(p, q, hN);
83
+ b = hue2rgb(p, q, hN - 1 / 3);
84
+ }
85
+ const hex = (v: number) =>
86
+ Math.round(v * 255)
87
+ .toString(16)
88
+ .padStart(2, "0");
89
+ return `#${hex(r)}${hex(g)}${hex(b)}`;
90
+ }
91
+
92
+ /** Hue 0–360, saturation and value 0–1. */
93
+ export interface HsvColor {
94
+ h: number;
95
+ s: number;
96
+ v: number;
97
+ }
98
+
99
+ /** Any colour normalizeHexColor accepts → HSV, or null. */
100
+ export function hexToHsv(value: string): HsvColor | null {
101
+ const normalized = normalizeHexColor(value);
102
+ if (!normalized) return null;
103
+
104
+ const red = Number.parseInt(normalized.slice(1, 3), 16) / 255;
105
+ const green = Number.parseInt(normalized.slice(3, 5), 16) / 255;
106
+ const blue = Number.parseInt(normalized.slice(5, 7), 16) / 255;
107
+ const max = Math.max(red, green, blue);
108
+ const min = Math.min(red, green, blue);
109
+ const delta = max - min;
110
+ let hue = 0;
111
+
112
+ if (delta !== 0) {
113
+ if (max === red) hue = 60 * (((green - blue) / delta) % 6);
114
+ else if (max === green) hue = 60 * ((blue - red) / delta + 2);
115
+ else hue = 60 * ((red - green) / delta + 4);
116
+ }
117
+
118
+ if (hue < 0) hue += 360;
119
+ return { h: hue, s: max === 0 ? 0 : delta / max, v: max };
120
+ }
121
+
122
+ /** HSV → lowercase `#rrggbb`. Out-of-range values are clamped. */
123
+ export function hsvToHex({ h, s, v }: HsvColor): string {
124
+ const hue = ((h % 360) + 360) % 360;
125
+ const saturation = Math.min(1, Math.max(0, s));
126
+ const value = Math.min(1, Math.max(0, v));
127
+ const chroma = value * saturation;
128
+ const section = hue / 60;
129
+ const secondary = chroma * (1 - Math.abs((section % 2) - 1));
130
+ const offset = value - chroma;
131
+ let rgb: [number, number, number];
132
+
133
+ if (section < 1) rgb = [chroma, secondary, 0];
134
+ else if (section < 2) rgb = [secondary, chroma, 0];
135
+ else if (section < 3) rgb = [0, chroma, secondary];
136
+ else if (section < 4) rgb = [0, secondary, chroma];
137
+ else if (section < 5) rgb = [secondary, 0, chroma];
138
+ else rgb = [chroma, 0, secondary];
139
+
140
+ return `#${rgb
141
+ .map((channel) =>
142
+ Math.round((channel + offset) * 255)
143
+ .toString(16)
144
+ .padStart(2, "0"),
145
+ )
146
+ .join("")}`;
147
+ }
@@ -0,0 +1,15 @@
1
+ import type { Scheme } from "../scheme.js";
2
+ import { NEUTRAL, type NeutralTokens } from "../tokens/neutral.js";
3
+ import { RADII } from "../tokens/radii.js";
4
+ import { SHADOWS, type ShadowTokens } from "../tokens/shadows.js";
5
+
6
+ export type ThemeOverrides = typeof RADII & ShadowTokens & NeutralTokens;
7
+
8
+ /**
9
+ * Every token the SiiWay look changes from Fluent's defaults, for one scheme.
10
+ * Spread it over a Fluent theme built from brandRamp():
11
+ * `{ ...createLightTheme(brandRamp(accent)), ...themeOverrides("light") }`.
12
+ */
13
+ export function themeOverrides(scheme: Scheme): ThemeOverrides {
14
+ return { ...RADII, ...SHADOWS[scheme], ...NEUTRAL[scheme] };
15
+ }
@@ -0,0 +1,8 @@
1
+ import type { Scheme } from "../scheme.js";
2
+
3
+ /**
4
+ * The page backdrop that header, sidebar and content share, and that cards
5
+ * sit on with a hairline border. Darker than colorNeutralBackground1 in the
6
+ * dark scheme, so cards still read as surfaces there.
7
+ */
8
+ export const CANVAS: Record<Scheme, string> = { light: "#ffffff", dark: "#1a1a1a" };