@mark1russell7/vex-domains 0.3.0 → 0.4.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/README.md CHANGED
@@ -18,7 +18,8 @@ npm install @mark1russell7/vex @mark1russell7/vex-domains
18
18
  | `NDVectorDomain` | `NDVector`: a vector with named components | `add`, `subtract`, `scale`, `dot` |
19
19
  | `NumDomain` | numbers | `add`, `multiply`, `min`, `max`, `sqrt`, `floor`, `gt` |
20
20
  | `BoolDomain` | booleans | `and`, `or`, `xor`, `not` |
21
- | `ColorDomain` | `Color`: an RGB color | `mix`, `add`, `multiply`, `luminance` |
21
+ | `TextDomain` | texts | `concat`, `length`, `upper`, `lower`, `trim`, `contains` |
22
+ | `ColorDomain` | `Color`: an RGB color | `mix`, `add`, `multiply`, `luminance`, `relativeLuminance`, `contrast` |
22
23
  | `AngleDomain` | `Angle`: an angle | `add`, `normalize`, `sin`, `cos`, `degrees` |
23
24
  | `FnDomain`, `MaybeDomain` | Two example domains: function wrappers and `Maybe` | `invoke`, `map`, `chain`, `valueOr` |
24
25
 
package/build/index.d.ts CHANGED
@@ -189,6 +189,21 @@ export type BoolOps = {
189
189
  /** The domain of booleans. For lazy logic, use the special forms `and`, `or` and `if` of the IR. */
190
190
  export declare const BoolDomain: Domain<"Bool", boolean, BoolOps>;
191
191
  //#endregion
192
+ //#region src/text.d.ts
193
+ type TextFn1 = (a: string) => string;
194
+ /** The ops of `TextDomain`. */
195
+ export type TextOps = {
196
+ readonly concat: FnOp<string, (a: string, b: unknown) => string>;
197
+ readonly length: FnOp<string, (a: string) => number>;
198
+ readonly upper: FnOp<string, TextFn1>;
199
+ readonly lower: FnOp<string, TextFn1>;
200
+ readonly trim: FnOp<string, TextFn1>;
201
+ readonly contains: FnOp<string, (a: string, b: string) => boolean>;
202
+ readonly eq: FnOp<string, (a: string, b: string) => boolean>;
203
+ };
204
+ /** The domain of texts. */
205
+ export declare const TextDomain: Domain<"Text", string, TextOps>;
206
+ //#endregion
192
207
  //#region src/color.d.ts
193
208
  /**
194
209
  * An immutable RGBA color. The channels have no fixed range: use `clamp` to limit them. `add` adds the color
@@ -214,12 +229,19 @@ export declare class Color {
214
229
  mix(o: Color, t: number): Color;
215
230
  /** The relative luminance (Rec. 709 weights) of the color channels. */
216
231
  luminance(): number;
232
+ /**
233
+ * The relative luminance of WCAG 2.1, from 0 (black) to 1 (white). Each channel from 0 to 255 becomes a linear
234
+ * value with the sRGB transfer function. A channel outside the range counts as the nearest end of the range.
235
+ */
236
+ relativeLuminance(): number;
237
+ /** The contrast ratio of WCAG 2.1 between this color and `o`: 1 for equal luminance, 21 for black and white. */
238
+ contrast(o: Color): number;
217
239
  equals(o: Color): boolean;
218
240
  toArray(): readonly [number, number, number, number];
219
241
  toString(): string;
220
242
  }
221
243
  /** The ops of `ColorDomain`. */
222
- export type ColorOps = OpTable<Color, "add" | "multiply" | "clamp" | "mix" | "luminance" | "equals">;
244
+ export type ColorOps = OpTable<Color, "add" | "multiply" | "clamp" | "mix" | "luminance" | "relativeLuminance" | "contrast" | "equals">;
223
245
  /** The domain of colors. */
224
246
  export declare const ColorDomain: Domain<"Color", Color, ColorOps>;
