onejs-core 1.0.8 → 1.0.11

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.
@@ -0,0 +1,3 @@
1
+ export { DocumentWrapper } from './document';
2
+ export { DomWrapper } from './dom';
3
+ export { DomStyleWrapper } from './dom-style';
@@ -0,0 +1,3 @@
1
+ export { DocumentWrapper } from './document';
2
+ export { DomWrapper } from './dom';
3
+ export { DomStyleWrapper } from './dom-style';
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference path="../definitions/index.d.ts" />
1
2
  import { DocumentWrapper } from "./dom/document";
2
3
  import { DomWrapper } from "./dom/dom";
3
4
  /**
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference path="./definitions/index.d.ts" />
1
2
  import { DocumentWrapper } from "./dom/document";
2
3
  /**
3
4
  * OneJS's own h function. Use this to quickly create elements in jsx-like syntax
@@ -0,0 +1,55 @@
1
+ import { DomWrapper } from "./dom"
2
+
3
+ interface ElementCreationOptions {
4
+ is?: string
5
+ }
6
+
7
+ export class DocumentWrapper {
8
+ public get _doc(): CS.OneJS.Dom.Document { return this.#doc }
9
+
10
+ #doc: CS.OneJS.Dom.Document;
11
+ #body: DomWrapper | null;
12
+
13
+ /**
14
+ * The body/root element of the document. Will be null for Editor documents.
15
+ */
16
+ public get body(): DomWrapper | null { return this.#body }
17
+
18
+ constructor(doc: CS.OneJS.Dom.Document) {
19
+ this.#doc = doc
20
+ this.#body = doc.body ? new DomWrapper(doc.body) : null
21
+ }
22
+
23
+ addRuntimeUSS(uss: string): void {
24
+ this.#doc.addRuntimeUSS(uss)
25
+ }
26
+
27
+ clearRuntimeStyleSheets(): void {
28
+ this.#doc.clearRuntimeStyleSheets()
29
+ }
30
+
31
+ createElement(tagName: string, options?: ElementCreationOptions): DomWrapper {
32
+ return new DomWrapper(this.#doc.createElement(tagName))
33
+ }
34
+
35
+ createElementNS(ns: string, tagName: string, options?: ElementCreationOptions): DomWrapper {
36
+ return new DomWrapper(this.#doc.createElement(tagName))
37
+ }
38
+
39
+ createTextNode(text: string): DomWrapper {
40
+ return new DomWrapper(this.#doc.createTextNode(text))
41
+ }
42
+
43
+ getElementById(id: string): DomWrapper {
44
+ return new DomWrapper(this.#doc.getElementById(id))
45
+ }
46
+
47
+ querySelectorAll(selector: string): DomWrapper[] {
48
+ let doms = this.#doc.querySelectorAll(selector)
49
+ let res = [] as any[]
50
+ for (let i = 0; i < doms.Length; i++) {
51
+ res.push(new DomWrapper(doms.get_Item(i)))
52
+ }
53
+ return res;
54
+ }
55
+ }
@@ -0,0 +1,31 @@
1
+
2
+
3
+ export class DomStyleWrapper implements CS.OneJS.Dom.DomStyle {
4
+ _domStyle: CS.OneJS.Dom.DomStyle // Making this public so that Proxy's setProperty can access it
5
+
6
+ constructor(domStyle: CS.OneJS.Dom.DomStyle) {
7
+ this._domStyle = domStyle
8
+
9
+ return new Proxy(this, {
10
+ set(target, prop, value) {
11
+ target.setProperty(prop as string, value);
12
+ return true;
13
+ },
14
+ get(target, prop) {
15
+ return target.getProperty(prop as string);
16
+ }
17
+ })
18
+ }
19
+
20
+ getProperty(name: string) {
21
+ return this._domStyle.getProperty(name)
22
+ }
23
+
24
+ setProperty(name: string, value: any) {
25
+ this._domStyle.setProperty(name, value)
26
+ }
27
+ }
28
+
29
+ export interface DomStyleWrapper extends CS.OneJS.Dom.DomStyle {
30
+ [key: string | symbol]: any;
31
+ }
package/dom/dom.ts ADDED
@@ -0,0 +1,267 @@
1
+ import { EventBase } from "UnityEngine/UIElements";
2
+ import { DomStyleWrapper } from "./dom-style"
3
+
4
+ export class DomWrapper {
5
+ public get _dom(): CS.OneJS.Dom.Dom { return this.dom }
6
+ public get ve(): CS.UnityEngine.UIElements.VisualElement { return this.dom.ve }
7
+ public get childNodes(): DomWrapper[] {
8
+ if (this.cachedChildNodes) return this.cachedChildNodes
9
+ this.cachedChildNodes = new Array(this.dom.childNodes.Length) as DomWrapper[];
10
+ var i = this.dom.childNodes.Length;
11
+ while (i--) {
12
+ this.cachedChildNodes[i] = new DomWrapper(this.dom.childNodes.get_Item(i));
13
+ }
14
+ return this.cachedChildNodes
15
+ }
16
+ public get firstChild(): DomWrapper | null {
17
+ return this.dom.firstChild ? new DomWrapper(this.dom.firstChild) : null;
18
+ }
19
+ public get parentNode(): DomWrapper | null {
20
+ return this.dom.parentNode ? new DomWrapper(this.dom.parentNode) : null;
21
+ }
22
+ public get nextSibling(): DomWrapper | null {
23
+ return this.dom.nextSibling ? new DomWrapper(this.dom.nextSibling) : null;
24
+ }
25
+
26
+ public get nodeType(): number { return this.dom.nodeType }
27
+ public get style(): DomStyleWrapper { return this.domStyleWrapper }
28
+ public get Id(): string { return this.dom.Id }
29
+ public set Id(value: string) { this.dom.Id = value }
30
+ public get key(): string { return this.dom.key }
31
+ public set key(value: string) { this.dom.key = value }
32
+ public get value(): any { return this.dom.value }
33
+ public get checked(): boolean { return this.dom.checked }
34
+ public get data(): any { return this.dom.data }
35
+ public set data(value: any) { this.dom.data = value }
36
+
37
+ public get classname(): string { return this.dom.classname }
38
+ public set classname(value: string) { this.dom.classname = value }
39
+
40
+ /**
41
+ * Not using private fields because of issues with the `#private;` line
42
+ * generated by tsc
43
+ */
44
+ dom: CS.OneJS.Dom.Dom
45
+ domStyleWrapper: DomStyleWrapper
46
+
47
+ cachedChildNodes: DomWrapper[] | null = null
48
+ boundListeners = new WeakMap();
49
+
50
+ constructor(dom: CS.OneJS.Dom.Dom) {
51
+ this.dom = dom
52
+ this.domStyleWrapper = new DomStyleWrapper(dom.style)
53
+ }
54
+
55
+ appendChild(child: DomWrapper) {
56
+ if (!child) return
57
+ this.dom.appendChild(child.dom)
58
+ this.cachedChildNodes = null
59
+ }
60
+
61
+ removeChild(child: DomWrapper) {
62
+ if (!child) return
63
+ this.dom.removeChild(child.dom)
64
+ this.cachedChildNodes = null
65
+ }
66
+
67
+ insertBefore(a: DomWrapper, b: DomWrapper) {
68
+ this.dom.insertBefore(a?._dom, b?._dom)
69
+ this.cachedChildNodes = null
70
+ }
71
+
72
+ insertAfter(a: DomWrapper, b: DomWrapper) {
73
+ this.dom.insertAfter(a?._dom, b?._dom)
74
+ this.cachedChildNodes = null
75
+ }
76
+
77
+ contains(child: DomWrapper) {
78
+ if (!child) return false
79
+ return this.dom.contains(child._dom)
80
+ }
81
+
82
+ clearChildren() {
83
+ this.dom.clearChildren()
84
+ this.cachedChildNodes = null
85
+ }
86
+
87
+ focus() {
88
+ this.dom.focus()
89
+ }
90
+
91
+ addEventListener(type: string, listener: (event: EventBase) => void, useCapture?: boolean) {
92
+ let boundListener = this.boundListeners.get(listener);
93
+ if (!boundListener) {
94
+ boundListener = listener.bind(this);
95
+ this.boundListeners.set(listener, boundListener);
96
+ }
97
+ this.dom.addEventListener(type, boundListener, useCapture ? true : false)
98
+ }
99
+
100
+ removeEventListener(type: string, listener: (event: EventBase) => void, useCapture?: boolean) {
101
+ const boundListener = this.boundListeners.get(listener);
102
+ if (boundListener) {
103
+ this.dom.removeEventListener(type, boundListener, useCapture ? true : false)
104
+ this.boundListeners.delete(listener); // isn't strictly necessary for WeakMap, but still good practice
105
+ }
106
+ }
107
+
108
+ setAttribute(name: string, value: any) {
109
+ this.dom.setAttribute(name, value)
110
+ }
111
+
112
+ removeAttribute(name: string) {
113
+ this.dom.removeAttribute(name)
114
+ }
115
+
116
+ /**
117
+ * Returns all elements matching the specified selector.
118
+ * Supports basic selectors:
119
+ * - Tag names: 'div'
120
+ * - IDs: '#myId'
121
+ * - Classes: '.myClass'
122
+ * - Combinations: 'div.myClass#myId'
123
+ */
124
+ querySelectorAll(selector: string): DomWrapper[] {
125
+ const selectorInfo = parseSelector(selector);
126
+ const results: DomWrapper[] = [];
127
+
128
+ function traverse(element: DomWrapper) {
129
+ if (elementMatchesSelector(element, selectorInfo)) {
130
+ results.push(element);
131
+ }
132
+
133
+ for (const child of element.childNodes) {
134
+ traverse(child);
135
+ }
136
+ }
137
+
138
+ traverse(this);
139
+ return results;
140
+ }
141
+
142
+ /**
143
+ * Returns the first element matching the specified selector.
144
+ * Supports the same basic selectors as querySelectorAll.
145
+ */
146
+ querySelector(selector: string): DomWrapper | null {
147
+ const selectorInfo = parseSelector(selector);
148
+
149
+ function traverse(element: DomWrapper): DomWrapper | null {
150
+ if (elementMatchesSelector(element, selectorInfo)) {
151
+ return element;
152
+ }
153
+
154
+ for (const child of element.childNodes) {
155
+ const match = traverse(child);
156
+ if (match) {
157
+ return match;
158
+ }
159
+ }
160
+
161
+ return null;
162
+ }
163
+
164
+ return traverse(this);
165
+ }
166
+ }
167
+
168
+ interface SelectorInfo {
169
+ tag?: string;
170
+ id?: string;
171
+ classes: string[];
172
+ }
173
+
174
+ function parseSelector(selector: string): SelectorInfo {
175
+ const selectorInfo: SelectorInfo = {
176
+ classes: []
177
+ };
178
+
179
+ // Handle ID
180
+ const idMatch = selector.match(/#([^.#\s]+)/);
181
+ if (idMatch) {
182
+ selectorInfo.id = idMatch[1];
183
+ selector = selector.replace(idMatch[0], '');
184
+ }
185
+
186
+ // Handle classes
187
+ const classMatches = selector.match(/\.([^.#\s]+)/g);
188
+ if (classMatches) {
189
+ selectorInfo.classes = classMatches.map(c => c.substring(1));
190
+ selector = selector.replace(/\.[^.#\s]+/g, '');
191
+ }
192
+
193
+ // Handle tag name (what's left after removing id and classes)
194
+ const tagName = selector.trim();
195
+ if (tagName) {
196
+ selectorInfo.tag = tagName.toLowerCase();
197
+ }
198
+
199
+ return selectorInfo;
200
+ }
201
+
202
+ function elementMatchesSelector(element: DomWrapper, selectorInfo: SelectorInfo): boolean {
203
+ // Check tag name
204
+ if (selectorInfo.tag && element.ve.name.toLowerCase() !== selectorInfo.tag) {
205
+ return false;
206
+ }
207
+
208
+ // Check ID
209
+ if (selectorInfo.id && element.Id !== selectorInfo.id) {
210
+ return false;
211
+ }
212
+
213
+ // Check classes
214
+ if (selectorInfo.classes.length > 0) {
215
+ const elementClasses = element.classname.split(' ').filter(c => c);
216
+ for (const className of selectorInfo.classes) {
217
+ if (!elementClasses.includes(className)) {
218
+ return false;
219
+ }
220
+ }
221
+ }
222
+
223
+ return true;
224
+ }
225
+
226
+ export function querySelectorAll(root: DomWrapper, selector: string): DomWrapper[] {
227
+ const results: DomWrapper[] = [];
228
+ const selectorInfo = parseSelector(selector);
229
+
230
+ function traverse(element: DomWrapper) {
231
+ // Check if current element matches
232
+ if (elementMatchesSelector(element, selectorInfo)) {
233
+ results.push(element);
234
+ }
235
+
236
+ // Recursively check children
237
+ for (const child of element.childNodes) {
238
+ traverse(child);
239
+ }
240
+ }
241
+
242
+ traverse(root);
243
+ return results;
244
+ }
245
+
246
+ export function querySelector(root: DomWrapper, selector: string): DomWrapper | null {
247
+ const selectorInfo = parseSelector(selector);
248
+
249
+ function traverse(element: DomWrapper): DomWrapper | null {
250
+ // Check if current element matches
251
+ if (elementMatchesSelector(element, selectorInfo)) {
252
+ return element;
253
+ }
254
+
255
+ // Recursively check children
256
+ for (const child of element.childNodes) {
257
+ const match = traverse(child);
258
+ if (match) {
259
+ return match;
260
+ }
261
+ }
262
+
263
+ return null;
264
+ }
265
+
266
+ return traverse(root);
267
+ }
package/dom/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { DocumentWrapper } from './document';
2
+ export { DomWrapper } from './dom';
3
+ export { DomStyleWrapper } from './dom-style';
package/index.ts ADDED
@@ -0,0 +1,53 @@
1
+ /// <reference path="./definitions/index.d.ts" />
2
+ import { DocumentWrapper } from "./dom/document";
3
+ import { DomWrapper } from "./dom/dom";
4
+
5
+ /**
6
+ * OneJS's own h function. Use this to quickly create elements in jsx-like syntax
7
+ * @param type
8
+ * @param props
9
+ * @param children
10
+ * @returns
11
+ */
12
+ export function h(type: any, props: any, ...children: any[]): any {
13
+ const element = typeof type === "string" ? document.createElement(type) : type;
14
+
15
+ // Assign properties to the element
16
+ for (const [key, value] of Object.entries(props || {})) {
17
+ if (key.startsWith("on") && typeof value === "function") {
18
+ element.addEventListener(key.substring(2).toLowerCase(), value);
19
+ } else if (key === "style" && typeof value === "object") {
20
+ Object.assign(element.style, value);
21
+ } else {
22
+ element.setAttribute(key, value);
23
+ }
24
+ }
25
+
26
+ // Append children
27
+ for (const child of children) {
28
+ if (typeof child === "string") {
29
+ element.appendChild(document.createTextNode(child));
30
+ } else {
31
+ element.appendChild(child);
32
+ }
33
+ }
34
+
35
+ return element;
36
+ }
37
+
38
+ export { emo } from "./styling/index"
39
+
40
+ declare global {
41
+ interface Document extends DocumentWrapper { }
42
+ interface Element extends DomWrapper {
43
+ classname: string
44
+ nodeType: number
45
+ ve: CS.UnityEngine.UIElements.VisualElement
46
+ }
47
+ }
48
+
49
+ // @ts-ignore
50
+ if (typeof ___document != "undefined") {
51
+ // @ts-ignore
52
+ globalThis.document = new DocumentWrapper(___document)
53
+ }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "onejs-core",
3
3
  "description": "The JS part of OneJS, a UI framework and Scripting Engine for Unity.",
4
- "version": "1.0.8",
4
+ "version": "1.0.11",
5
5
  "main": "./dist/index.js",
6
- "types": "./typings.d.ts",
6
+ "types": "./dist/index.d.ts",
7
7
  "dependencies": {
8
8
  "css-flatten": "^2.0.0",
9
9
  "css-simple-parser": "^3.0.0"
@@ -0,0 +1,44 @@
1
+
2
+
3
+ export function btoa(text: string): string {
4
+ // return CS.OneJS.CommonGlobals.btoa(text);
5
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
6
+ let str = text;
7
+ let output = '';
8
+
9
+ for (let i = 0; i < str.length; i += 3) {
10
+ let block = (str.charCodeAt(i) << 16) | ((i + 1 < str.length ? str.charCodeAt(i + 1) : 0) << 8) | (i + 2 < str.length ? str.charCodeAt(i + 2) : 0);
11
+ output += chars[(block >> 18) & 0x3F] +
12
+ chars[(block >> 12) & 0x3F] +
13
+ (i + 1 < str.length ? chars[(block >> 6) & 0x3F] : '=') +
14
+ (i + 2 < str.length ? chars[block & 0x3F] : '=');
15
+ }
16
+
17
+ return output;
18
+ }
19
+
20
+ // function uint8ArrayToString(uint8Array) {
21
+ // const chars: any = [];
22
+ // for (let i = 0; i < uint8Array.length; i++) {
23
+ // chars.push(String.fromCharCode(uint8Array[i]));
24
+ // }
25
+ // return chars.join('');
26
+ // }
27
+
28
+ export function atob(base64: string): string {
29
+ // return CS.OneJS.CommonGlobals.atob(base64);
30
+ // return CS.System.Text.Encoding.UTF8.GetString(CS.System.Convert.FromBase64String(base64))
31
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
32
+ let str = base64.replace(/=+$/, '');
33
+ let output = '';
34
+
35
+ if (str.length % 4 == 1) {
36
+ throw new Error('Invalid base64 string');
37
+ }
38
+
39
+ for (let bc = 0, bs = 0, buffer, idx = 0; buffer = str.charAt(idx++); ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer, bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0) {
40
+ buffer = chars.indexOf(buffer);
41
+ }
42
+
43
+ return output;
44
+ }
@@ -0,0 +1,32 @@
1
+ import flatten from "css-flatten"
2
+ import generateComponentId from "./utils/generateComponentId"
3
+
4
+ const _pastCompIds = new Set<string>()
5
+
6
+ export function hashAndAddRuntimeUSS(style: string) {
7
+ let compId = generateComponentId(style)
8
+ if (_pastCompIds.has(compId))
9
+ return compId
10
+ style = `.${compId} {${style}}`
11
+ style = flatten(style)
12
+ document.addRuntimeUSS(style)
13
+ _pastCompIds.add(compId)
14
+
15
+ return compId
16
+ }
17
+
18
+ /**
19
+ * Similar to the Emotion api, this function takes a template string and returns
20
+ * a class name that can be used to style an element.
21
+ * @param strings
22
+ * @param values
23
+ * @returns
24
+ */
25
+ export const emo = function (strings: TemplateStringsArray, ...values: any[]): string {
26
+ let style = values.reduce((result, expr, index) => {
27
+ const value = expr
28
+ return result + (value ? value : "") + strings[index + 1]
29
+ }, strings[0])
30
+
31
+ return hashAndAddRuntimeUSS(style)
32
+ }
@@ -0,0 +1,21 @@
1
+ const AD_REPLACER_R = /(a)(d)/gi
2
+
3
+ /* This is the "capacity" of our alphabet i.e. 2x26 for all letters plus their capitalised
4
+ * counterparts */
5
+ const charsLength = 52
6
+
7
+ /* start at 75 for 'a' until 'z' (25) and then start at 65 for capitalised letters */
8
+ const getAlphabeticChar = (code: number) => String.fromCharCode(code + (code > 25 ? 39 : 97));
9
+
10
+ /* input a number, usually a hash and convert it to base-52 */
11
+ export default function generateAlphabeticName(code: number) {
12
+ let name = ''
13
+ let x
14
+
15
+ /* get a char and divide by alphabet-length */
16
+ for (x = Math.abs(code); x > charsLength; x = (x / charsLength) | 0) {
17
+ name = getAlphabeticChar(x % charsLength) + name
18
+ }
19
+
20
+ return (getAlphabeticChar(x % charsLength) + name).replace(AD_REPLACER_R, '$1-$2')
21
+ }
@@ -0,0 +1,6 @@
1
+ import generateAlphabeticName from './generateAlphabeticName'
2
+ import { hash } from './hash'
3
+
4
+ export default function generateComponentId(str: string) {
5
+ return generateAlphabeticName(hash(str) >>> 0)
6
+ }
@@ -0,0 +1,46 @@
1
+ export const SEED = 5381;
2
+
3
+ // When we have separate strings it's useful to run a progressive
4
+ // version of djb2 where we pretend that we're still looping over
5
+ // the same string
6
+ export const phash = (h: number, x: string) => {
7
+ let i = x.length;
8
+
9
+ while (i) {
10
+ h = (h * 33) ^ x.charCodeAt(--i);
11
+ }
12
+
13
+ return h;
14
+ };
15
+
16
+ // This is a djb2 hashing function
17
+ export const hash = (x: string) => {
18
+ return phash(SEED, x);
19
+ };
20
+
21
+ const AD_REPLACER_R = /(a)(d)/gi
22
+
23
+ /* This is the "capacity" of our alphabet i.e. 2x26 for all letters plus their capitalised
24
+ * counterparts */
25
+ const charsLength = 52
26
+
27
+ /* start at 75 for 'a' until 'z' (25) and then start at 65 for capitalised letters */
28
+ const getAlphabeticChar = (code: number) => String.fromCharCode(code + (code > 25 ? 39 : 97));
29
+
30
+ /* input a number, usually a hash and convert it to base-52 */
31
+ export function generateAlphabeticName(code: number) {
32
+ let name = ''
33
+ let x
34
+
35
+ /* get a char and divide by alphabet-length */
36
+ for (x = Math.abs(code); x > charsLength; x = (x / charsLength) | 0) {
37
+ name = getAlphabeticChar(x % charsLength) + name
38
+ }
39
+
40
+ return (getAlphabeticChar(x % charsLength) + name).replace(AD_REPLACER_R, '$1-$2')
41
+ }
42
+
43
+ export default function generateComponentId(str: string) {
44
+ return generateAlphabeticName(hash(str) >>> 0)
45
+ }
46
+
package/tsconfig.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "skipLibCheck": false,
12
12
  "forceConsistentCasingInFileNames": true,
13
13
  "declaration": true,
14
- "typeRoots": [ "./definitions/**/*" ],
14
+ "typeRoots": [ "./definitions" ],
15
15
  },
16
16
  "include": [
17
17
  "./**/*"
package/typings.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ // This typing file is not used anymore
2
+
1
3
  /// <reference path="./definitions/index.d.ts" />
2
4
  /// <reference path="./dist/index.d.ts" />
3
5
 
@@ -0,0 +1,3 @@
1
+ let palettes = [["#69d2e7", "#a7dbd8", "#e0e4cc", "#f38630", "#fa6900"], ["#fe4365", "#fc9d9a", "#f9cdad", "#c8c8a9", "#83af9b"], ["#ecd078", "#d95b43", "#c02942", "#542437", "#53777a"], ["#556270", "#4ecdc4", "#c7f464", "#ff6b6b", "#c44d58"], ["#774f38", "#e08e79", "#f1d4af", "#ece5ce", "#c5e0dc"], ["#e8ddcb", "#cdb380", "#036564", "#033649", "#031634"], ["#490a3d", "#bd1550", "#e97f02", "#f8ca00", "#8a9b0f"], ["#594f4f", "#547980", "#45ada8", "#9de0ad", "#e5fcc2"], ["#00a0b0", "#6a4a3c", "#cc333f", "#eb6841", "#edc951"], ["#e94e77", "#d68189", "#c6a49a", "#c6e5d9", "#f4ead5"], ["#3fb8af", "#7fc7af", "#dad8a7", "#ff9e9d", "#ff3d7f"], ["#d9ceb2", "#948c75", "#d5ded9", "#7a6a53", "#99b2b7"], ["#ffffff", "#cbe86b", "#f2e9e1", "#1c140d", "#cbe86b"], ["#efffcd", "#dce9be", "#555152", "#2e2633", "#99173c"], ["#343838", "#005f6b", "#008c9e", "#00b4cc", "#00dffc"], ["#413e4a", "#73626e", "#b38184", "#f0b49e", "#f7e4be"], ["#ff4e50", "#fc913a", "#f9d423", "#ede574", "#e1f5c4"], ["#99b898", "#fecea8", "#ff847c", "#e84a5f", "#2a363b"], ["#655643", "#80bca3", "#f6f7bd", "#e6ac27", "#bf4d28"], ["#00a8c6", "#40c0cb", "#f9f2e7", "#aee239", "#8fbe00"], ["#351330", "#424254", "#64908a", "#e8caa4", "#cc2a41"], ["#554236", "#f77825", "#d3ce3d", "#f1efa5", "#60b99a"], ["#5d4157", "#838689", "#a8caba", "#cad7b2", "#ebe3aa"], ["#8c2318", "#5e8c6a", "#88a65e", "#bfb35a", "#f2c45a"], ["#fad089", "#ff9c5b", "#f5634a", "#ed303c", "#3b8183"], ["#ff4242", "#f4fad2", "#d4ee5e", "#e1edb9", "#f0f2eb"], ["#f8b195", "#f67280", "#c06c84", "#6c5b7b", "#355c7d"], ["#d1e751", "#ffffff", "#000000", "#4dbce9", "#26ade4"], ["#1b676b", "#519548", "#88c425", "#bef202", "#eafde6"], ["#5e412f", "#fcebb6", "#78c0a8", "#f07818", "#f0a830"], ["#bcbdac", "#cfbe27", "#f27435", "#f02475", "#3b2d38"], ["#452632", "#91204d", "#e4844a", "#e8bf56", "#e2f7ce"], ["#eee6ab", "#c5bc8e", "#696758", "#45484b", "#36393b"], ["#f0d8a8", "#3d1c00", "#86b8b1", "#f2d694", "#fa2a00"], ["#2a044a", "#0b2e59", "#0d6759", "#7ab317", "#a0c55f"], ["#f04155", "#ff823a", "#f2f26f", "#fff7bd", "#95cfb7"], ["#b9d7d9", "#668284", "#2a2829", "#493736", "#7b3b3b"], ["#bbbb88", "#ccc68d", "#eedd99", "#eec290", "#eeaa88"], ["#b3cc57", "#ecf081", "#ffbe40", "#ef746f", "#ab3e5b"], ["#a3a948", "#edb92e", "#f85931", "#ce1836", "#009989"], ["#300030", "#480048", "#601848", "#c04848", "#f07241"], ["#67917a", "#170409", "#b8af03", "#ccbf82", "#e33258"], ["#aab3ab", "#c4cbb7", "#ebefc9", "#eee0b7", "#e8caaf"], ["#e8d5b7", "#0e2430", "#fc3a51", "#f5b349", "#e8d5b9"], ["#ab526b", "#bca297", "#c5ceae", "#f0e2a4", "#f4ebc3"], ["#607848", "#789048", "#c0d860", "#f0f0d8", "#604848"], ["#b6d8c0", "#c8d9bf", "#dadabd", "#ecdbbc", "#fedcba"], ["#a8e6ce", "#dcedc2", "#ffd3b5", "#ffaaa6", "#ff8c94"], ["#3e4147", "#fffedf", "#dfba69", "#5a2e2e", "#2a2c31"], ["#fc354c", "#29221f", "#13747d", "#0abfbc", "#fcf7c5"], ["#cc0c39", "#e6781e", "#c8cf02", "#f8fcc1", "#1693a7"], ["#1c2130", "#028f76", "#b3e099", "#ffeaad", "#d14334"], ["#a7c5bd", "#e5ddcb", "#eb7b59", "#cf4647", "#524656"], ["#dad6ca", "#1bb0ce", "#4f8699", "#6a5e72", "#563444"], ["#5c323e", "#a82743", "#e15e32", "#c0d23e", "#e5f04c"], ["#edebe6", "#d6e1c7", "#94c7b6", "#403b33", "#d3643b"], ["#fdf1cc", "#c6d6b8", "#987f69", "#e3ad40", "#fcd036"], ["#230f2b", "#f21d41", "#ebebbc", "#bce3c5", "#82b3ae"], ["#b9d3b0", "#81bda4", "#b28774", "#f88f79", "#f6aa93"], ["#3a111c", "#574951", "#83988e", "#bcdea5", "#e6f9bc"], ["#5e3929", "#cd8c52", "#b7d1a3", "#dee8be", "#fcf7d3"], ["#1c0113", "#6b0103", "#a30006", "#c21a01", "#f03c02"], ["#000000", "#9f111b", "#b11623", "#292c37", "#cccccc"], ["#382f32", "#ffeaf2", "#fcd9e5", "#fbc5d8", "#f1396d"], ["#e3dfba", "#c8d6bf", "#93ccc6", "#6cbdb5", "#1a1f1e"], ["#f6f6f6", "#e8e8e8", "#333333", "#990100", "#b90504"], ["#1b325f", "#9cc4e4", "#e9f2f9", "#3a89c9", "#f26c4f"], ["#a1dbb2", "#fee5ad", "#faca66", "#f7a541", "#f45d4c"], ["#c1b398", "#605951", "#fbeec2", "#61a6ab", "#accec0"], ["#5e9fa3", "#dcd1b4", "#fab87f", "#f87e7b", "#b05574"], ["#951f2b", "#f5f4d7", "#e0dfb1", "#a5a36c", "#535233"], ["#8dccad", "#988864", "#fea6a2", "#f9d6ac", "#ffe9af"], ["#2d2d29", "#215a6d", "#3ca2a2", "#92c7a3", "#dfece6"], ["#413d3d", "#040004", "#c8ff00", "#fa023c", "#4b000f"], ["#eff3cd", "#b2d5ba", "#61ada0", "#248f8d", "#605063"], ["#ffefd3", "#fffee4", "#d0ecea", "#9fd6d2", "#8b7a5e"], ["#cfffdd", "#b4dec1", "#5c5863", "#a85163", "#ff1f4c"], ["#9dc9ac", "#fffec7", "#f56218", "#ff9d2e", "#919167"], ["#4e395d", "#827085", "#8ebe94", "#ccfc8e", "#dc5b3e"], ["#a8a7a7", "#cc527a", "#e8175d", "#474747", "#363636"], ["#f8edd1", "#d88a8a", "#474843", "#9d9d93", "#c5cfc6"], ["#046d8b", "#309292", "#2fb8ac", "#93a42a", "#ecbe13"], ["#f38a8a", "#55443d", "#a0cab5", "#cde9ca", "#f1edd0"], ["#a70267", "#f10c49", "#fb6b41", "#f6d86b", "#339194"], ["#ff003c", "#ff8a00", "#fabe28", "#88c100", "#00c176"], ["#ffedbf", "#f7803c", "#f54828", "#2e0d23", "#f8e4c1"], ["#4e4d4a", "#353432", "#94ba65", "#2790b0", "#2b4e72"], ["#0ca5b0", "#4e3f30", "#fefeeb", "#f8f4e4", "#a5b3aa"], ["#4d3b3b", "#de6262", "#ffb88c", "#ffd0b3", "#f5e0d3"], ["#fffbb7", "#a6f6af", "#66b6ab", "#5b7c8d", "#4f2958"], ["#edf6ee", "#d1c089", "#b3204d", "#412e28", "#151101"], ["#9d7e79", "#ccac95", "#9a947c", "#748b83", "#5b756c"], ["#fcfef5", "#e9ffe1", "#cdcfb7", "#d6e6c3", "#fafbe3"], ["#9cddc8", "#bfd8ad", "#ddd9ab", "#f7af63", "#633d2e"], ["#30261c", "#403831", "#36544f", "#1f5f61", "#0b8185"], ["#aaff00", "#ffaa00", "#ff00aa", "#aa00ff", "#00aaff"], ["#d1313d", "#e5625c", "#f9bf76", "#8eb2c5", "#615375"], ["#ffe181", "#eee9e5", "#fad3b2", "#ffba7f", "#ff9c97"], ["#73c8a9", "#dee1b6", "#e1b866", "#bd5532", "#373b44"], ["#805841", "#dcf7f3", "#fffcdd", "#ffd8d8", "#f5a2a2"]]
2
+
3
+ export { palettes }
@@ -0,0 +1,249 @@
1
+ // import { float4, float3, quaternion } from "Unity/Mathematics" // JSR prepends a "./" to this, which messes things up
2
+ // import { Color } from "UnityEngine" // TODO Somehow this bypasses unity-import-transform and leads to esbuild error
3
+
4
+ type float4 = CS.Unity.Mathematics.float4
5
+ const { float4: f4, float3: f3, PI } = CS.Unity.Mathematics.math
6
+ const { Color } = CS.UnityEngine
7
+
8
+ /*
9
+ https://github.com/deanm/css-color-parser-js
10
+
11
+ Minor changes were made for TS compatibility. Original License below:
12
+
13
+ (c) Dean McNamee <dean@gmail.com>, 2012.
14
+
15
+ Permission is hereby granted, free of charge, to any person obtaining a copy
16
+ of this software and associated documentation files (the "Software"), to
17
+ deal in the Software without restriction, including without limitation the
18
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
19
+ sell copies of the Software, and to permit persons to whom the Software is
20
+ furnished to do so, subject to the following conditions:
21
+
22
+ The above copyright notice and this permission notice shall be included in
23
+ all copies or substantial portions of the Software.
24
+
25
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31
+ IN THE SOFTWARE.
32
+ */
33
+
34
+ var kCSSColorTable = {
35
+ "transparent": [0, 0, 0, 0], "aliceblue": [240, 248, 255, 1],
36
+ "antiquewhite": [250, 235, 215, 1], "aqua": [0, 255, 255, 1],
37
+ "aquamarine": [127, 255, 212, 1], "azure": [240, 255, 255, 1],
38
+ "beige": [245, 245, 220, 1], "bisque": [255, 228, 196, 1],
39
+ "black": [0, 0, 0, 1], "blanchedalmond": [255, 235, 205, 1],
40
+ "blue": [0, 0, 255, 1], "blueviolet": [138, 43, 226, 1],
41
+ "brown": [165, 42, 42, 1], "burlywood": [222, 184, 135, 1],
42
+ "cadetblue": [95, 158, 160, 1], "chartreuse": [127, 255, 0, 1],
43
+ "chocolate": [210, 105, 30, 1], "coral": [255, 127, 80, 1],
44
+ "cornflowerblue": [100, 149, 237, 1], "cornsilk": [255, 248, 220, 1],
45
+ "crimson": [220, 20, 60, 1], "cyan": [0, 255, 255, 1],
46
+ "darkblue": [0, 0, 139, 1], "darkcyan": [0, 139, 139, 1],
47
+ "darkgoldenrod": [184, 134, 11, 1], "darkgray": [169, 169, 169, 1],
48
+ "darkgreen": [0, 100, 0, 1], "darkgrey": [169, 169, 169, 1],
49
+ "darkkhaki": [189, 183, 107, 1], "darkmagenta": [139, 0, 139, 1],
50
+ "darkolivegreen": [85, 107, 47, 1], "darkorange": [255, 140, 0, 1],
51
+ "darkorchid": [153, 50, 204, 1], "darkred": [139, 0, 0, 1],
52
+ "darksalmon": [233, 150, 122, 1], "darkseagreen": [143, 188, 143, 1],
53
+ "darkslateblue": [72, 61, 139, 1], "darkslategray": [47, 79, 79, 1],
54
+ "darkslategrey": [47, 79, 79, 1], "darkturquoise": [0, 206, 209, 1],
55
+ "darkviolet": [148, 0, 211, 1], "deeppink": [255, 20, 147, 1],
56
+ "deepskyblue": [0, 191, 255, 1], "dimgray": [105, 105, 105, 1],
57
+ "dimgrey": [105, 105, 105, 1], "dodgerblue": [30, 144, 255, 1],
58
+ "firebrick": [178, 34, 34, 1], "floralwhite": [255, 250, 240, 1],
59
+ "forestgreen": [34, 139, 34, 1], "fuchsia": [255, 0, 255, 1],
60
+ "gainsboro": [220, 220, 220, 1], "ghostwhite": [248, 248, 255, 1],
61
+ "gold": [255, 215, 0, 1], "goldenrod": [218, 165, 32, 1],
62
+ "gray": [128, 128, 128, 1], "green": [0, 128, 0, 1],
63
+ "greenyellow": [173, 255, 47, 1], "grey": [128, 128, 128, 1],
64
+ "honeydew": [240, 255, 240, 1], "hotpink": [255, 105, 180, 1],
65
+ "indianred": [205, 92, 92, 1], "indigo": [75, 0, 130, 1],
66
+ "ivory": [255, 255, 240, 1], "khaki": [240, 230, 140, 1],
67
+ "lavender": [230, 230, 250, 1], "lavenderblush": [255, 240, 245, 1],
68
+ "lawngreen": [124, 252, 0, 1], "lemonchiffon": [255, 250, 205, 1],
69
+ "lightblue": [173, 216, 230, 1], "lightcoral": [240, 128, 128, 1],
70
+ "lightcyan": [224, 255, 255, 1], "lightgoldenrodyellow": [250, 250, 210, 1],
71
+ "lightgray": [211, 211, 211, 1], "lightgreen": [144, 238, 144, 1],
72
+ "lightgrey": [211, 211, 211, 1], "lightpink": [255, 182, 193, 1],
73
+ "lightsalmon": [255, 160, 122, 1], "lightseagreen": [32, 178, 170, 1],
74
+ "lightskyblue": [135, 206, 250, 1], "lightslategray": [119, 136, 153, 1],
75
+ "lightslategrey": [119, 136, 153, 1], "lightsteelblue": [176, 196, 222, 1],
76
+ "lightyellow": [255, 255, 224, 1], "lime": [0, 255, 0, 1],
77
+ "limegreen": [50, 205, 50, 1], "linen": [250, 240, 230, 1],
78
+ "magenta": [255, 0, 255, 1], "maroon": [128, 0, 0, 1],
79
+ "mediumaquamarine": [102, 205, 170, 1], "mediumblue": [0, 0, 205, 1],
80
+ "mediumorchid": [186, 85, 211, 1], "mediumpurple": [147, 112, 219, 1],
81
+ "mediumseagreen": [60, 179, 113, 1], "mediumslateblue": [123, 104, 238, 1],
82
+ "mediumspringgreen": [0, 250, 154, 1], "mediumturquoise": [72, 209, 204, 1],
83
+ "mediumvioletred": [199, 21, 133, 1], "midnightblue": [25, 25, 112, 1],
84
+ "mintcream": [245, 255, 250, 1], "mistyrose": [255, 228, 225, 1],
85
+ "moccasin": [255, 228, 181, 1], "navajowhite": [255, 222, 173, 1],
86
+ "navy": [0, 0, 128, 1], "oldlace": [253, 245, 230, 1],
87
+ "olive": [128, 128, 0, 1], "olivedrab": [107, 142, 35, 1],
88
+ "orange": [255, 165, 0, 1], "orangered": [255, 69, 0, 1],
89
+ "orchid": [218, 112, 214, 1], "palegoldenrod": [238, 232, 170, 1],
90
+ "palegreen": [152, 251, 152, 1], "paleturquoise": [175, 238, 238, 1],
91
+ "palevioletred": [219, 112, 147, 1], "papayawhip": [255, 239, 213, 1],
92
+ "peachpuff": [255, 218, 185, 1], "peru": [205, 133, 63, 1],
93
+ "pink": [255, 192, 203, 1], "plum": [221, 160, 221, 1],
94
+ "powderblue": [176, 224, 230, 1], "purple": [128, 0, 128, 1],
95
+ "rebeccapurple": [102, 51, 153, 1],
96
+ "red": [255, 0, 0, 1], "rosybrown": [188, 143, 143, 1],
97
+ "royalblue": [65, 105, 225, 1], "saddlebrown": [139, 69, 19, 1],
98
+ "salmon": [250, 128, 114, 1], "sandybrown": [244, 164, 96, 1],
99
+ "seagreen": [46, 139, 87, 1], "seashell": [255, 245, 238, 1],
100
+ "sienna": [160, 82, 45, 1], "silver": [192, 192, 192, 1],
101
+ "skyblue": [135, 206, 235, 1], "slateblue": [106, 90, 205, 1],
102
+ "slategray": [112, 128, 144, 1], "slategrey": [112, 128, 144, 1],
103
+ "snow": [255, 250, 250, 1], "springgreen": [0, 255, 127, 1],
104
+ "steelblue": [70, 130, 180, 1], "tan": [210, 180, 140, 1],
105
+ "teal": [0, 128, 128, 1], "thistle": [216, 191, 216, 1],
106
+ "tomato": [255, 99, 71, 1], "turquoise": [64, 224, 208, 1],
107
+ "violet": [238, 130, 238, 1], "wheat": [245, 222, 179, 1],
108
+ "white": [255, 255, 255, 1], "whitesmoke": [245, 245, 245, 1],
109
+ "yellow": [255, 255, 0, 1], "yellowgreen": [154, 205, 50, 1]
110
+ }
111
+
112
+ function clamp_css_byte(i: number) { // Clamp to integer 0 .. 255.
113
+ i = Math.round(i); // Seems to be what Chrome does (vs truncation).
114
+ return i < 0 ? 0 : i > 255 ? 255 : i;
115
+ }
116
+
117
+ function clamp_css_float(f: number) { // Clamp to float 0.0 .. 1.0.
118
+ return f < 0 ? 0 : f > 1 ? 1 : f;
119
+ }
120
+
121
+ function parse_css_int(str: string) { // int or percentage.
122
+ if (str[str.length - 1] === '%')
123
+ return clamp_css_byte(parseFloat(str) / 100 * 255);
124
+ return clamp_css_byte(parseInt(str));
125
+ }
126
+
127
+ function parse_css_float(str: string) { // float or percentage.
128
+ if (str[str.length - 1] === '%')
129
+ return clamp_css_float(parseFloat(str) / 100);
130
+ return clamp_css_float(parseFloat(str));
131
+ }
132
+
133
+ function css_hue_to_rgb(m1: number, m2: number, h: number) {
134
+ if (h < 0) h += 1;
135
+ else if (h > 1) h -= 1;
136
+
137
+ if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
138
+ if (h * 2 < 1) return m2;
139
+ if (h * 3 < 2) return m1 + (m2 - m1) * (2 / 3 - h) * 6;
140
+ return m1;
141
+ }
142
+
143
+ function parseCSSColor(css_str: string): number[] | null {
144
+ // Remove all whitespace, not compliant, but should just be more accepting.
145
+ var str = css_str.replace(/ /g, '').toLowerCase();
146
+
147
+ // Color keywords (and transparent) lookup.
148
+ if (str in kCSSColorTable) return (kCSSColorTable as { [key: string]: number[] })[str].slice(); // dup.
149
+
150
+ // #abc and #abc123 syntax.
151
+ if (str[0] === '#') {
152
+ if (str.length === 4) {
153
+ var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing.
154
+ if (!(iv >= 0 && iv <= 0xfff)) return null; // Covers NaN.
155
+ return [((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8),
156
+ (iv & 0xf0) | ((iv & 0xf0) >> 4),
157
+ (iv & 0xf) | ((iv & 0xf) << 4),
158
+ 1];
159
+ } else if (str.length === 7) {
160
+ var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing.
161
+ if (!(iv >= 0 && iv <= 0xffffff)) return null; // Covers NaN.
162
+ return [(iv & 0xff0000) >> 16,
163
+ (iv & 0xff00) >> 8,
164
+ iv & 0xff,
165
+ 1];
166
+ }
167
+
168
+ return null;
169
+ }
170
+
171
+ var op = str.indexOf('('), ep = str.indexOf(')');
172
+ if (op !== -1 && ep + 1 === str.length) {
173
+ var fname = str.substr(0, op);
174
+ var params = str.substr(op + 1, ep - (op + 1)).split(',');
175
+ var alpha = 1; // To allow case fallthrough.
176
+ switch (fname) {
177
+ case 'rgba':
178
+ if (params.length !== 4) return null;
179
+ alpha = parse_css_float(params.pop() as string);
180
+ // Fall through.
181
+ case 'rgb':
182
+ if (params.length !== 3) return null;
183
+ return [parse_css_int(params[0]),
184
+ parse_css_int(params[1]),
185
+ parse_css_int(params[2]),
186
+ alpha];
187
+ case 'hsla':
188
+ if (params.length !== 4) return null;
189
+ alpha = parse_css_float(params.pop() as string);
190
+ // Fall through.
191
+ case 'hsl':
192
+ if (params.length !== 3) return null;
193
+ var h = (((parseFloat(params[0]) % 360) + 360) % 360) / 360; // 0 .. 1
194
+ // NOTE(deanm): According to the CSS spec s/l should only be
195
+ // percentages, but we don't bother and let float or percentage.
196
+ var s = parse_css_float(params[1]);
197
+ var l = parse_css_float(params[2]);
198
+ var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
199
+ var m1 = l * 2 - m2;
200
+ return [clamp_css_byte(css_hue_to_rgb(m1, m2, h + 1 / 3) * 255),
201
+ clamp_css_byte(css_hue_to_rgb(m1, m2, h) * 255),
202
+ clamp_css_byte(css_hue_to_rgb(m1, m2, h - 1 / 3) * 255),
203
+ alpha];
204
+ default:
205
+ return null;
206
+ }
207
+ }
208
+
209
+ return null;
210
+ }
211
+
212
+ function namedColor(name: keyof typeof kCSSColorTable): Color {
213
+ var c = kCSSColorTable[name]
214
+ return new Color(c[0] / 255, c[1] / 255, c[2] / 255, c[3])
215
+ }
216
+
217
+ function namedColors(...names: (keyof typeof kCSSColorTable)[]): Color[] {
218
+ let res = [] as Color[]
219
+ for (const name of names) {
220
+ res.push(namedColor(name))
221
+ }
222
+ return res
223
+ }
224
+
225
+ export function colorStrToF4(str: string): float4 {
226
+ var c = parseCSSColor(str)
227
+ if (c == null)
228
+ throw `Color ${str} cannot be parsed`
229
+
230
+ return f4(c[0] / 255, c[1] / 255, c[2] / 255, c[3])
231
+ }
232
+
233
+ /**
234
+ * Can parse any css color, array of 4 values [0 to 1.0], or a plain float4
235
+ */
236
+ export function parseColor(input: string | number[] | float4): Color {
237
+ if (Array.isArray(input)) {
238
+ return new Color(input[0], input[1], input[2], input[3])
239
+ } else if (typeof input === "string") {
240
+ var c = parseCSSColor(input)
241
+ if (c !== null) {
242
+ return new Color(c[0] / 255, c[1] / 255, c[2] / 255, c[3])
243
+ }
244
+ return new Color(0, 0, 0, 0)
245
+ }
246
+ return new Color(input.x, input.y, input.z, input.w)
247
+ }
248
+
249
+ export { parseCSSColor, namedColor, namedColors }
@@ -0,0 +1,31 @@
1
+ // import { float2, float3, float4 } from "Unity/Mathematics" // JSR prepends a "./" to this, which messes things up
2
+
3
+ type float2 = CS.Unity.Mathematics.float2
4
+ type float3 = CS.Unity.Mathematics.float3
5
+ type float4 = CS.Unity.Mathematics.float4
6
+
7
+ const { clamp, float2: f2, float3: f3, float4: f4 } = CS.Unity.Mathematics.math
8
+
9
+ export function parseFloat2(input: any): float2 {
10
+ if (!input)
11
+ return f2(0, 0)
12
+ if (Array.isArray(input))
13
+ input = f2(input[0] ?? 0, input[1] ?? 0)
14
+ return f2(input.x ?? 0, input.y ?? 0)
15
+ }
16
+
17
+ export function parseFloat3(input: any): float3 {
18
+ if (!input)
19
+ return f3(0, 0, 0)
20
+ if (Array.isArray(input))
21
+ input = f3(input[0] ?? 0, input[1] ?? 0, input[2] ?? 0)
22
+ return f3(input.x ?? 0, input.y ?? 0, input.z ?? 0)
23
+ }
24
+
25
+ export function parseFloat4(input: any): float4 {
26
+ if (!input)
27
+ return f4(0, 0, 0, 0)
28
+ if (Array.isArray(input))
29
+ input = f4(input[0] ?? 0, input[1] ?? 0, input[2] ?? 0, input[3] ?? 0)
30
+ return f4(input.x ?? 0, input.y ?? 0, input.z ?? 0, input.w ?? 0)
31
+ }
package/utils/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @module utils
3
+ *
4
+ * This module contains some commonly-used utility functions.
5
+ */
6
+
7
+ // @ts-ignore - prevent `allowImportingTsExtensions` error
8
+ export { parseColor, parseCSSColor, colorStrToF4, namedColor, namedColors } from "./color-parser"
9
+ // @ts-ignore - prevent `allowImportingTsExtensions` error
10
+ export { palettes } from "./color-palettes"
11
+ // @ts-ignore - prevent `allowImportingTsExtensions` error
12
+ export { parseFloat2, parseFloat3, parseFloat4 } from "./float-parser"