@promptctl/rich-js 0.6.0 → 0.7.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.
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/template-bindings/color-funcs.d.ts +78 -0
- package/dist/template-bindings/color-funcs.d.ts.map +1 -0
- package/dist/template-bindings/color-funcs.js +212 -0
- package/dist/template-bindings/color-funcs.js.map +1 -0
- package/dist/template-bindings/index.d.ts +25 -18
- package/dist/template-bindings/index.d.ts.map +1 -1
- package/dist/template-bindings/index.js +27 -19
- package/dist/template-bindings/index.js.map +1 -1
- package/dist/template-bindings/palette-funcs.d.ts +60 -53
- package/dist/template-bindings/palette-funcs.d.ts.map +1 -1
- package/dist/template-bindings/palette-funcs.js +64 -132
- package/dist/template-bindings/palette-funcs.js.map +1 -1
- package/dist/template-bindings/style-funcs.d.ts +11 -12
- package/dist/template-bindings/style-funcs.d.ts.map +1 -1
- package/dist/template-bindings/style-funcs.js +48 -74
- package/dist/template-bindings/style-funcs.js.map +1 -1
- package/dist/themes/buildPalette.d.ts +7 -1
- package/dist/themes/buildPalette.d.ts.map +1 -1
- package/dist/themes/buildPalette.js +15 -1
- package/dist/themes/buildPalette.js.map +1 -1
- package/dist/themes/colorRef.d.ts +52 -0
- package/dist/themes/colorRef.d.ts.map +1 -0
- package/dist/themes/colorRef.js +97 -0
- package/dist/themes/colorRef.js.map +1 -0
- package/dist/themes/palette.d.ts +4 -2
- package/dist/themes/palette.d.ts.map +1 -1
- package/dist/themes/palette.js +4 -2
- package/dist/themes/palette.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"style-funcs.js","sourceRoot":"","sources":["../../src/template-bindings/style-funcs.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"style-funcs.js","sourceRoot":"","sources":["../../src/template-bind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,CAAC,QAAQ,EAAE,UAAU,CAAC;IAChC,UAAU,EAAE,GAAG;CAChB,CAAC;AAEF,oBAAoB;AACpB,EAAE;AACF,oEAAoE;AACpE,wEAAwE;AACxE,yEAAyE;AACzE,sEAAsE;AACtE,qDAAqD;AACrD,EAAE;AACF,uEAAuE;AACvE,oEAAoE;AACpE,0EAA0E;AAC1E,sEAAsE;AACtE,wEAAwE;AACxE,kDAAkD;AAClD,EAAE;AACF,+DAA+D;AAC/D,uEAAuE;AACvE,uEAAuE;AAEvE,MAAM,QAAQ,GAAiB;IAC7B,EAAE,EAAE,CAAC,CAAC,GAAW,EAAE,KAAc,EAAE,EAAE;QACnC,OAAO,oBAAoB,CAAC,KAAK,EAAE,IAAI,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;IAC/D,CAAC,CAAuB;IACxB,QAAQ,EAAE,CAAC,QAAQ,EAAE,UAAU,CAAC;IAChC,UAAU,EAAE,GAAG;CAChB,CAAC;AAEF,0BAA0B;AAE1B;;;;;;;;GAQG;AACH,MAAM,UAAU,kBAAkB;IAChC,OAAO;QACL,EAAE,EAAE,aAAa,CAAC,OAAO,CAAC;QAC1B,EAAE,EAAE,aAAa,CAAC,SAAS,CAAC;QAC5B,GAAG,cAAc,EAAE;QACnB,IAAI,EAAE,QAAQ;QACd,KAAK,EAAE,aAAa;KACrB,CAAC;AACJ,CAAC"}
|
|
@@ -18,7 +18,13 @@ export interface BaseColors {
|
|
|
18
18
|
* Build a full semantic palette from base colors.
|
|
19
19
|
*
|
|
20
20
|
* Derived entries follow Textual's formulas:
|
|
21
|
-
* - `*-muted` = color blended 70% toward
|
|
21
|
+
* - `*-muted` = color blended 70% toward its opposite base role. Defined for
|
|
22
|
+
* every accent AND for the two base roles themselves
|
|
23
|
+
* (`foreground-muted` blends toward `background`,
|
|
24
|
+
* `background-muted` blends toward `foreground`) — a caller
|
|
25
|
+
* de-emphasizing body/structural text reaches for
|
|
26
|
+
* `foreground-muted` the same way it reaches for
|
|
27
|
+
* `primary-muted` to de-emphasize an accent.
|
|
22
28
|
* - `text-*` = contrast text tinted 66% with the accent color (use as
|
|
23
29
|
* foreground in muted/background-tinted contexts)
|
|
24
30
|
* - `on-*` = WCAG-correct contrast colour (black or white) for use as
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildPalette.d.ts","sourceRoot":"","sources":["../../src/themes/buildPalette.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAY,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,SAAS,CAAC;IACnB,SAAS,EAAE,SAAS,CAAC;IACrB,MAAM,EAAE,SAAS,CAAC;IAClB,OAAO,EAAE,SAAS,CAAC;IACnB,OAAO,EAAE,SAAS,CAAC;IACnB,KAAK,EAAE,SAAS,CAAC;IACjB,UAAU,EAAE,SAAS,CAAC;IACtB,UAAU,EAAE,SAAS,CAAC;CACvB;AAUD
|
|
1
|
+
{"version":3,"file":"buildPalette.d.ts","sourceRoot":"","sources":["../../src/themes/buildPalette.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAY,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,SAAS,CAAC;IACnB,SAAS,EAAE,SAAS,CAAC;IACrB,MAAM,EAAE,SAAS,CAAC;IAClB,OAAO,EAAE,SAAS,CAAC;IACnB,OAAO,EAAE,SAAS,CAAC;IACnB,KAAK,EAAE,SAAS,CAAC;IACjB,UAAU,EAAE,SAAS,CAAC;IACtB,UAAU,EAAE,SAAS,CAAC;CACvB;AAUD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAkCnF"}
|
|
@@ -9,7 +9,13 @@ const ACCENT_KEYS = ["primary", "secondary", "accent", "success", "warning", "er
|
|
|
9
9
|
* Build a full semantic palette from base colors.
|
|
10
10
|
*
|
|
11
11
|
* Derived entries follow Textual's formulas:
|
|
12
|
-
* - `*-muted` = color blended 70% toward
|
|
12
|
+
* - `*-muted` = color blended 70% toward its opposite base role. Defined for
|
|
13
|
+
* every accent AND for the two base roles themselves
|
|
14
|
+
* (`foreground-muted` blends toward `background`,
|
|
15
|
+
* `background-muted` blends toward `foreground`) — a caller
|
|
16
|
+
* de-emphasizing body/structural text reaches for
|
|
17
|
+
* `foreground-muted` the same way it reaches for
|
|
18
|
+
* `primary-muted` to de-emphasize an accent.
|
|
13
19
|
* - `text-*` = contrast text tinted 66% with the accent color (use as
|
|
14
20
|
* foreground in muted/background-tinted contexts)
|
|
15
21
|
* - `on-*` = WCAG-correct contrast colour (black or white) for use as
|
|
@@ -26,6 +32,14 @@ export function buildPalette(name, dark, base) {
|
|
|
26
32
|
for (const key of ACCENT_KEYS) {
|
|
27
33
|
vars.set(key, base[key]);
|
|
28
34
|
}
|
|
35
|
+
// Muted variants of the two base roles, same "blend 70% toward
|
|
36
|
+
// background" formula as the accent `*-muted` family below — every
|
|
37
|
+
// base color the palette defines gets a de-emphasized variant, not just
|
|
38
|
+
// the accents. `foreground-muted` is what a caller reaches for to draw
|
|
39
|
+
// structural/contextual text (labels, punctuation, ids) at reduced visual
|
|
40
|
+
// weight without a terminal-support-dependent SGR attribute.
|
|
41
|
+
vars.set("foreground-muted", blendRgb(base.foreground, base.background, MUTED_BLEND));
|
|
42
|
+
vars.set("background-muted", blendRgb(base.background, base.foreground, MUTED_BLEND));
|
|
29
43
|
// Surface — subtle lift from background
|
|
30
44
|
vars.set("surface", blendRgb(base.background, base.foreground, SURFACE_LIFT));
|
|
31
45
|
// Derived: muted, text-, and on- for each accent.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildPalette.js","sourceRoot":"","sources":["../../src/themes/buildPalette.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAiBvC,MAAM,WAAW,GAAG,GAAG,CAAC;AACxB,MAAM,UAAU,GAAG,IAAI,CAAC;AACxB,MAAM,YAAY,GAAG,IAAI,CAAC;AAI1B,MAAM,WAAW,GAAgB,CAAC,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAEnG
|
|
1
|
+
{"version":3,"file":"buildPalette.js","sourceRoot":"","sources":["../../src/themes/buildPalette.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAiBvC,MAAM,WAAW,GAAG,GAAG,CAAC;AACxB,MAAM,UAAU,GAAG,IAAI,CAAC;AACxB,MAAM,YAAY,GAAG,IAAI,CAAC;AAI1B,MAAM,WAAW,GAAgB,CAAC,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAEnG;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,YAAY,CAAC,IAAY,EAAE,IAAa,EAAE,IAAgB;IACxE,MAAM,IAAI,GAAG,IAAI,GAAG,EAAqB,CAAC;IAE1C,eAAe;IACf,IAAI,CAAC,GAAG,CAAC,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IACxC,IAAI,CAAC,GAAG,CAAC,YAAY,EAAE,IAA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IAAI,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ColorRgba } from "../core/color.js";
|
|
2
|
+
import type { Palette } from "./palette.js";
|
|
3
|
+
/**
|
|
4
|
+
* A **color reference** is the one string form an author uses to name a
|
|
5
|
+
* concrete color: a palette variable name (`"primary"`, `"surface-active"`,
|
|
6
|
+
* `"text-primary"`) or a literal `#RRGGBB` / `#RRGGBBAA`.
|
|
7
|
+
*
|
|
8
|
+
* [LAW:parse-dont-validate] `resolveColorRef` is the single checkpoint that
|
|
9
|
+
* turns an untrusted reference string into a `ColorRgba`. Everything
|
|
10
|
+
* downstream — color math, `Style` construction, serialization — takes
|
|
11
|
+
* `ColorRgba` and therefore cannot ask again whether the name existed. There
|
|
12
|
+
* is no `isValidColorRef` companion, deliberately: a validator would hand back
|
|
13
|
+
* the same `string` it was given and every consumer would re-check.
|
|
14
|
+
*
|
|
15
|
+
* [LAW:no-silent-failure] A miss throws. An unknown palette name is a broken
|
|
16
|
+
* config, and the author needs to see which name and what was available — not
|
|
17
|
+
* a silently substituted default that makes the bar merely look wrong.
|
|
18
|
+
*/
|
|
19
|
+
export declare class ColorRefError extends Error {
|
|
20
|
+
readonly ref: string;
|
|
21
|
+
constructor(ref: string, detail: string);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The literal-color shape, exported so the template bindings gate on the same
|
|
25
|
+
* pattern this module parses. [LAW:one-source-of-truth] — one regex, one
|
|
26
|
+
* parser body; the bindings add only their own error wording.
|
|
27
|
+
*/
|
|
28
|
+
export declare const HEX_COLOR_RE: RegExp;
|
|
29
|
+
/**
|
|
30
|
+
* Parse a `#RRGGBB` / `#RRGGBBAA` literal. The hex arm of
|
|
31
|
+
* {@link resolveColorRef}, separated so callers that accept *only* literal
|
|
32
|
+
* colors (color math, which cannot meaningfully operate on a palette name)
|
|
33
|
+
* share the one implementation instead of re-deriving it.
|
|
34
|
+
*
|
|
35
|
+
* @throws {ColorRefError} when `hex` is not a well-formed literal.
|
|
36
|
+
*/
|
|
37
|
+
export declare function parseHexColor(hex: string): ColorRgba;
|
|
38
|
+
/**
|
|
39
|
+
* Resolve a color reference against `palette`.
|
|
40
|
+
*
|
|
41
|
+
* Accepting hex alongside names is what makes this **idempotent**:
|
|
42
|
+
* `resolveColorRef(p, resolveColorRef(p, r).hex)` equals
|
|
43
|
+
* `resolveColorRef(p, r)` for every `r` this function accepts. Callers
|
|
44
|
+
* therefore never branch on "is this a palette name or already a color" —
|
|
45
|
+
* they call it unconditionally on whatever the author wrote, whether that is
|
|
46
|
+
* `"surface-active"` or the output of a chain of color math.
|
|
47
|
+
* [LAW:dataflow-not-control-flow]
|
|
48
|
+
*
|
|
49
|
+
* @throws {ColorRefError} on malformed hex or an unknown palette name.
|
|
50
|
+
*/
|
|
51
|
+
export declare function resolveColorRef(palette: Palette, ref: string): ColorRgba;
|
|
52
|
+
//# sourceMappingURL=colorRef.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"colorRef.d.ts","sourceRoot":"","sources":["../../src/themes/colorRef.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAA6B,MAAM,kBAAkB,CAAC;AACxE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5C;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,aAAc,SAAQ,KAAK;IAEpC,QAAQ,CAAC,GAAG,EAAE,MAAM;gBAAX,GAAG,EAAE,MAAM,EACpB,MAAM,EAAE,MAAM;CAKjB;AAUD;;;;GAIG;AACH,eAAO,MAAM,YAAY,QAAuC,CAAC;AAIjE;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAUpD;AAMD;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,CAcxE"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { parseRgbHex, parseRgbaHex } from "../core/color.js";
|
|
2
|
+
/**
|
|
3
|
+
* A **color reference** is the one string form an author uses to name a
|
|
4
|
+
* concrete color: a palette variable name (`"primary"`, `"surface-active"`,
|
|
5
|
+
* `"text-primary"`) or a literal `#RRGGBB` / `#RRGGBBAA`.
|
|
6
|
+
*
|
|
7
|
+
* [LAW:parse-dont-validate] `resolveColorRef` is the single checkpoint that
|
|
8
|
+
* turns an untrusted reference string into a `ColorRgba`. Everything
|
|
9
|
+
* downstream — color math, `Style` construction, serialization — takes
|
|
10
|
+
* `ColorRgba` and therefore cannot ask again whether the name existed. There
|
|
11
|
+
* is no `isValidColorRef` companion, deliberately: a validator would hand back
|
|
12
|
+
* the same `string` it was given and every consumer would re-check.
|
|
13
|
+
*
|
|
14
|
+
* [LAW:no-silent-failure] A miss throws. An unknown palette name is a broken
|
|
15
|
+
* config, and the author needs to see which name and what was available — not
|
|
16
|
+
* a silently substituted default that makes the bar merely look wrong.
|
|
17
|
+
*/
|
|
18
|
+
export class ColorRefError extends Error {
|
|
19
|
+
ref;
|
|
20
|
+
constructor(ref, detail) {
|
|
21
|
+
super(`color reference ${JSON.stringify(ref)} did not resolve — ${detail}`);
|
|
22
|
+
this.ref = ref;
|
|
23
|
+
this.name = "ColorRefError";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
// A reference *shaped* like hex is resolved as hex; anything else is a palette
|
|
27
|
+
// name. This is a lexical distinction with no overlap — palette variable names
|
|
28
|
+
// never begin with `#` — so it is a parse, not a mode. A `#`-leading string
|
|
29
|
+
// that is malformed hex fails loudly here rather than falling through to a
|
|
30
|
+
// palette lookup that would report a confusing "unknown name" instead of
|
|
31
|
+
// "malformed hex".
|
|
32
|
+
const HEX_LEAD = "#";
|
|
33
|
+
/**
|
|
34
|
+
* The literal-color shape, exported so the template bindings gate on the same
|
|
35
|
+
* pattern this module parses. [LAW:one-source-of-truth] — one regex, one
|
|
36
|
+
* parser body; the bindings add only their own error wording.
|
|
37
|
+
*/
|
|
38
|
+
export const HEX_COLOR_RE = /^#[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$/;
|
|
39
|
+
const OPAQUE_HEX_LENGTH = 7; // "#RRGGBB"
|
|
40
|
+
/**
|
|
41
|
+
* Parse a `#RRGGBB` / `#RRGGBBAA` literal. The hex arm of
|
|
42
|
+
* {@link resolveColorRef}, separated so callers that accept *only* literal
|
|
43
|
+
* colors (color math, which cannot meaningfully operate on a palette name)
|
|
44
|
+
* share the one implementation instead of re-deriving it.
|
|
45
|
+
*
|
|
46
|
+
* @throws {ColorRefError} when `hex` is not a well-formed literal.
|
|
47
|
+
*/
|
|
48
|
+
export function parseHexColor(hex) {
|
|
49
|
+
const trimmed = hex.trim();
|
|
50
|
+
if (!HEX_COLOR_RE.test(trimmed)) {
|
|
51
|
+
throw new ColorRefError(hex, "expected #RRGGBB or #RRGGBBAA");
|
|
52
|
+
}
|
|
53
|
+
// Both parsers take the digits without the leading `#`.
|
|
54
|
+
const digits = trimmed.slice(1);
|
|
55
|
+
return trimmed.length > OPAQUE_HEX_LENGTH
|
|
56
|
+
? parseRgbaHex(digits)
|
|
57
|
+
: parseRgbHex(digits);
|
|
58
|
+
}
|
|
59
|
+
// Cap the "did you mean" list. Palettes carry ~150 variables; dumping all of
|
|
60
|
+
// them buries the message. Nearby names are what a mistyped reference needs.
|
|
61
|
+
const SUGGESTION_LIMIT = 8;
|
|
62
|
+
/**
|
|
63
|
+
* Resolve a color reference against `palette`.
|
|
64
|
+
*
|
|
65
|
+
* Accepting hex alongside names is what makes this **idempotent**:
|
|
66
|
+
* `resolveColorRef(p, resolveColorRef(p, r).hex)` equals
|
|
67
|
+
* `resolveColorRef(p, r)` for every `r` this function accepts. Callers
|
|
68
|
+
* therefore never branch on "is this a palette name or already a color" —
|
|
69
|
+
* they call it unconditionally on whatever the author wrote, whether that is
|
|
70
|
+
* `"surface-active"` or the output of a chain of color math.
|
|
71
|
+
* [LAW:dataflow-not-control-flow]
|
|
72
|
+
*
|
|
73
|
+
* @throws {ColorRefError} on malformed hex or an unknown palette name.
|
|
74
|
+
*/
|
|
75
|
+
export function resolveColorRef(palette, ref) {
|
|
76
|
+
const trimmed = ref.trim();
|
|
77
|
+
if (trimmed.startsWith(HEX_LEAD))
|
|
78
|
+
return parseHexColor(trimmed);
|
|
79
|
+
const hit = palette.get(trimmed);
|
|
80
|
+
if (hit === undefined) {
|
|
81
|
+
throw new ColorRefError(ref, `no such variable in palette ${JSON.stringify(palette.name)}` +
|
|
82
|
+
suggest(palette, trimmed));
|
|
83
|
+
}
|
|
84
|
+
return hit;
|
|
85
|
+
}
|
|
86
|
+
// Names sharing a prefix or suffix with the miss — enough to catch the two
|
|
87
|
+
// mistakes that actually happen (a wrong qualifier, `surface-active` vs
|
|
88
|
+
// `surface-lighten`; a wrong root, `text-primary` vs `primary-text`) without
|
|
89
|
+
// printing the whole palette.
|
|
90
|
+
function suggest(palette, miss) {
|
|
91
|
+
const parts = miss.split("-");
|
|
92
|
+
const near = [...palette.vars.keys()]
|
|
93
|
+
.filter((name) => parts.some((part) => part !== "" && name.includes(part)))
|
|
94
|
+
.slice(0, SUGGESTION_LIMIT);
|
|
95
|
+
return near.length === 0 ? "" : `; did you mean: ${near.join(", ")}`;
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=colorRef.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"colorRef.js","sourceRoot":"","sources":["../../src/themes/colorRef.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,WAAW,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAGxE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,aAAc,SAAQ,KAAK;IAE3B;IADX,YACW,GAAW,EACpB,MAAc;QAEd,KAAK,CAAC,mBAAmB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,sBAAsB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AAE,CAAC;QACtB,MAAM,IAAI,aAAa,CACrB,GAAG,EACH,+BAA+B,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;YAC3D,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAC5B,CAAC;IACJ,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED,2EAA2E;AAC3E,wEAAwE;AACxE,6EAA6E;AAC7E,8BAA8B;AAC9B,SAAS,OAAO,CAAC,OAAgB,EAAE,IAAY;IAC7C,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC9B,MAAM,IAAI,GAAG,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;SAClC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,EAAE,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;SAC1E,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC;IAC9B,OAAO,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,mBAAmB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;AACvE,CAAC"}
|
package/dist/themes/palette.d.ts
CHANGED
|
@@ -7,8 +7,10 @@ import type { ColorRgba } from "../core/color.js";
|
|
|
7
7
|
* `accent`, `error`, etc. — and are the foundation of the theming system.
|
|
8
8
|
*
|
|
9
9
|
* Storage is uniformly ColorRgba; consumers that load from hex JSON
|
|
10
|
-
* parse to ColorRgba at load time.
|
|
11
|
-
*
|
|
10
|
+
* parse to ColorRgba at load time. `get` is a bare lookup — turning an
|
|
11
|
+
* author-written *reference* (a name, or a `#RRGGBB` literal) into a colour
|
|
12
|
+
* is `resolveColorRef`'s job in `colorRef.ts`, so a Palette never has to
|
|
13
|
+
* know about the syntax callers write.
|
|
12
14
|
*/
|
|
13
15
|
export declare class Palette {
|
|
14
16
|
readonly name: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"palette.d.ts","sourceRoot":"","sources":["../../src/themes/palette.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD
|
|
1
|
+
{"version":3,"file":"palette.d.ts","sourceRoot":"","sources":["../../src/themes/palette.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD;;;;;;;;;;;;GAYG;AACH,qBAAa,OAAO;IAClB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;gBAG5C,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,OAAO,EACb,IAAI,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,CAAC;IAStC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS;CAGxC"}
|
package/dist/themes/palette.js
CHANGED
|
@@ -6,8 +6,10 @@
|
|
|
6
6
|
* `accent`, `error`, etc. — and are the foundation of the theming system.
|
|
7
7
|
*
|
|
8
8
|
* Storage is uniformly ColorRgba; consumers that load from hex JSON
|
|
9
|
-
* parse to ColorRgba at load time.
|
|
10
|
-
*
|
|
9
|
+
* parse to ColorRgba at load time. `get` is a bare lookup — turning an
|
|
10
|
+
* author-written *reference* (a name, or a `#RRGGBB` literal) into a colour
|
|
11
|
+
* is `resolveColorRef`'s job in `colorRef.ts`, so a Palette never has to
|
|
12
|
+
* know about the syntax callers write.
|
|
11
13
|
*/
|
|
12
14
|
export class Palette {
|
|
13
15
|
name;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"palette.js","sourceRoot":"","sources":["../../src/themes/palette.ts"],"names":[],"mappings":"AAEA
|
|
1
|
+
{"version":3,"file":"palette.js","sourceRoot":"","sources":["../../src/themes/palette.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,OAAO;IACT,IAAI,CAAS;IACb,IAAI,CAAU;IACd,IAAI,CAAiC;IAE9C,YACE,IAAY,EACZ,IAAa,EACb,IAAoC;QAEpC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,2EAA2E;QAC3E,sEAAsE;QACtE,IAAI,CAAC,IAAI,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC;IAED,GAAG,CAAC,GAAW;QACb,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC5B,CAAC;CACF"}
|