225
247
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/vec2.ts","../src/ndvector.ts","../src/scalars.ts","../src/color.ts","../src/angle.ts","../src/wrappers.ts"],"mappings":";;;;;;qBAMa;WACF;WACA;EAET,YAAY,WAAW;;SAMhB,GAAG,WAAW,YAAY;;SAI1B,OAAO,YAAY;;kBAIV,MAAM;;kBAEN,KAAK;EAErB,IAAI,GAAG,OAAO;EAGd,SAAS,GAAG,OAAO;;EAInB,SAAS,GAAG,OAAO;;EAInB,OAAO,GAAG,OAAO;;EAIjB,IAAI,GAAG,OAAO;;EAId,IAAI,GAAG,OAAO;EAGd,MAAM,YAAY;EAGlB,UAAU;EAGV,SAAS;EAGT,OAAO;EAGP,SAAS;EAGT,SAAS;EAG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B,aAAa;;EAMb,OAAO,kBAAa;EAGpB;EAGA;EAGA;EAGA;;;YAMU,WAAW,QAAQ;;qBAGlB,aAAa,gBAAgB,OAAO;;;;qBCpDpC;WACF,QAAQ;EAEjB,YAAY,QAAQ;EAGpB,UAAU;;;YAMA,QAAQ,QAAQ;;qBAGf,UAAU,aAAa,IAAI;;;;;qBAU3B,MAAM;;UAIT;SAID,IAAI,GAAG,OAAO,IAAI,MAAM;SAGxB,OAAO,cAAc,MAAM;EAGlC;EAGA;EAGA,IAAI,GAAG,IAAI,OAAO,MAAM,IAAI,MAAM;EAGlC,MAAM,GAAG,IAAI,OAAO,MAAM,MAAM,KAAK,MAAM;EAG3C,QAAQ,GAAG,UAAU,IAAI,IAAI;;;YAMnB,WAAW,QAAQ;;qBAGlB,aAAa,gBAAgB,gBAAgB"}
package/build/index.js CHANGED
@@ -449,7 +449,7 @@ const LiftedNDVectorDomain = defineDomain({
449
449
  const isNumRecord = (u) => {
450
450
  if (typeof u !== "object" || u === null || Array.isArray(u)) return false;
451
451
  const proto = Object.getPrototypeOf(u);
452
- return (proto === Object.prototype || proto === null) && Object.values(u).every((v) => typeof v === "number" && Number.isFinite(v));
452
+ return (proto === Object.prototype || proto === null) && Object.values(u).every((v) => Number.isFinite(v));
453
453
  };
454
454
  /** The domain of plain number records. Its op `toNDVector` builds a vector from fields, for example from `rec()`. */
455
455
  const NumRecordDomain = defineDomain({
@@ -586,7 +586,48 @@ const BoolDomain = defineDomain({
586
586
  }
587
587
  });
588
588
  //#endregion
589
+ //#region src/text.ts
590
+ /**
591
+ * The domain of texts. The op `concat` joins a text and a value of any kind. It is the operator `&` of a
592
+ * spreadsheet, and of the formula language of the sheet app.
593
+ */
594
+ /**
595
+ * This function gives the text of a value for `concat`. A text stays. A plain record gives its JSON text. Another
596
+ * value, for example a number or a `Vec2`, gives its own text.
597
+ */
598
+ const textOf = (u) => typeof u === "object" && u !== null && Object.getPrototypeOf(u) === Object.prototype ? JSON.stringify(u) : String(u);
599
+ /** The domain of texts. */
600
+ const TextDomain = defineDomain({
601
+ name: "Text",
602
+ is: (u) => typeof u === "string",
603
+ show: (s) => JSON.stringify(s),
604
+ ops: {
605
+ concat: {
606
+ fn: (a, b) => a + textOf(b),
607
+ params: ["any"]
608
+ },
609
+ length: { fn: (a) => a.length },
610
+ upper: { fn: (a) => a.toUpperCase() },
611
+ lower: { fn: (a) => a.toLowerCase() },
612
+ trim: { fn: (a) => a.trim() },
613
+ contains: {
614
+ fn: (a, b) => a.includes(b),
615
+ params: ["string"]
616
+ },
617
+ eq: {
618
+ fn: (a, b) => a === b,
619
+ laws: ["commutative"],
620
+ params: ["string"]
621
+ }
622
+ }
623
+ });
624
+ //#endregion
589
625
  //#region src/color.ts
626
+ /** The linear value of an sRGB channel from 0 to 255, with the sRGB transfer function of WCAG 2.1. */
627
+ function linear(channel) {
628
+ const s = Math.min(1, Math.max(0, channel / 255));
629
+ return s <= .04045 ? s / 12.92 : ((s + .055) / 1.055) ** 2.4;
630
+ }
590
631
  /**
591
632
  * An immutable RGBA color. The channels have no fixed range: use `clamp` to limit them. `add` adds the color
592
633
  * channels and keeps the larger alpha, so `add` is commutative and associative.
@@ -630,6 +671,19 @@ var Color = class Color {
630
671
  luminance() {
631
672
  return .2126 * this.r + .7152 * this.g + .0722 * this.b;
632
673
  }
674
+ /**
675
+ * The relative luminance of WCAG 2.1, from 0 (black) to 1 (white). Each channel from 0 to 255 becomes a linear
676
+ * value with the sRGB transfer function. A channel outside the range counts as the nearest end of the range.
677
+ */
678
+ relativeLuminance() {
679
+ return .2126 * linear(this.r) + .7152 * linear(this.g) + .0722 * linear(this.b);
680
+ }
681
+ /** The contrast ratio of WCAG 2.1 between this color and `o`: 1 for equal luminance, 21 for black and white. */
682
+ contrast(o) {
683
+ const a = this.relativeLuminance();
684
+ const b = o.relativeLuminance();
685
+ return (Math.max(a, b) + .05) / (Math.min(a, b) + .05);
686
+ }
633
687
  equals(o) {
634
688
  return this.r === o.r && this.g === o.g && this.b === o.b && this.a === o.a;
635
689
  }
@@ -667,6 +721,11 @@ const ColorDomain = defineDomain({
667
721
  clamp: { params: ["number", "number"] },
668
722
  mix: { params: ["domain", "number"] },
669
723
  luminance: {},
724
+ relativeLuminance: {},
725
+ contrast: {
726
+ laws: ["commutative"],
727
+ params: ["domain"]
728
+ },
670
729
  equals: {
671
730
  laws: ["commutative"],
672
731
  params: ["domain"]
@@ -810,6 +869,6 @@ const MaybeDomain = defineDomain({
810
869
  }
811
870
  });
812
871
  //#endregion
813
- export { Angle, AngleDomain, BoolDomain, Color, ColorDomain, Fn, FnDomain, LiftedNDVectorDomain, Maybe, MaybeDomain, NDVector, NDVectorDomain, NumDomain, NumRecordDomain, Vec2, Vec2Domain };
872
+ export { Angle, AngleDomain, BoolDomain, Color, ColorDomain, Fn, FnDomain, LiftedNDVectorDomain, Maybe, MaybeDomain, NDVector, NDVectorDomain, NumDomain, NumRecordDomain, TextDomain, Vec2, Vec2Domain };
814
873
 
815
874
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/vec2.ts","../src/ndvector.ts","../src/scalars.ts","../src/color.ts","../src/angle.ts","../src/wrappers.ts"],"sourcesContent":["import { defineDomain, type Domain, type OpTable } from \"@mark1russell7/vex\";\n\n/**\n * An immutable 2D vector. The methods live on the prototype, so a vector costs two numbers. Division is real\n * division: a division by zero gives an infinite component, and the domain check turns it into `#NUM!`.\n */\nexport class Vec2 {\n readonly x: number;\n readonly y: number;\n\n constructor(x: number, y: number) {\n this.x = x;\n this.y = y;\n }\n\n /** This function makes a vector. */\n static of(x: number, y: number): Vec2 {\n return new Vec2(x, y);\n }\n /** This function makes a vector with the same value in both components. */\n static scalar(n: number): Vec2 {\n return new Vec2(n, n);\n }\n /** The zero vector. */\n static readonly zero: Vec2 = new Vec2(0, 0);\n /** The vector of ones. */\n static readonly one: Vec2 = new Vec2(1, 1);\n\n add(o: Vec2): Vec2 {\n return new Vec2(this.x + o.x, this.y + o.y);\n }\n subtract(o: Vec2): Vec2 {\n return new Vec2(this.x - o.x, this.y - o.y);\n }\n /** The component-wise product. */\n multiply(o: Vec2): Vec2 {\n return new Vec2(this.x * o.x, this.y * o.y);\n }\n /** The component-wise quotient. A zero component of `o` gives an infinite component. */\n divide(o: Vec2): Vec2 {\n return new Vec2(this.x / o.x, this.y / o.y);\n }\n /** The component-wise minimum. */\n min(o: Vec2): Vec2 {\n return new Vec2(Math.min(this.x, o.x), Math.min(this.y, o.y));\n }\n /** The component-wise maximum. */\n max(o: Vec2): Vec2 {\n return new Vec2(Math.max(this.x, o.x), Math.max(this.y, o.y));\n }\n scale(k: number): Vec2 {\n return new Vec2(this.x * k, this.y * k);\n }\n negate(): Vec2 {\n return new Vec2(-this.x, -this.y);\n }\n halve(): Vec2 {\n return this.scale(0.5);\n }\n abs(): Vec2 {\n return new Vec2(Math.abs(this.x), Math.abs(this.y));\n }\n round(): Vec2 {\n return new Vec2(Math.round(this.x), Math.round(this.y));\n }\n floor(): Vec2 {\n return new Vec2(Math.floor(this.x), Math.floor(this.y));\n }\n ceil(): Vec2 {\n return new Vec2(Math.ceil(this.x), Math.ceil(this.y));\n }\n clamp(lo: number, hi: number): Vec2 {\n return new Vec2(Math.min(hi, Math.max(lo, this.x)), Math.min(hi, Math.max(lo, this.y)));\n }\n swap(): Vec2 {\n return new Vec2(this.y, this.x);\n }\n /** The vector turned by `radians`, counterclockwise. */\n rotate(radians: number): Vec2 {\n const c = Math.cos(radians);\n const s = Math.sin(radians);\n return new Vec2(c * this.x - s * this.y, s * this.x + c * this.y);\n }\n /** The vector with length 1. The zero vector stays zero. */\n normalize(): Vec2 {\n const len = this.length();\n return len === 0 ? this : this.scale(1 / len);\n }\n /** The linear interpolation from this vector to `o`. */\n lerp(o: Vec2, t: number): Vec2 {\n return new Vec2(this.x + (o.x - this.x) * t, this.y + (o.y - this.y) * t);\n }\n length(): number {\n return Math.hypot(this.x, this.y);\n }\n lengthSq(): number {\n return this.x * this.x + this.y * this.y;\n }\n distance(o: Vec2): number {\n return Math.hypot(this.x - o.x, this.y - o.y);\n }\n dot(o: Vec2): number {\n return this.x * o.x + this.y * o.y;\n }\n /** The z component of the 3D cross product. */\n cross(o: Vec2): number {\n return this.x * o.y - this.y * o.x;\n }\n /** The angle of the vector, in radians, from the x axis. */\n angle(): number {\n return Math.atan2(this.y, this.x);\n }\n /** The product of the components: the area of a size vector. */\n area(): number {\n return this.x * this.y;\n }\n /** The sum of the components. */\n sum(): number {\n return this.x + this.y;\n }\n minComponent(): number {\n return Math.min(this.x, this.y);\n }\n maxComponent(): number {\n return Math.max(this.x, this.y);\n }\n equals(o: Vec2): boolean {\n return this.x === o.x && this.y === o.y;\n }\n anyNonPositive(): boolean {\n return this.x <= 0 || this.y <= 0;\n }\n allPositive(): boolean {\n return this.x > 0 && this.y > 0;\n }\n anyNegative(): boolean {\n return this.x < 0 || this.y < 0;\n }\n allNonNegative(): boolean {\n return this.x >= 0 && this.y >= 0;\n }\n anyZero(): boolean {\n return this.x === 0 || this.y === 0;\n }\n allZero(): boolean {\n return this.x === 0 && this.y === 0;\n }\n anyLessThan(n: number): boolean {\n return this.x < n || this.y < n;\n }\n allLessThan(n: number): boolean {\n return this.x < n && this.y < n;\n }\n anyGreaterThan(n: number): boolean {\n return this.x > n || this.y > n;\n }\n allGreaterThan(n: number): boolean {\n return this.x > n && this.y > n;\n }\n toJSON(): { readonly x: number; readonly y: number } {\n return { x: this.x, y: this.y };\n }\n toString(): string {\n return `(${this.x}, ${this.y})`;\n }\n}\n\ntype Vec2OpNames =\n | \"add\" | \"subtract\" | \"multiply\" | \"divide\" | \"min\" | \"max\" | \"scale\" | \"negate\" | \"halve\" | \"abs\" | \"round\" | \"floor\"\n | \"ceil\" | \"clamp\" | \"swap\" | \"rotate\" | \"normalize\" | \"lerp\" | \"length\" | \"lengthSq\" | \"distance\" | \"dot\" | \"cross\"\n | \"angle\" | \"area\" | \"sum\" | \"minComponent\" | \"maxComponent\" | \"equals\" | \"anyNonPositive\" | \"allPositive\"\n | \"anyNegative\" | \"allNonNegative\" | \"anyZero\" | \"allZero\" | \"anyLessThan\" | \"allLessThan\" | \"anyGreaterThan\"\n | \"allGreaterThan\";\n\n/** The ops of `Vec2Domain`. */\nexport type Vec2Ops = OpTable<Vec2, Vec2OpNames, \"add\" | \"subtract\" | \"multiply\" | \"divide\" | \"min\" | \"max\">;\n\n/** The domain of 2D vectors. A number argument of a component-wise op lifts to a vector with that value twice. */\nexport const Vec2Domain: Domain<\"Vec2\", Vec2, Vec2Ops> = defineDomain<\"Vec2\", Vec2, Vec2Ops>({\n name: \"Vec2\",\n is: (u: unknown): u is Vec2 => u instanceof Vec2,\n fromScalar: (n: number): Vec2 => Vec2.scalar(n),\n valid: (v: Vec2): boolean => Number.isFinite(v.x) && Number.isFinite(v.y),\n show: (v: Vec2): string => v.toString(),\n encode: (v: Vec2): readonly [number, number] => [v.x, v.y],\n decode: (u: unknown): Vec2 => {\n const [x, y] = Array.isArray(u) ? (u as readonly unknown[]) : [];\n return new Vec2(typeof x === \"number\" ? x : Number.NaN, typeof y === \"number\" ? y : Number.NaN);\n },\n ops: {\n add: { laws: [\"commutative\", \"associative\"], identity: () => Vec2.zero, liftScalar: true, params: [\"domain\"] },\n subtract: { liftScalar: true, params: [\"domain\"] },\n multiply: { laws: [\"commutative\", \"associative\"], identity: () => Vec2.one, liftScalar: true, params: [\"domain\"] },\n divide: { liftScalar: true, params: [\"domain\"] },\n min: { laws: [\"commutative\", \"associative\", \"idempotent\"], liftScalar: true, params: [\"domain\"] },\n max: { laws: [\"commutative\", \"associative\", \"idempotent\"], liftScalar: true, params: [\"domain\"] },\n scale: { params: [\"number\"] },\n negate: {},\n halve: {},\n abs: {},\n round: {},\n floor: {},\n ceil: {},\n clamp: { params: [\"number\", \"number\"] },\n swap: {},\n rotate: { params: [\"number\"] },\n normalize: {},\n lerp: { params: [\"domain\", \"number\"] },\n length: {},\n lengthSq: {},\n distance: { params: [\"domain\"] },\n dot: { laws: [\"commutative\"], params: [\"domain\"] },\n cross: { params: [\"domain\"] },\n angle: {},\n area: {},\n sum: {},\n minComponent: {},\n maxComponent: {},\n equals: { laws: [\"commutative\"], params: [\"domain\"] },\n anyNonPositive: {},\n allPositive: {},\n anyNegative: {},\n allNonNegative: {},\n anyZero: {},\n allZero: {},\n anyLessThan: { params: [\"number\"] },\n allLessThan: { params: [\"number\"] },\n anyGreaterThan: { params: [\"number\"] },\n allGreaterThan: { params: [\"number\"] },\n },\n});\n","import { defineDomain, type Domain, type FnOp, type OpTable } from \"@mark1russell7/vex\";\n\n/**\n * An immutable vector with named components. The type parameter `K` is the set of component names. Ops that\n * combine two vectors use the union of their names, and a missing component counts as 0. The constructor\n * keeps each number, also a number that is not finite: the domain check reports such a value as `#NUM!`.\n */\nexport class NDVector<K extends string = string> {\n /** The components: name to number. */\n readonly c: Readonly<Record<K, number>>;\n\n constructor(c: Readonly<Record<K, number>>) {\n this.c = Object.freeze({ ...c });\n }\n\n /** This function makes a vector from name and value pairs. */\n static of<K extends string>(entries: readonly (readonly [K, number])[]): NDVector<K> {\n const c = {} as Record<K, number>;\n for (const [k, v] of entries) c[k] = v;\n return new NDVector(c);\n }\n /** This function makes the zero vector over the names `keys`. */\n static zero<K extends string>(keys: readonly K[] = []): NDVector<K> {\n return NDVector.of(keys.map((k) => [k, 0] as const));\n }\n\n /** The names of the components. */\n keys(): readonly K[] {\n return Object.keys(this.c) as K[];\n }\n /** The value of a component, or 0 if the vector does not have it. */\n get(k: string): number {\n return Object.hasOwn(this.c, k) ? Number((this.c as Readonly<Record<string, number>>)[k]) : 0;\n }\n set<K2 extends string>(k: K2, v: number): NDVector<K | K2> {\n return new NDVector({ ...this.c, [k]: v } as Record<K | K2, number>);\n }\n map(f: (name: K, v: number) => number): NDVector<K> {\n const c = {} as Record<K, number>;\n for (const k of this.keys()) c[k] = f(k, this.get(k));\n return new NDVector(c);\n }\n /** The vector with only the names in `keys`. A zero component is dropped, unless `keepZeros` is true. */\n pick<K2 extends K>(keys: readonly K2[], keepZeros = false): NDVector<K2> {\n const c = {} as Record<K2, number>;\n for (const k of keys) {\n const v = this.get(k);\n if (keepZeros || v !== 0) c[k] = v;\n }\n return new NDVector(c);\n }\n /** The vector with the names in `keys` added. A new component is 0. */\n withKeys<K2 extends string>(keys: readonly K2[]): NDVector<K | K2> {\n const c = { ...this.c } as Record<K | K2, number>;\n for (const k of keys) if (!Object.hasOwn(c, k)) c[k] = 0;\n return new NDVector(c);\n }\n #combine<K2 extends string>(o: NDVector<K2>, f: (a: number, b: number) => number): NDVector<K | K2> {\n const c = {} as Record<K | K2, number>;\n for (const k of new Set<K | K2>([...this.keys(), ...o.keys()])) c[k] = f(this.get(k), o.get(k));\n return new NDVector(c);\n }\n add<K2 extends string>(o: NDVector<K2>): NDVector<K | K2> {\n return this.#combine(o, (a, b) => a + b);\n }\n subtract<K2 extends string>(o: NDVector<K2>): NDVector<K | K2> {\n return this.#combine(o, (a, b) => a - b);\n }\n /** The component-wise product. */\n multiply<K2 extends string>(o: NDVector<K2>): NDVector<K | K2> {\n return this.#combine(o, (a, b) => a * b);\n }\n /** Of each component, the value of this vector if it is not 0, else the value of `o`. */\n mergePreferNonZero<K2 extends string>(o: NDVector<K2>): NDVector<K | K2> {\n return this.#combine(o, (a, b) => (a !== 0 ? a : b));\n }\n scale(k: number): NDVector<K> {\n return this.map((_n, v) => v * k);\n }\n clamp(lo: number, hi: number): NDVector<K> {\n return this.map((_n, v) => Math.min(hi, Math.max(lo, v)));\n }\n dot(o: NDVector): number {\n let s = 0;\n for (const k of new Set<string>([...this.keys(), ...o.keys()])) s += this.get(k) * o.get(k);\n return s;\n }\n /** The L2 length. */\n length(): number {\n return Math.sqrt(this.keys().reduce((s, k) => s + this.get(k) ** 2, 0));\n }\n /** The L1 norm. */\n norm1(): number {\n return this.keys().reduce((s, k) => s + Math.abs(this.get(k)), 0);\n }\n /** The largest absolute component. */\n normInf(): number {\n return this.keys().reduce((m, k) => Math.max(m, Math.abs(this.get(k))), 0);\n }\n equals(o: NDVector): boolean {\n const keys = new Set<string>([...this.keys(), ...o.keys()]);\n for (const k of keys) if (this.get(k) !== o.get(k)) return false;\n return true;\n }\n /** True if each component is a finite number. */\n isFinite(): boolean {\n return this.keys().every((k) => Number.isFinite(this.get(k)));\n }\n toJSON(): Readonly<Record<K, number>> {\n return { ...this.c };\n }\n toString(): string {\n return `NDVector(${JSON.stringify(this.c)})`;\n }\n}\n\ntype NDOpNames =\n | \"add\" | \"subtract\" | \"multiply\" | \"mergePreferNonZero\" | \"scale\" | \"clamp\" | \"dot\" | \"length\" | \"norm1\" | \"normInf\"\n | \"equals\" | \"pick\" | \"withKeys\" | \"get\" | \"set\";\n\n/** The ops of `NDVectorDomain`. No op lifts numbers. */\nexport type NDVectorOps = OpTable<NDVector, NDOpNames>;\n\n/** The ops of `LiftedNDVectorDomain`. */\nexport type LiftedNDVectorOps = OpTable<NDVector, NDOpNames, \"add\" | \"subtract\" | \"multiply\" | \"dot\">;\n\nconst ndOps = {\n add: { laws: [\"commutative\", \"associative\"], identity: (): NDVector => NDVector.zero(), params: [\"domain\"] },\n subtract: { params: [\"domain\"] },\n multiply: { laws: [\"commutative\", \"associative\"], params: [\"domain\"] },\n mergePreferNonZero: { laws: [\"associative\", \"idempotent\"], params: [\"domain\"] },\n scale: { params: [\"number\"] },\n clamp: { params: [\"number\", \"number\"] },\n dot: { laws: [\"commutative\"], params: [\"domain\"] },\n length: {},\n norm1: {},\n normInf: {},\n equals: { laws: [\"commutative\"], params: [\"domain\"] },\n pick: {},\n withKeys: {},\n get: { params: [\"string\"] },\n set: { params: [\"string\", \"number\"] },\n} as const satisfies NDVectorOps;\n\nconst isNDVector = (u: unknown): u is NDVector => u instanceof NDVector;\n\n/** The domain of named-component vectors. A number argument is an error here: use `LiftedNDVectorDomain` to lift. */\nexport const NDVectorDomain: Domain<\"NDVector\", NDVector, NDVectorOps> = defineDomain<\"NDVector\", NDVector, NDVectorOps>({\n name: \"NDVector\",\n is: isNDVector,\n valid: (v: NDVector): boolean => v.isFinite(),\n show: (v: NDVector): string => v.toString(),\n encode: (v: NDVector): unknown => v.toJSON(),\n decode: (u: unknown): NDVector => new NDVector(u as Readonly<Record<string, number>>),\n ops: ndOps,\n});\n\n/**\n * The domain of named-component vectors with scalar lifting. A number argument of `add`, `subtract`,\n * `multiply` or `dot` becomes the vector `{ scalar: n }`. This domain is independent of `NDVectorDomain`:\n * the two can be in one program, and neither changes the other.\n */\nexport const LiftedNDVectorDomain: Domain<\"NDVector(lifted)\", NDVector, LiftedNDVectorOps> = defineDomain<\"NDVector(lifted)\", NDVector, LiftedNDVectorOps>({\n name: \"NDVector(lifted)\",\n is: isNDVector,\n fromScalar: (n: number): NDVector => new NDVector({ scalar: n }),\n valid: (v: NDVector): boolean => v.isFinite(),\n show: (v: NDVector): string => v.toString(),\n ops: {\n ...ndOps,\n add: { ...ndOps.add, liftScalar: true },\n subtract: { ...ndOps.subtract, liftScalar: true },\n multiply: { ...ndOps.multiply, liftScalar: true },\n dot: { ...ndOps.dot, liftScalar: true },\n },\n});\n\n/** A plain record whose values are finite numbers. */\nexport type NumRecord = Readonly<Record<string, number>>;\n\n/** The ops of `NumRecordDomain`. */\nexport type NumRecordOps = {\n readonly toNDVector: FnOp<NumRecord, (r: NumRecord) => NDVector>;\n};\n\nconst isNumRecord = (u: unknown): u is NumRecord => {\n if (typeof u !== \"object\" || u === null || Array.isArray(u)) return false;\n const proto: unknown = Object.getPrototypeOf(u);\n return (proto === Object.prototype || proto === null) && Object.values(u).every((v) => typeof v === \"number\" && Number.isFinite(v));\n};\n\n/** The domain of plain number records. Its op `toNDVector` builds a vector from fields, for example from `rec()`. */\nexport const NumRecordDomain: Domain<\"NumRecord\", NumRecord, NumRecordOps> = defineDomain<\"NumRecord\", NumRecord, NumRecordOps>({\n name: \"NumRecord\",\n is: isNumRecord,\n ops: {\n toNDVector: { fn: (r: NumRecord): NDVector => new NDVector(r) },\n },\n});\n","import { defineDomain, type Domain, type FnOp } from \"@mark1russell7/vex\";\n\ntype NumFn1 = (a: number) => number;\ntype NumFn2 = (a: number, b: number) => number;\ntype NumFn3 = (a: number, b: number, c: number) => number;\ntype NumTest = (a: number, b: number) => boolean;\n\n/** The ops of `NumDomain`. */\nexport type NumOps = {\n readonly add: FnOp<number, NumFn2>;\n readonly subtract: FnOp<number, NumFn2>;\n readonly multiply: FnOp<number, NumFn2>;\n readonly divide: FnOp<number, NumFn2>;\n readonly mod: FnOp<number, NumFn2>;\n readonly pow: FnOp<number, NumFn2>;\n readonly min: FnOp<number, NumFn2>;\n readonly max: FnOp<number, NumFn2>;\n readonly clamp: FnOp<number, NumFn3>;\n readonly abs: FnOp<number, NumFn1>;\n readonly negate: FnOp<number, NumFn1>;\n readonly sqrt: FnOp<number, NumFn1>;\n readonly floor: FnOp<number, NumFn1>;\n readonly ceil: FnOp<number, NumFn1>;\n readonly round: FnOp<number, NumFn1>;\n readonly gt: FnOp<number, NumTest>;\n readonly gte: FnOp<number, NumTest>;\n readonly lt: FnOp<number, NumTest>;\n readonly lte: FnOp<number, NumTest>;\n readonly eq: FnOp<number, NumTest>;\n};\n\n/** The domain of numbers. A result that is not a finite number is the error `#NUM!`. */\nexport const NumDomain: Domain<\"Num\", number, NumOps> = defineDomain<\"Num\", number, NumOps>({\n name: \"Num\",\n is: (u: unknown): u is number => typeof u === \"number\",\n valid: (n: number): boolean => Number.isFinite(n),\n show: (n: number): string => String(n),\n ops: {\n add: { fn: (a: number, b: number): number => a + b, laws: [\"commutative\", \"associative\"], identity: (): number => 0, params: [\"number\"] },\n subtract: { fn: (a: number, b: number): number => a - b, params: [\"number\"] },\n multiply: { fn: (a: number, b: number): number => a * b, laws: [\"commutative\", \"associative\"], identity: (): number => 1, params: [\"number\"] },\n divide: { fn: (a: number, b: number): number => a / b, params: [\"number\"] },\n mod: { fn: (a: number, b: number): number => a % b, params: [\"number\"] },\n pow: { fn: (a: number, b: number): number => a ** b, params: [\"number\"] },\n min: { fn: (a: number, b: number): number => Math.min(a, b), laws: [\"commutative\", \"associative\", \"idempotent\"], params: [\"number\"] },\n max: { fn: (a: number, b: number): number => Math.max(a, b), laws: [\"commutative\", \"associative\", \"idempotent\"], params: [\"number\"] },\n clamp: { fn: (a: number, lo: number, hi: number): number => Math.min(hi, Math.max(lo, a)), params: [\"number\", \"number\"] },\n abs: { fn: (a: number): number => Math.abs(a) },\n negate: { fn: (a: number): number => -a },\n sqrt: { fn: (a: number): number => Math.sqrt(a) },\n floor: { fn: (a: number): number => Math.floor(a) },\n ceil: { fn: (a: number): number => Math.ceil(a) },\n round: { fn: (a: number): number => Math.round(a) },\n gt: { fn: (a: number, b: number): boolean => a > b, params: [\"number\"] },\n gte: { fn: (a: number, b: number): boolean => a >= b, params: [\"number\"] },\n lt: { fn: (a: number, b: number): boolean => a < b, params: [\"number\"] },\n lte: { fn: (a: number, b: number): boolean => a <= b, params: [\"number\"] },\n eq: { fn: (a: number, b: number): boolean => a === b, laws: [\"commutative\"], params: [\"number\"] },\n },\n});\n\ntype BoolFn1 = (a: boolean) => boolean;\ntype BoolFn2 = (a: boolean, b: boolean) => boolean;\n\n/** The ops of `BoolDomain`. */\nexport type BoolOps = {\n readonly and: FnOp<boolean, BoolFn2>;\n readonly or: FnOp<boolean, BoolFn2>;\n readonly xor: FnOp<boolean, BoolFn2>;\n readonly not: FnOp<boolean, BoolFn1>;\n readonly eq: FnOp<boolean, BoolFn2>;\n};\n\n/** The domain of booleans. For lazy logic, use the special forms `and`, `or` and `if` of the IR. */\nexport const BoolDomain: Domain<\"Bool\", boolean, BoolOps> = defineDomain<\"Bool\", boolean, BoolOps>({\n name: \"Bool\",\n is: (u: unknown): u is boolean => typeof u === \"boolean\",\n show: (b: boolean): string => String(b),\n ops: {\n and: { fn: (a: boolean, b: boolean): boolean => a && b, laws: [\"commutative\", \"associative\", \"idempotent\"], identity: (): boolean => true, params: [\"boolean\"] },\n or: { fn: (a: boolean, b: boolean): boolean => a || b, laws: [\"commutative\", \"associative\", \"idempotent\"], identity: (): boolean => false, params: [\"boolean\"] },\n xor: { fn: (a: boolean, b: boolean): boolean => a !== b, laws: [\"commutative\", \"associative\"], identity: (): boolean => false, params: [\"boolean\"] },\n not: { fn: (a: boolean): boolean => !a },\n eq: { fn: (a: boolean, b: boolean): boolean => a === b, laws: [\"commutative\"], params: [\"boolean\"] },\n },\n});\n","import { defineDomain, type Domain, type OpTable } from \"@mark1russell7/vex\";\n\n/**\n * An immutable RGBA color. The channels have no fixed range: use `clamp` to limit them. `add` adds the color\n * channels and keeps the larger alpha, so `add` is commutative and associative.\n */\nexport class Color {\n readonly r: number;\n readonly g: number;\n readonly b: number;\n readonly a: number;\n\n constructor(r: number, g: number, b: number, a = 1) {\n this.r = r;\n this.g = g;\n this.b = b;\n this.a = a;\n }\n\n /** This function makes a gray color with the value `n` in each channel. */\n static gray(n: number): Color {\n return new Color(n, n, n, 1);\n }\n /** The black color with alpha 0: the identity of `add`. */\n static readonly none: Color = new Color(0, 0, 0, 0);\n\n /** The channel sum. The alpha is the larger alpha. */\n add(o: Color): Color {\n return new Color(this.r + o.r, this.g + o.g, this.b + o.b, Math.max(this.a, o.a));\n }\n /** Each color channel times `factor`. The alpha does not change. */\n multiply(factor: number): Color {\n return new Color(this.r * factor, this.g * factor, this.b * factor, this.a);\n }\n /** Each color channel in the range from `lo` to `hi`. */\n clamp(lo = 0, hi = 255): Color {\n const c = (x: number): number => Math.min(hi, Math.max(lo, x));\n return new Color(c(this.r), c(this.g), c(this.b), this.a);\n }\n /** The linear mix from this color to `o`. */\n mix(o: Color, t: number): Color {\n const m = (x: number, y: number): number => x + (y - x) * t;\n return new Color(m(this.r, o.r), m(this.g, o.g), m(this.b, o.b), m(this.a, o.a));\n }\n /** The relative luminance (Rec. 709 weights) of the color channels. */\n luminance(): number {\n return 0.2126 * this.r + 0.7152 * this.g + 0.0722 * this.b;\n }\n equals(o: Color): boolean {\n return this.r === o.r && this.g === o.g && this.b === o.b && this.a === o.a;\n }\n toArray(): readonly [number, number, number, number] {\n return [this.r, this.g, this.b, this.a];\n }\n toString(): string {\n return `rgba(${this.r}, ${this.g}, ${this.b}, ${this.a})`;\n }\n}\n\n/** The ops of `ColorDomain`. */\nexport type ColorOps = OpTable<Color, \"add\" | \"multiply\" | \"clamp\" | \"mix\" | \"luminance\" | \"equals\">;\n\n/** The domain of colors. */\nexport const ColorDomain: Domain<\"Color\", Color, ColorOps> = defineDomain<\"Color\", Color, ColorOps>({\n name: \"Color\",\n is: (u: unknown): u is Color => u instanceof Color,\n fromScalar: (n: number): Color => Color.gray(n),\n valid: (c: Color): boolean => c.toArray().every((x) => Number.isFinite(x)),\n show: (c: Color): string => c.toString(),\n encode: (c: Color): readonly number[] => c.toArray(),\n decode: (u: unknown): Color => {\n const [r, g, b, a] = (Array.isArray(u) ? u : []) as readonly number[];\n return new Color(r ?? Number.NaN, g ?? Number.NaN, b ?? Number.NaN, a ?? 1);\n },\n ops: {\n add: { laws: [\"commutative\", \"associative\"], identity: (): Color => Color.none, params: [\"domain\"] },\n multiply: { params: [\"number\"] },\n clamp: { params: [\"number\", \"number\"] },\n mix: { params: [\"domain\", \"number\"] },\n luminance: {},\n equals: { laws: [\"commutative\"], params: [\"domain\"] },\n },\n});\n","import { defineDomain, type Domain, type OpTable } from \"@mark1russell7/vex\";\nimport { Vec2 } from \"./vec2.ts\";\n\n/** An immutable angle in radians. */\nexport class Angle {\n readonly radians: number;\n\n constructor(radians: number) {\n this.radians = radians;\n }\n\n /** This function makes an angle from degrees. */\n static fromDegrees(deg: number): Angle {\n return new Angle((deg * Math.PI) / 180);\n }\n /** The zero angle: the identity of `add`. */\n static readonly zero: Angle = new Angle(0);\n\n add(o: Angle): Angle {\n return new Angle(this.radians + o.radians);\n }\n subtract(o: Angle): Angle {\n return new Angle(this.radians - o.radians);\n }\n scale(k: number): Angle {\n return new Angle(this.radians * k);\n }\n /** The same angle in the range from -π (included) to π (not included). */\n normalize(): Angle {\n const twoPi = Math.PI * 2;\n return new Angle((((this.radians + Math.PI) % twoPi) + twoPi) % twoPi - Math.PI);\n }\n /** The vector with this angle and the length `length`. The result is a `Vec2`, so a chain continues in that domain. */\n toVec2(length = 1): Vec2 {\n return new Vec2(Math.cos(this.radians) * length, Math.sin(this.radians) * length);\n }\n sin(): number {\n return Math.sin(this.radians);\n }\n cos(): number {\n return Math.cos(this.radians);\n }\n degrees(): number {\n return (this.radians * 180) / Math.PI;\n }\n toString(): string {\n return `${this.radians} rad`;\n }\n}\n\n/** The ops of `AngleDomain`. */\nexport type AngleOps = OpTable<Angle, \"add\" | \"subtract\" | \"scale\" | \"normalize\" | \"toVec2\" | \"sin\" | \"cos\" | \"degrees\", \"add\" | \"subtract\">;\n\n/** The domain of angles. A number argument of `add` or `subtract` lifts to an angle in radians. */\nexport const AngleDomain: Domain<\"Angle\", Angle, AngleOps> = defineDomain<\"Angle\", Angle, AngleOps>({\n name: \"Angle\",\n is: (u: unknown): u is Angle => u instanceof Angle,\n fromScalar: (n: number): Angle => new Angle(n),\n valid: (a: Angle): boolean => Number.isFinite(a.radians),\n show: (a: Angle): string => a.toString(),\n encode: (a: Angle): number => a.radians,\n decode: (u: unknown): Angle => new Angle(typeof u === \"number\" ? u : Number.NaN),\n ops: {\n add: { laws: [\"commutative\", \"associative\"], identity: (): Angle => Angle.zero, liftScalar: true, params: [\"domain\"] },\n subtract: { liftScalar: true, params: [\"domain\"] },\n scale: { params: [\"number\"] },\n normalize: {},\n toVec2: { params: [\"number\"] },\n sin: {},\n cos: {},\n degrees: {},\n },\n});\n","import { defineDomain, type Domain, type OpTable } from \"@mark1russell7/vex\";\n\n/** A wrapper around a function. Its op `invoke` calls the function. It shows how a domain can hold behavior. */\nexport class Fn {\n readonly fn: (...args: never[]) => unknown;\n\n constructor(fn: (...args: never[]) => unknown) {\n this.fn = fn;\n }\n invoke(...args: readonly unknown[]): unknown {\n return (this.fn as (...a: readonly unknown[]) => unknown)(...args);\n }\n}\n\n/** The ops of `FnDomain`. */\nexport type FnOps = OpTable<Fn, \"invoke\">;\n\n/** The domain of function wrappers. */\nexport const FnDomain: Domain<\"Fn\", Fn, FnOps> = defineDomain<\"Fn\", Fn, FnOps>({\n name: \"Fn\",\n is: (u: unknown): u is Fn => u instanceof Fn,\n ops: { invoke: {} },\n});\n\n/**\n * A value that is there (`top`) or not there (`bottom`). It shows a domain whose ops take functions. A Vex\n * program can also hold this state in its own errors, so most programs do not need this domain.\n */\nexport class Maybe<T> {\n readonly #has: boolean;\n readonly #value: T | undefined;\n\n private constructor(has: boolean, value: T | undefined) {\n this.#has = has;\n this.#value = value;\n }\n static top<T>(value: T): Maybe<T> {\n return new Maybe<T>(true, value);\n }\n static bottom<T = never>(): Maybe<T> {\n return new Maybe<T>(false, undefined);\n }\n isTop(): boolean {\n return this.#has;\n }\n isBottom(): boolean {\n return !this.#has;\n }\n map<U>(f: (value: T) => U): Maybe<U> {\n return this.#has ? Maybe.top(f(this.#value as T)) : Maybe.bottom<U>();\n }\n chain<U>(f: (value: T) => Maybe<U>): Maybe<U> {\n return this.#has ? f(this.#value as T) : Maybe.bottom<U>();\n }\n valueOr<U>(fallback: U): T | U {\n return this.#has ? (this.#value as T) : fallback;\n }\n}\n\n/** The ops of `MaybeDomain`. */\nexport type MaybeOps = OpTable<Maybe<unknown>, \"isTop\" | \"isBottom\" | \"map\" | \"chain\" | \"valueOr\">;\n\n/** The domain of `Maybe` values. */\nexport const MaybeDomain: Domain<\"Maybe\", Maybe<unknown>, MaybeOps> = defineDomain<\"Maybe\", Maybe<unknown>, MaybeOps>({\n name: \"Maybe\",\n is: (u: unknown): u is Maybe<unknown> => u instanceof Maybe,\n ops: { isTop: {}, isBottom: {}, map: {}, chain: {}, valueOr: {} },\n});\n"],"mappings":";;;;;;AAMA,IAAa,OAAb,MAAa,KAAK;CAChB;CACA;CAEA,YAAY,GAAW,GAAW;EAChC,KAAK,IAAI;EACT,KAAK,IAAI;CACX;;CAGA,OAAO,GAAG,GAAW,GAAiB;EACpC,OAAO,I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;CACA,OAAO,SAA8B;EACnC,OAAO,IAAI,MAAS,OAAO,KAAA,CAAS;CACtC;CACA,QAAiB;EACf,OAAO,KAAK;CACd;CACA,WAAoB;EAClB,OAAO,CAAC,KAAK;CACf;CACA,IAAO,GAA8B;EACnC,OAAO,KAAK,OAAO,MAAM,IAAI,EAAE,KAAK,MAAW,CAAC,IAAI,MAAM,OAAU;CACtE;CACA,MAAS,GAAqC;EAC5C,OAAO,KAAK,OAAO,EAAE,KAAK,MAAW,IAAI,MAAM,OAAU;CAC3D;CACA,QAAW,UAAoB;EAC7B,OAAO,KAAK,OAAQ,KAAK,SAAe;CAC1C;AACF;;AAMA,MAAa,cAAyD,aAAgD;CACpH,MAAM;CACN,KAAK,MAAoC,aAAa;CACtD,KAAK;EAAE,OAAO,CAAC;EAAG,UAAU,CAAC;EAAG,KAAK,CAAC;EAAG,OAAO,CAAC;EAAG,SAAS,CAAC;CAAE;AAClE,CAAC"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/vec2.ts","../src/ndvector.ts","../src/scalars.ts","../src/text.ts","../src/color.ts","../src/angle.ts","../src/wrappers.ts"],"sourcesContent":["import { defineDomain, type Domain, type OpTable } from \"@mark1russell7/vex\";\n\n/**\n * An immutable 2D vector. The methods live on the prototype, so a vector costs two numbers. Division is real\n * division: a division by zero gives an infinite component, and the domain check turns it into `#NUM!`.\n */\nexport class Vec2 {\n readonly x: number;\n readonly y: number;\n\n constructor(x: number, y: number) {\n this.x = x;\n this.y = y;\n }\n\n /** This function makes a vector. */\n static of(x: number, y: number): Vec2 {\n return new Vec2(x, y);\n }\n /** This function makes a vector with the same value in both components. */\n static scalar(n: number): Vec2 {\n return new Vec2(n, n);\n }\n /** The zero vector. */\n static readonly zero: Vec2 = new Vec2(0, 0);\n /** The vector of ones. */\n static readonly one: Vec2 = new Vec2(1, 1);\n\n add(o: Vec2): Vec2 {\n return new Vec2(this.x + o.x, this.y + o.y);\n }\n subtract(o: Vec2): Vec2 {\n return new Vec2(this.x - o.x, this.y - o.y);\n }\n /** The component-wise product. */\n multiply(o: Vec2): Vec2 {\n return new Vec2(this.x * o.x, this.y * o.y);\n }\n /** The component-wise quotient. A zero component of `o` gives an infinite component. */\n divide(o: Vec2): Vec2 {\n return new Vec2(this.x / o.x, this.y / o.y);\n }\n /** The component-wise minimum. */\n min(o: Vec2): Vec2 {\n return new Vec2(Math.min(this.x, o.x), Math.min(this.y, o.y));\n }\n /** The component-wise maximum. */\n max(o: Vec2): Vec2 {\n return new Vec2(Math.max(this.x, o.x), Math.max(this.y, o.y));\n }\n scale(k: number): Vec2 {\n return new Vec2(this.x * k, this.y * k);\n }\n negate(): Vec2 {\n return new Vec2(-this.x, -this.y);\n }\n halve(): Vec2 {\n return this.scale(0.5);\n }\n abs(): Vec2 {\n return new Vec2(Math.abs(this.x), Math.abs(this.y));\n }\n round(): Vec2 {\n return new Vec2(Math.round(this.x), Math.round(this.y));\n }\n floor(): Vec2 {\n return new Vec2(Math.floor(this.x), Math.floor(this.y));\n }\n ceil(): Vec2 {\n return new Vec2(Math.ceil(this.x), Math.ceil(this.y));\n }\n clamp(lo: number, hi: number): Vec2 {\n return new Vec2(Math.min(hi, Math.max(lo, this.x)), Math.min(hi, Math.max(lo, this.y)));\n }\n swap(): Vec2 {\n return new Vec2(this.y, this.x);\n }\n /** The vector turned by `radians`, counterclockwise. */\n rotate(radians: number): Vec2 {\n const c = Math.cos(radians);\n const s = Math.sin(radians);\n return new Vec2(c * this.x - s * this.y, s * this.x + c * this.y);\n }\n /** The vector with length 1. The zero vector stays zero. */\n normalize(): Vec2 {\n const len = this.length();\n return len === 0 ? this : this.scale(1 / len);\n }\n /** The linear interpolation from this vector to `o`. */\n lerp(o: Vec2, t: number): Vec2 {\n return new Vec2(this.x + (o.x - this.x) * t, this.y + (o.y - this.y) * t);\n }\n length(): number {\n return Math.hypot(this.x, this.y);\n }\n lengthSq(): number {\n return this.x * this.x + this.y * this.y;\n }\n distance(o: Vec2): number {\n return Math.hypot(this.x - o.x, this.y - o.y);\n }\n dot(o: Vec2): number {\n return this.x * o.x + this.y * o.y;\n }\n /** The z component of the 3D cross product. */\n cross(o: Vec2): number {\n return this.x * o.y - this.y * o.x;\n }\n /** The angle of the vector, in radians, from the x axis. */\n angle(): number {\n return Math.atan2(this.y, this.x);\n }\n /** The product of the components: the area of a size vector. */\n area(): number {\n return this.x * this.y;\n }\n /** The sum of the components. */\n sum(): number {\n return this.x + this.y;\n }\n minComponent(): number {\n return Math.min(this.x, this.y);\n }\n maxComponent(): number {\n return Math.max(this.x, this.y);\n }\n equals(o: Vec2): boolean {\n return this.x === o.x && this.y === o.y;\n }\n anyNonPositive(): boolean {\n return this.x <= 0 || this.y <= 0;\n }\n allPositive(): boolean {\n return this.x > 0 && this.y > 0;\n }\n anyNegative(): boolean {\n return this.x < 0 || this.y < 0;\n }\n allNonNegative(): boolean {\n return this.x >= 0 && this.y >= 0;\n }\n anyZero(): boolean {\n return this.x === 0 || this.y === 0;\n }\n allZero(): boolean {\n return this.x === 0 && this.y === 0;\n }\n anyLessThan(n: number): boolean {\n return this.x < n || this.y < n;\n }\n allLessThan(n: number): boolean {\n return this.x < n && this.y < n;\n }\n anyGreaterThan(n: number): boolean {\n return this.x > n || this.y > n;\n }\n allGreaterThan(n: number): boolean {\n return this.x > n && this.y > n;\n }\n toJSON(): { readonly x: number; readonly y: number } {\n return { x: this.x, y: this.y };\n }\n toString(): string {\n return `(${this.x}, ${this.y})`;\n }\n}\n\ntype Vec2OpNames =\n | \"add\" | \"subtract\" | \"multiply\" | \"divide\" | \"min\" | \"max\" | \"scale\" | \"negate\" | \"halve\" | \"abs\" | \"round\" | \"floor\"\n | \"ceil\" | \"clamp\" | \"swap\" | \"rotate\" | \"normalize\" | \"lerp\" | \"length\" | \"lengthSq\" | \"distance\" | \"dot\" | \"cross\"\n | \"angle\" | \"area\" | \"sum\" | \"minComponent\" | \"maxComponent\" | \"equals\" | \"anyNonPositive\" | \"allPositive\"\n | \"anyNegative\" | \"allNonNegative\" | \"anyZero\" | \"allZero\" | \"anyLessThan\" | \"allLessThan\" | \"anyGreaterThan\"\n | \"allGreaterThan\";\n\n/** The ops of `Vec2Domain`. */\nexport type Vec2Ops = OpTable<Vec2, Vec2OpNames, \"add\" | \"subtract\" | \"multiply\" | \"divide\" | \"min\" | \"max\">;\n\n/** The domain of 2D vectors. A number argument of a component-wise op lifts to a vector with that value twice. */\nexport const Vec2Domain: Domain<\"Vec2\", Vec2, Vec2Ops> = defineDomain<\"Vec2\", Vec2, Vec2Ops>({\n name: \"Vec2\",\n is: (u: unknown): u is Vec2 => u instanceof Vec2,\n fromScalar: (n: number): Vec2 => Vec2.scalar(n),\n valid: (v: Vec2): boolean => Number.isFinite(v.x) && Number.isFinite(v.y),\n show: (v: Vec2): string => v.toString(),\n encode: (v: Vec2): readonly [number, number] => [v.x, v.y],\n decode: (u: unknown): Vec2 => {\n // Stryker disable next-line ArrayDeclaration: a list with a text gives NaN components, like the empty list\n const [x, y] = Array.isArray(u) ? (u as readonly unknown[]) : [];\n return new Vec2(typeof x === \"number\" ? x : Number.NaN, typeof y === \"number\" ? y : Number.NaN);\n },\n ops: {\n add: { laws: [\"commutative\", \"associative\"], identity: () => Vec2.zero, liftScalar: true, params: [\"domain\"] },\n subtract: { liftScalar: true, params: [\"domain\"] },\n multiply: { laws: [\"commutative\", \"associative\"], identity: () => Vec2.one, liftScalar: true, params: [\"domain\"] },\n divide: { liftScalar: true, params: [\"domain\"] },\n min: { laws: [\"commutative\", \"associative\", \"idempotent\"], liftScalar: true, params: [\"domain\"] },\n max: { laws: [\"commutative\", \"associative\", \"idempotent\"], liftScalar: true, params: [\"domain\"] },\n scale: { params: [\"number\"] },\n negate: {},\n halve: {},\n abs: {},\n round: {},\n floor: {},\n ceil: {},\n clamp: { params: [\"number\", \"number\"] },\n swap: {},\n rotate: { params: [\"number\"] },\n normalize: {},\n lerp: { params: [\"domain\", \"number\"] },\n length: {},\n lengthSq: {},\n distance: { params: [\"domain\"] },\n dot: { laws: [\"commutative\"], params: [\"domain\"] },\n cross: { params: [\"domain\"] },\n angle: {},\n area: {},\n sum: {},\n minComponent: {},\n maxComponent: {},\n equals: { laws: [\"commutative\"], params: [\"domain\"] },\n anyNonPositive: {},\n allPositive: {},\n anyNegative: {},\n allNonNegative: {},\n anyZero: {},\n allZero: {},\n anyLessThan: { params: [\"number\"] },\n allLessThan: { params: [\"number\"] },\n anyGreaterThan: { params: [\"number\"] },\n allGreaterThan: { params: [\"number\"] },\n },\n});\n","import { defineDomain, type Domain, type FnOp, type OpTable } from \"@mark1russell7/vex\";\n\n/**\n * An immutable vector with named components. The type parameter `K` is the set of component names. Ops that\n * combine two vectors use the union of their names, and a missing component counts as 0. The constructor\n * keeps each number, also a number that is not finite: the domain check reports such a value as `#NUM!`.\n */\nexport class NDVector<K extends string = string> {\n /** The components: name to number. */\n readonly c: Readonly<Record<K, number>>;\n\n constructor(c: Readonly<Record<K, number>>) {\n this.c = Object.freeze({ ...c });\n }\n\n /** This function makes a vector from name and value pairs. */\n static of<K extends string>(entries: readonly (readonly [K, number])[]): NDVector<K> {\n const c = {} as Record<K, number>;\n for (const [k, v] of entries) c[k] = v;\n return new NDVector(c);\n }\n /** This function makes the zero vector over the names `keys`. */\n static zero<K extends string>(keys: readonly K[] = []): NDVector<K> {\n return NDVector.of(keys.map((k) => [k, 0] as const));\n }\n\n /** The names of the components. */\n keys(): readonly K[] {\n return Object.keys(this.c) as K[];\n }\n /** The value of a component, or 0 if the vector does not have it. */\n get(k: string): number {\n return Object.hasOwn(this.c, k) ? Number((this.c as Readonly<Record<string, number>>)[k]) : 0;\n }\n set<K2 extends string>(k: K2, v: number): NDVector<K | K2> {\n return new NDVector({ ...this.c, [k]: v } as Record<K | K2, number>);\n }\n map(f: (name: K, v: number) => number): NDVector<K> {\n const c = {} as Record<K, number>;\n for (const k of this.keys()) c[k] = f(k, this.get(k));\n return new NDVector(c);\n }\n /** The vector with only the names in `keys`. A zero component is dropped, unless `keepZeros` is true. */\n pick<K2 extends K>(keys: readonly K2[], keepZeros = false): NDVector<K2> {\n const c = {} as Record<K2, number>;\n for (const k of keys) {\n const v = this.get(k);\n if (keepZeros || v !== 0) c[k] = v;\n }\n return new NDVector(c);\n }\n /** The vector with the names in `keys` added. A new component is 0. */\n withKeys<K2 extends string>(keys: readonly K2[]): NDVector<K | K2> {\n const c = { ...this.c } as Record<K | K2, number>;\n for (const k of keys) if (!Object.hasOwn(c, k)) c[k] = 0;\n return new NDVector(c);\n }\n #combine<K2 extends string>(o: NDVector<K2>, f: (a: number, b: number) => number): NDVector<K | K2> {\n const c = {} as Record<K | K2, number>;\n for (const k of new Set<K | K2>([...this.keys(), ...o.keys()])) c[k] = f(this.get(k), o.get(k));\n return new NDVector(c);\n }\n add<K2 extends string>(o: NDVector<K2>): NDVector<K | K2> {\n return this.#combine(o, (a, b) => a + b);\n }\n subtract<K2 extends string>(o: NDVector<K2>): NDVector<K | K2> {\n return this.#combine(o, (a, b) => a - b);\n }\n /** The component-wise product. */\n multiply<K2 extends string>(o: NDVector<K2>): NDVector<K | K2> {\n return this.#combine(o, (a, b) => a * b);\n }\n /** Of each component, the value of this vector if it is not 0, else the value of `o`. */\n mergePreferNonZero<K2 extends string>(o: NDVector<K2>): NDVector<K | K2> {\n return this.#combine(o, (a, b) => (a !== 0 ? a : b));\n }\n scale(k: number): NDVector<K> {\n return this.map((_n, v) => v * k);\n }\n clamp(lo: number, hi: number): NDVector<K> {\n return this.map((_n, v) => Math.min(hi, Math.max(lo, v)));\n }\n dot(o: NDVector): number {\n let s = 0;\n for (const k of new Set<string>([...this.keys(), ...o.keys()])) s += this.get(k) * o.get(k);\n return s;\n }\n /** The L2 length. */\n length(): number {\n return Math.sqrt(this.keys().reduce((s, k) => s + this.get(k) ** 2, 0));\n }\n /** The L1 norm. */\n norm1(): number {\n return this.keys().reduce((s, k) => s + Math.abs(this.get(k)), 0);\n }\n /** The largest absolute component. */\n normInf(): number {\n return this.keys().reduce((m, k) => Math.max(m, Math.abs(this.get(k))), 0);\n }\n equals(o: NDVector): boolean {\n const keys = new Set<string>([...this.keys(), ...o.keys()]);\n for (const k of keys) if (this.get(k) !== o.get(k)) return false;\n return true;\n }\n /** True if each component is a finite number. */\n isFinite(): boolean {\n return this.keys().every((k) => Number.isFinite(this.get(k)));\n }\n toJSON(): Readonly<Record<K, number>> {\n return { ...this.c };\n }\n toString(): string {\n return `NDVector(${JSON.stringify(this.c)})`;\n }\n}\n\ntype NDOpNames =\n | \"add\" | \"subtract\" | \"multiply\" | \"mergePreferNonZero\" | \"scale\" | \"clamp\" | \"dot\" | \"length\" | \"norm1\" | \"normInf\"\n | \"equals\" | \"pick\" | \"withKeys\" | \"get\" | \"set\";\n\n/** The ops of `NDVectorDomain`. No op lifts numbers. */\nexport type NDVectorOps = OpTable<NDVector, NDOpNames>;\n\n/** The ops of `LiftedNDVectorDomain`. */\nexport type LiftedNDVectorOps = OpTable<NDVector, NDOpNames, \"add\" | \"subtract\" | \"multiply\" | \"dot\">;\n\nconst ndOps = {\n add: { laws: [\"commutative\", \"associative\"], identity: (): NDVector => NDVector.zero(), params: [\"domain\"] },\n subtract: { params: [\"domain\"] },\n multiply: { laws: [\"commutative\", \"associative\"], params: [\"domain\"] },\n mergePreferNonZero: { laws: [\"associative\", \"idempotent\"], params: [\"domain\"] },\n scale: { params: [\"number\"] },\n clamp: { params: [\"number\", \"number\"] },\n dot: { laws: [\"commutative\"], params: [\"domain\"] },\n length: {},\n norm1: {},\n normInf: {},\n equals: { laws: [\"commutative\"], params: [\"domain\"] },\n pick: {},\n withKeys: {},\n get: { params: [\"string\"] },\n set: { params: [\"string\", \"number\"] },\n} as const satisfies NDVectorOps;\n\nconst isNDVector = (u: unknown): u is NDVector => u instanceof NDVector;\n\n/** The domain of named-component vectors. A number argument is an error here: use `LiftedNDVectorDomain` to lift. */\nexport const NDVectorDomain: Domain<\"NDVector\", NDVector, NDVectorOps> = defineDomain<\"NDVector\", NDVector, NDVectorOps>({\n name: \"NDVector\",\n is: isNDVector,\n valid: (v: NDVector): boolean => v.isFinite(),\n show: (v: NDVector): string => v.toString(),\n encode: (v: NDVector): unknown => v.toJSON(),\n decode: (u: unknown): NDVector => new NDVector(u as Readonly<Record<string, number>>),\n ops: ndOps,\n});\n\n/**\n * The domain of named-component vectors with scalar lifting. A number argument of `add`, `subtract`,\n * `multiply` or `dot` becomes the vector `{ scalar: n }`. This domain is independent of `NDVectorDomain`:\n * the two can be in one program, and neither changes the other.\n */\nexport const LiftedNDVectorDomain: Domain<\"NDVector(lifted)\", NDVector, LiftedNDVectorOps> = defineDomain<\"NDVector(lifted)\", NDVector, LiftedNDVectorOps>({\n name: \"NDVector(lifted)\",\n is: isNDVector,\n fromScalar: (n: number): NDVector => new NDVector({ scalar: n }),\n valid: (v: NDVector): boolean => v.isFinite(),\n show: (v: NDVector): string => v.toString(),\n ops: {\n ...ndOps,\n add: { ...ndOps.add, liftScalar: true },\n subtract: { ...ndOps.subtract, liftScalar: true },\n multiply: { ...ndOps.multiply, liftScalar: true },\n dot: { ...ndOps.dot, liftScalar: true },\n },\n});\n\n/** A plain record whose values are finite numbers. */\nexport type NumRecord = Readonly<Record<string, number>>;\n\n/** The ops of `NumRecordDomain`. */\nexport type NumRecordOps = {\n readonly toNDVector: FnOp<NumRecord, (r: NumRecord) => NDVector>;\n};\n\nconst isNumRecord = (u: unknown): u is NumRecord => {\n if (typeof u !== \"object\" || u === null || Array.isArray(u)) return false;\n const proto: unknown = Object.getPrototypeOf(u);\n // Number.isFinite gives false for each value that is not a number.\n return (proto === Object.prototype || proto === null) && Object.values(u).every((v) => Number.isFinite(v));\n};\n\n/** The domain of plain number records. Its op `toNDVector` builds a vector from fields, for example from `rec()`. */\nexport const NumRecordDomain: Domain<\"NumRecord\", NumRecord, NumRecordOps> = defineDomain<\"NumRecord\", NumRecord, NumRecordOps>({\n name: \"NumRecord\",\n is: isNumRecord,\n ops: {\n toNDVector: { fn: (r: NumRecord): NDVector => new NDVector(r) },\n },\n});\n","import { defineDomain, type Domain, type FnOp } from \"@mark1russell7/vex\";\n\ntype NumFn1 = (a: number) => number;\ntype NumFn2 = (a: number, b: number) => number;\ntype NumFn3 = (a: number, b: number, c: number) => number;\ntype NumTest = (a: number, b: number) => boolean;\n\n/** The ops of `NumDomain`. */\nexport type NumOps = {\n readonly add: FnOp<number, NumFn2>;\n readonly subtract: FnOp<number, NumFn2>;\n readonly multiply: FnOp<number, NumFn2>;\n readonly divide: FnOp<number, NumFn2>;\n readonly mod: FnOp<number, NumFn2>;\n readonly pow: FnOp<number, NumFn2>;\n readonly min: FnOp<number, NumFn2>;\n readonly max: FnOp<number, NumFn2>;\n readonly clamp: FnOp<number, NumFn3>;\n readonly abs: FnOp<number, NumFn1>;\n readonly negate: FnOp<number, NumFn1>;\n readonly sqrt: FnOp<number, NumFn1>;\n readonly floor: FnOp<number, NumFn1>;\n readonly ceil: FnOp<number, NumFn1>;\n readonly round: FnOp<number, NumFn1>;\n readonly gt: FnOp<number, NumTest>;\n readonly gte: FnOp<number, NumTest>;\n readonly lt: FnOp<number, NumTest>;\n readonly lte: FnOp<number, NumTest>;\n readonly eq: FnOp<number, NumTest>;\n};\n\n/** The domain of numbers. A result that is not a finite number is the error `#NUM!`. */\nexport const NumDomain: Domain<\"Num\", number, NumOps> = defineDomain<\"Num\", number, NumOps>({\n name: \"Num\",\n is: (u: unknown): u is number => typeof u === \"number\",\n valid: (n: number): boolean => Number.isFinite(n),\n show: (n: number): string => String(n),\n ops: {\n add: { fn: (a: number, b: number): number => a + b, laws: [\"commutative\", \"associative\"], identity: (): number => 0, params: [\"number\"] },\n subtract: { fn: (a: number, b: number): number => a - b, params: [\"number\"] },\n multiply: { fn: (a: number, b: number): number => a * b, laws: [\"commutative\", \"associative\"], identity: (): number => 1, params: [\"number\"] },\n divide: { fn: (a: number, b: number): number => a / b, params: [\"number\"] },\n mod: { fn: (a: number, b: number): number => a % b, params: [\"number\"] },\n pow: { fn: (a: number, b: number): number => a ** b, params: [\"number\"] },\n min: { fn: (a: number, b: number): number => Math.min(a, b), laws: [\"commutative\", \"associative\", \"idempotent\"], params: [\"number\"] },\n max: { fn: (a: number, b: number): number => Math.max(a, b), laws: [\"commutative\", \"associative\", \"idempotent\"], params: [\"number\"] },\n clamp: { fn: (a: number, lo: number, hi: number): number => Math.min(hi, Math.max(lo, a)), params: [\"number\", \"number\"] },\n abs: { fn: (a: number): number => Math.abs(a) },\n negate: { fn: (a: number): number => -a },\n sqrt: { fn: (a: number): number => Math.sqrt(a) },\n floor: { fn: (a: number): number => Math.floor(a) },\n ceil: { fn: (a: number): number => Math.ceil(a) },\n round: { fn: (a: number): number => Math.round(a) },\n gt: { fn: (a: number, b: number): boolean => a > b, params: [\"number\"] },\n gte: { fn: (a: number, b: number): boolean => a >= b, params: [\"number\"] },\n lt: { fn: (a: number, b: number): boolean => a < b, params: [\"number\"] },\n lte: { fn: (a: number, b: number): boolean => a <= b, params: [\"number\"] },\n eq: { fn: (a: number, b: number): boolean => a === b, laws: [\"commutative\"], params: [\"number\"] },\n },\n});\n\ntype BoolFn1 = (a: boolean) => boolean;\ntype BoolFn2 = (a: boolean, b: boolean) => boolean;\n\n/** The ops of `BoolDomain`. */\nexport type BoolOps = {\n readonly and: FnOp<boolean, BoolFn2>;\n readonly or: FnOp<boolean, BoolFn2>;\n readonly xor: FnOp<boolean, BoolFn2>;\n readonly not: FnOp<boolean, BoolFn1>;\n readonly eq: FnOp<boolean, BoolFn2>;\n};\n\n/** The domain of booleans. For lazy logic, use the special forms `and`, `or` and `if` of the IR. */\nexport const BoolDomain: Domain<\"Bool\", boolean, BoolOps> = defineDomain<\"Bool\", boolean, BoolOps>({\n name: \"Bool\",\n is: (u: unknown): u is boolean => typeof u === \"boolean\",\n show: (b: boolean): string => String(b),\n ops: {\n and: { fn: (a: boolean, b: boolean): boolean => a && b, laws: [\"commutative\", \"associative\", \"idempotent\"], identity: (): boolean => true, params: [\"boolean\"] },\n or: { fn: (a: boolean, b: boolean): boolean => a || b, laws: [\"commutative\", \"associative\", \"idempotent\"], identity: (): boolean => false, params: [\"boolean\"] },\n xor: { fn: (a: boolean, b: boolean): boolean => a !== b, laws: [\"commutative\", \"associative\"], identity: (): boolean => false, params: [\"boolean\"] },\n not: { fn: (a: boolean): boolean => !a },\n eq: { fn: (a: boolean, b: boolean): boolean => a === b, laws: [\"commutative\"], params: [\"boolean\"] },\n },\n});\n","/**\n * The domain of texts. The op `concat` joins a text and a value of any kind. It is the operator `&` of a\n * spreadsheet, and of the formula language of the sheet app.\n */\nimport { defineDomain, type Domain, type FnOp } from \"@mark1russell7/vex\";\n\ntype TextFn1 = (a: string) => string;\n\n/** The ops of `TextDomain`. */\nexport type TextOps = {\n readonly concat: FnOp<string, (a: string, b: unknown) => string>;\n readonly length: FnOp<string, (a: string) => number>;\n readonly upper: FnOp<string, TextFn1>;\n readonly lower: FnOp<string, TextFn1>;\n readonly trim: FnOp<string, TextFn1>;\n readonly contains: FnOp<string, (a: string, b: string) => boolean>;\n readonly eq: FnOp<string, (a: string, b: string) => boolean>;\n};\n\n/**\n * This function gives the text of a value for `concat`. A text stays. A plain record gives its JSON text. Another\n * value, for example a number or a `Vec2`, gives its own text.\n */\nconst textOf = (u: unknown): string => (typeof u === \"object\" && u !== null && Object.getPrototypeOf(u) === Object.prototype ? JSON.stringify(u) : String(u));\n\n/** The domain of texts. */\nexport const TextDomain: Domain<\"Text\", string, TextOps> = defineDomain<\"Text\", string, TextOps>({\n name: \"Text\",\n is: (u: unknown): u is string => typeof u === \"string\",\n show: (s: string): string => JSON.stringify(s),\n ops: {\n concat: { fn: (a: string, b: unknown): string => a + textOf(b), params: [\"any\"] },\n // The length counts UTF-16 code units, like the function LEN of a spreadsheet.\n length: { fn: (a: string): number => a.length },\n upper: { fn: (a: string): string => a.toUpperCase() },\n lower: { fn: (a: string): string => a.toLowerCase() },\n trim: { fn: (a: string): string => a.trim() },\n contains: { fn: (a: string, b: string): boolean => a.includes(b), params: [\"string\"] },\n eq: { fn: (a: string, b: string): boolean => a === b, laws: [\"commutative\"], params: [\"string\"] },\n },\n});\n","import { defineDomain, type Domain, type OpTable } from \"@mark1russell7/vex\";\n\n/** The linear value of an sRGB channel from 0 to 255, with the sRGB transfer function of WCAG 2.1. */\nfunction linear(channel: number): number {\n const s = Math.min(1, Math.max(0, channel / 255));\n // For integer channels, the sRGB threshold 0.04045 and the WCAG threshold 0.03928 give the same result.\n return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;\n}\n\n/**\n * An immutable RGBA color. The channels have no fixed range: use `clamp` to limit them. `add` adds the color\n * channels and keeps the larger alpha, so `add` is commutative and associative.\n */\nexport class Color {\n readonly r: number;\n readonly g: number;\n readonly b: number;\n readonly a: number;\n\n constructor(r: number, g: number, b: number, a = 1) {\n this.r = r;\n this.g = g;\n this.b = b;\n this.a = a;\n }\n\n /** This function makes a gray color with the value `n` in each channel. */\n static gray(n: number): Color {\n return new Color(n, n, n, 1);\n }\n /** The black color with alpha 0: the identity of `add`. */\n static readonly none: Color = new Color(0, 0, 0, 0);\n\n /** The channel sum. The alpha is the larger alpha. */\n add(o: Color): Color {\n return new Color(this.r + o.r, this.g + o.g, this.b + o.b, Math.max(this.a, o.a));\n }\n /** Each color channel times `factor`. The alpha does not change. */\n multiply(factor: number): Color {\n return new Color(this.r * factor, this.g * factor, this.b * factor, this.a);\n }\n /** Each color channel in the range from `lo` to `hi`. */\n clamp(lo = 0, hi = 255): Color {\n const c = (x: number): number => Math.min(hi, Math.max(lo, x));\n return new Color(c(this.r), c(this.g), c(this.b), this.a);\n }\n /** The linear mix from this color to `o`. */\n mix(o: Color, t: number): Color {\n const m = (x: number, y: number): number => x + (y - x) * t;\n return new Color(m(this.r, o.r), m(this.g, o.g), m(this.b, o.b), m(this.a, o.a));\n }\n /** The relative luminance (Rec. 709 weights) of the color channels. */\n luminance(): number {\n return 0.2126 * this.r + 0.7152 * this.g + 0.0722 * this.b;\n }\n /**\n * The relative luminance of WCAG 2.1, from 0 (black) to 1 (white). Each channel from 0 to 255 becomes a linear\n * value with the sRGB transfer function. A channel outside the range counts as the nearest end of the range.\n */\n relativeLuminance(): number {\n return 0.2126 * linear(this.r) + 0.7152 * linear(this.g) + 0.0722 * linear(this.b);\n }\n /** The contrast ratio of WCAG 2.1 between this color and `o`: 1 for equal luminance, 21 for black and white. */\n contrast(o: Color): number {\n const a = this.relativeLuminance();\n const b = o.relativeLuminance();\n return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);\n }\n equals(o: Color): boolean {\n return this.r === o.r && this.g === o.g && this.b === o.b && this.a === o.a;\n }\n toArray(): readonly [number, number, number, number] {\n return [this.r, this.g, this.b, this.a];\n }\n toString(): string {\n return `rgba(${this.r}, ${this.g}, ${this.b}, ${this.a})`;\n }\n}\n\n/** The ops of `ColorDomain`. */\nexport type ColorOps = OpTable<Color, \"add\" | \"multiply\" | \"clamp\" | \"mix\" | \"luminance\" | \"relativeLuminance\" | \"contrast\" | \"equals\">;\n\n/** The domain of colors. */\nexport const ColorDomain: Domain<\"Color\", Color, ColorOps> = defineDomain<\"Color\", Color, ColorOps>({\n name: \"Color\",\n is: (u: unknown): u is Color => u instanceof Color,\n fromScalar: (n: number): Color => Color.gray(n),\n valid: (c: Color): boolean => c.toArray().every((x) => Number.isFinite(x)),\n show: (c: Color): string => c.toString(),\n encode: (c: Color): readonly number[] => c.toArray(),\n decode: (u: unknown): Color => {\n const [r, g, b, a] = (Array.isArray(u) ? u : []) as readonly number[];\n return new Color(r ?? Number.NaN, g ?? Number.NaN, b ?? Number.NaN, a ?? 1);\n },\n ops: {\n add: { laws: [\"commutative\", \"associative\"], identity: (): Color => Color.none, params: [\"domain\"] },\n multiply: { params: [\"number\"] },\n clamp: { params: [\"number\", \"number\"] },\n mix: { params: [\"domain\", \"number\"] },\n luminance: {},\n relativeLuminance: {},\n contrast: { laws: [\"commutative\"], params: [\"domain\"] },\n equals: { laws: [\"commutative\"], params: [\"domain\"] },\n },\n});\n","import { defineDomain, type Domain, type OpTable } from \"@mark1russell7/vex\";\nimport { Vec2 } from \"./vec2.ts\";\n\n/** An immutable angle in radians. */\nexport class Angle {\n readonly radians: number;\n\n constructor(radians: number) {\n this.radians = radians;\n }\n\n /** This function makes an angle from degrees. */\n static fromDegrees(deg: number): Angle {\n return new Angle((deg * Math.PI) / 180);\n }\n /** The zero angle: the identity of `add`. */\n static readonly zero: Angle = new Angle(0);\n\n add(o: Angle): Angle {\n return new Angle(this.radians + o.radians);\n }\n subtract(o: Angle): Angle {\n return new Angle(this.radians - o.radians);\n }\n scale(k: number): Angle {\n return new Angle(this.radians * k);\n }\n /** The same angle in the range from -π (included) to π (not included). */\n normalize(): Angle {\n const twoPi = Math.PI * 2;\n // Stryker disable next-line ArithmeticOperator: x + π and x - π differ by 2π, so the result after mod 2π is the same\n return new Angle((((this.radians + Math.PI) % twoPi) + twoPi) % twoPi - Math.PI);\n }\n /** The vector with this angle and the length `length`. The result is a `Vec2`, so a chain continues in that domain. */\n toVec2(length = 1): Vec2 {\n return new Vec2(Math.cos(this.radians) * length, Math.sin(this.radians) * length);\n }\n sin(): number {\n return Math.sin(this.radians);\n }\n cos(): number {\n return Math.cos(this.radians);\n }\n degrees(): number {\n return (this.radians * 180) / Math.PI;\n }\n toString(): string {\n return `${this.radians} rad`;\n }\n}\n\n/** The ops of `AngleDomain`. */\nexport type AngleOps = OpTable<Angle, \"add\" | \"subtract\" | \"scale\" | \"normalize\" | \"toVec2\" | \"sin\" | \"cos\" | \"degrees\", \"add\" | \"subtract\">;\n\n/** The domain of angles. A number argument of `add` or `subtract` lifts to an angle in radians. */\nexport const AngleDomain: Domain<\"Angle\", Angle, AngleOps> = defineDomain<\"Angle\", Angle, AngleOps>({\n name: \"Angle\",\n is: (u: unknown): u is Angle => u instanceof Angle,\n fromScalar: (n: number): Angle => new Angle(n),\n valid: (a: Angle): boolean => Number.isFinite(a.radians),\n show: (a: Angle): string => a.toString(),\n encode: (a: Angle): number => a.radians,\n decode: (u: unknown): Angle => new Angle(typeof u === \"number\" ? u : Number.NaN),\n ops: {\n add: { laws: [\"commutative\", \"associative\"], identity: (): Angle => Angle.zero, liftScalar: true, params: [\"domain\"] },\n subtract: { liftScalar: true, params: [\"domain\"] },\n scale: { params: [\"number\"] },\n normalize: {},\n toVec2: { params: [\"number\"] },\n sin: {},\n cos: {},\n degrees: {},\n },\n});\n","import { defineDomain, type Domain, type OpTable } from \"@mark1russell7/vex\";\n\n/** A wrapper around a function. Its op `invoke` calls the function. It shows how a domain can hold behavior. */\nexport class Fn {\n readonly fn: (...args: never[]) => unknown;\n\n constructor(fn: (...args: never[]) => unknown) {\n this.fn = fn;\n }\n invoke(...args: readonly unknown[]): unknown {\n return (this.fn as (...a: readonly unknown[]) => unknown)(...args);\n }\n}\n\n/** The ops of `FnDomain`. */\nexport type FnOps = OpTable<Fn, \"invoke\">;\n\n/** The domain of function wrappers. */\nexport const FnDomain: Domain<\"Fn\", Fn, FnOps> = defineDomain<\"Fn\", Fn, FnOps>({\n name: \"Fn\",\n is: (u: unknown): u is Fn => u instanceof Fn,\n ops: { invoke: {} },\n});\n\n/**\n * A value that is there (`top`) or not there (`bottom`). It shows a domain whose ops take functions. A Vex\n * program can also hold this state in its own errors, so most programs do not need this domain.\n */\nexport class Maybe<T> {\n readonly #has: boolean;\n readonly #value: T | undefined;\n\n private constructor(has: boolean, value: T | undefined) {\n this.#has = has;\n this.#value = value;\n }\n static top<T>(value: T): Maybe<T> {\n return new Maybe<T>(true, value);\n }\n static bottom<T = never>(): Maybe<T> {\n return new Maybe<T>(false, undefined);\n }\n isTop(): boolean {\n return this.#has;\n }\n isBottom(): boolean {\n return !this.#has;\n }\n map<U>(f: (value: T) => U): Maybe<U> {\n return this.#has ? Maybe.top(f(this.#value as T)) : Maybe.bottom<U>();\n }\n chain<U>(f: (value: T) => Maybe<U>): Maybe<U> {\n return this.#has ? f(this.#value as T) : Maybe.bottom<U>();\n }\n valueOr<U>(fallback: U): T | U {\n return this.#has ? (this.#value as T) : fallback;\n }\n}\n\n/** The ops of `MaybeDomain`. */\nexport type MaybeOps = OpTable<Maybe<unknown>, \"isTop\" | \"isBottom\" | \"map\" | \"chain\" | \"valueOr\">;\n\n/** The domain of `Maybe` values. */\nexport const MaybeDomain: Domain<\"Maybe\", Maybe<unknown>, MaybeOps> = defineDomain<\"Maybe\", Maybe<unknown>, MaybeOps>({\n name: \"Maybe\",\n is: (u: unknown): u is Maybe<unknown> => u instanceof Maybe,\n ops: { isTop: {}, isBottom: {}, map: {}, chain: {}, valueOr: {} },\n});\n"],"mappings":";;;;;;AAMA,IAAa,OAAb,MAAa,KAAK;CAChB;CACA;CAEA,YAAY,GAAW,GAAW;EAChC,KAAK,IAAI;EACT,KAAK,IAAI;CACX;;CAGA,OAAO,GAAG,GAAW,GAAiB;EACpC,OAAO,IAAI,KAAK,GAAG,CAAC;CACtB;;CAEA,OAAO,OAAO,GAAiB;EAC7B,OAAO,IAAI,KAAK,GAAG,CAAC;CACtB;;CAEA,OAAgB,OAAa,IAAI,KAAK,GAAG,CAAC;;CAE1C,OAAgB,MAAY,IAAI,KAAK,GAAG,CAAC;CAEzC,IAAI,GAAe;EACjB,OAAO,IAAI,KAAK,KAAK,IAAI,EAAE,GAAG,KAAK,IAAI,EAAE,CAAC;CAC5C;CACA,SAAS,GAAe;EACtB,OAAO,IAAI,KAAK,KAAK,IAAI,EAAE,GAAG,KAAK,IAAI,EAAE,CAAC;CAC5C;;CAEA,SAAS,GAAe;EACtB,OAAO,IAAI,KAAK,KAAK,IAAI,EAAE,GAAG,KAAK,IAAI,EAAE,CAAC;CAC5C;;CAEA,OAAO,GAAe;EACpB,OAAO,IAAI,KAAK,KAAK,IAAI,EAAE,GAAG,KAAK,IAAI,EAAE,CAAC;CAC5C;;CAEA,IAAI,GAAe;EACjB,OAAO,IAAI,KAAK,KAAK,IAAI,KAAK,GAAG,EAAE,CAAC,GAAG,KAAK,IAAI,KAAK,GAAG,EAAE,CAAC,CAAC;CAC9D;;CAEA,IA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vex-domains",
4
- "version": "0.3.0",
4
+ "version": "0.4.0",
5
5
  "description": "Vex domains: 2D vectors, named-component vectors, colors, angles, numbers and booleans.",
6
6
  "keywords": [
7
7
  "vex",
@@ -51,7 +51,7 @@
51
51
  "vitest": "^5.0.3"
52
52
  },
53
53
  "dependencies": {
54
- "@mark1russell7/vex": "0.3.0"
54
+ "@mark1russell7/vex": "0.4.0"
55
55
  },
56
56
  "scripts": {
57
57
  "build": "tsdown",
package/src/angle.ts CHANGED
@@ -28,6 +28,7 @@ export class Angle {
28
28
  /** The same angle in the range from -π (included) to π (not included). */
29
29
  normalize(): Angle {
30
30
  const twoPi = Math.PI * 2;
31
+ // Stryker disable next-line ArithmeticOperator: x + π and x - π differ by 2π, so the result after mod 2π is the same
31
32
  return new Angle((((this.radians + Math.PI) % twoPi) + twoPi) % twoPi - Math.PI);
32
33
  }
33
34
  /** The vector with this angle and the length `length`. The result is a `Vec2`, so a chain continues in that domain. */
package/src/color.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  import { defineDomain, type Domain, type OpTable } from "@mark1russell7/vex";
2
2
 
3
+ /** The linear value of an sRGB channel from 0 to 255, with the sRGB transfer function of WCAG 2.1. */
4
+ function linear(channel: number): number {
5
+ const s = Math.min(1, Math.max(0, channel / 255));
6
+ // For integer channels, the sRGB threshold 0.04045 and the WCAG threshold 0.03928 give the same result.
7
+ return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
8
+ }
9
+
3
10
  /**
4
11
  * An immutable RGBA color. The channels have no fixed range: use `clamp` to limit them. `add` adds the color
5
12
  * channels and keeps the larger alpha, so `add` is commutative and associative.
@@ -46,6 +53,19 @@ export class Color {
46
53
  luminance(): number {
47
54
  return 0.2126 * this.r + 0.7152 * this.g + 0.0722 * this.b;
48
55
  }
56
+ /**
57
+ * The relative luminance of WCAG 2.1, from 0 (black) to 1 (white). Each channel from 0 to 255 becomes a linear
58
+ * value with the sRGB transfer function. A channel outside the range counts as the nearest end of the range.
59
+ */
60
+ relativeLuminance(): number {
61
+ return 0.2126 * linear(this.r) + 0.7152 * linear(this.g) + 0.0722 * linear(this.b);
62
+ }
63
+ /** The contrast ratio of WCAG 2.1 between this color and `o`: 1 for equal luminance, 21 for black and white. */
64
+ contrast(o: Color): number {
65
+ const a = this.relativeLuminance();
66
+ const b = o.relativeLuminance();
67
+ return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
68
+ }
49
69
  equals(o: Color): boolean {
50
70
  return this.r === o.r && this.g === o.g && this.b === o.b && this.a === o.a;
51
71
  }
@@ -58,7 +78,7 @@ export class Color {
58
78
  }
59
79
 
60
80
  /** The ops of `ColorDomain`. */
61
- export type ColorOps = OpTable<Color, "add" | "multiply" | "clamp" | "mix" | "luminance" | "equals">;
81
+ export type ColorOps = OpTable<Color, "add" | "multiply" | "clamp" | "mix" | "luminance" | "relativeLuminance" | "contrast" | "equals">;
62
82
 
63
83
  /** The domain of colors. */
64
84
  export const ColorDomain: Domain<"Color", Color, ColorOps> = defineDomain<"Color", Color, ColorOps>({
@@ -78,6 +98,8 @@ export const ColorDomain: Domain<"Color", Color, ColorOps> = defineDomain<"Color
78
98
  clamp: { params: ["number", "number"] },
79
99
  mix: { params: ["domain", "number"] },
80
100
  luminance: {},
101
+ relativeLuminance: {},
102
+ contrast: { laws: ["commutative"], params: ["domain"] },
81
103
  equals: { laws: ["commutative"], params: ["domain"] },
82
104
  },
83
105
  });
package/src/index.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  /**
2
- * Domains for Vex: 2D vectors, named-component vectors, colors, angles, numbers and booleans, and two
2
+ * Domains for Vex: 2D vectors, named-component vectors, colors, angles, numbers, booleans and texts, and two
3
3
  * example domains (function wrappers and `Maybe`).
4
4
  */
5
5
  export * from "./vec2.ts";
6
6
  export * from "./ndvector.ts";
7
7
  export * from "./scalars.ts";
8
+ export * from "./text.ts";
8
9
  export * from "./color.ts";
9
10
  export * from "./angle.ts";
10
11
  export * from "./wrappers.ts";
package/src/ndvector.ts CHANGED
@@ -186,7 +186,8 @@ export type NumRecordOps = {
186
186
  const isNumRecord = (u: unknown): u is NumRecord => {
187
187
  if (typeof u !== "object" || u === null || Array.isArray(u)) return false;
188
188
  const proto: unknown = Object.getPrototypeOf(u);
189
- return (proto === Object.prototype || proto === null) && Object.values(u).every((v) => typeof v === "number" && Number.isFinite(v));
189
+ // Number.isFinite gives false for each value that is not a number.
190
+ return (proto === Object.prototype || proto === null) && Object.values(u).every((v) => Number.isFinite(v));
190
191
  };
191
192
 
192
193
  /** The domain of plain number records. Its op `toNDVector` builds a vector from fields, for example from `rec()`. */
package/src/text.ts ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The domain of texts. The op `concat` joins a text and a value of any kind. It is the operator `&` of a
3
+ * spreadsheet, and of the formula language of the sheet app.
4
+ */
5
+ import { defineDomain, type Domain, type FnOp } from "@mark1russell7/vex";
6
+
7
+ type TextFn1 = (a: string) => string;
8
+
9
+ /** The ops of `TextDomain`. */
10
+ export type TextOps = {
11
+ readonly concat: FnOp<string, (a: string, b: unknown) => string>;
12
+ readonly length: FnOp<string, (a: string) => number>;
13
+ readonly upper: FnOp<string, TextFn1>;
14
+ readonly lower: FnOp<string, TextFn1>;
15
+ readonly trim: FnOp<string, TextFn1>;
16
+ readonly contains: FnOp<string, (a: string, b: string) => boolean>;
17
+ readonly eq: FnOp<string, (a: string, b: string) => boolean>;
18
+ };
19
+
20
+ /**
21
+ * This function gives the text of a value for `concat`. A text stays. A plain record gives its JSON text. Another
22
+ * value, for example a number or a `Vec2`, gives its own text.
23
+ */
24
+ const textOf = (u: unknown): string => (typeof u === "object" && u !== null && Object.getPrototypeOf(u) === Object.prototype ? JSON.stringify(u) : String(u));
25
+
26
+ /** The domain of texts. */
27
+ export const TextDomain: Domain<"Text", string, TextOps> = defineDomain<"Text", string, TextOps>({
28
+ name: "Text",
29
+ is: (u: unknown): u is string => typeof u === "string",
30
+ show: (s: string): string => JSON.stringify(s),
31
+ ops: {
32
+ concat: { fn: (a: string, b: unknown): string => a + textOf(b), params: ["any"] },
33
+ // The length counts UTF-16 code units, like the function LEN of a spreadsheet.
34
+ length: { fn: (a: string): number => a.length },
35
+ upper: { fn: (a: string): string => a.toUpperCase() },
36
+ lower: { fn: (a: string): string => a.toLowerCase() },
37
+ trim: { fn: (a: string): string => a.trim() },
38
+ contains: { fn: (a: string, b: string): boolean => a.includes(b), params: ["string"] },
39
+ eq: { fn: (a: string, b: string): boolean => a === b, laws: ["commutative"], params: ["string"] },
40
+ },
41
+ });
package/src/vec2.ts CHANGED
@@ -184,6 +184,7 @@ export const Vec2Domain: Domain<"Vec2", Vec2, Vec2Ops> = defineDomain<"Vec2", Ve
184
184
  show: (v: Vec2): string => v.toString(),
185
185
  encode: (v: Vec2): readonly [number, number] => [v.x, v.y],
186
186
  decode: (u: unknown): Vec2 => {
187
+ // Stryker disable next-line ArrayDeclaration: a list with a text gives NaN components, like the empty list
187
188
  const [x, y] = Array.isArray(u) ? (u as readonly unknown[]) : [];
188
189
  return new Vec2(typeof x === "number" ? x : Number.NaN, typeof y === "number" ? y : Number.NaN);
189
190
  },