@terrazzo/token-tools 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @module @cobalt-ui/core
3
+ * @license MIT License
4
+ *
5
+ * Copyright (c) 2021 Drew Powers
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+ export * from './color.js';
26
+ export * from './id.js';
27
+ export * from './string.js';
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC"}
@@ -0,0 +1,4 @@
1
+ /** Pad string lengths */
2
+ export declare function padStr(input: string, length: number, alignment?: 'left' | 'center' | 'right'): string;
3
+ /** Pluralize strings */
4
+ export declare function pluralize<T = string>(count: number, singular: T, plural: T): T;
package/dist/string.js ADDED
@@ -0,0 +1,26 @@
1
+ /** Pad string lengths */
2
+ export function padStr(input, length, alignment = 'left') {
3
+ const d = Math.min(length || 0, 1000) - // guard against NaNs and Infinity
4
+ input.length;
5
+ if (d > 0) {
6
+ switch (alignment) {
7
+ case 'left': {
8
+ return `${input}${' '.repeat(d)}`;
9
+ }
10
+ case 'right': {
11
+ return `${' '.repeat(d)}${input}`;
12
+ }
13
+ case 'center': {
14
+ const left = Math.floor(d / 2);
15
+ const right = d - left;
16
+ return `${' '.repeat(left)}${input}${' '.repeat(right)}`;
17
+ }
18
+ }
19
+ }
20
+ return input;
21
+ }
22
+ /** Pluralize strings */
23
+ export function pluralize(count, singular, plural) {
24
+ return count === 1 ? singular : plural;
25
+ }
26
+ //# sourceMappingURL=string.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"string.js","sourceRoot":"","sources":["../src/string.ts"],"names":[],"mappings":"AAAA,yBAAyB;AACzB,MAAM,UAAU,MAAM,CAAC,KAAa,EAAE,MAAc,EAAE,YAAyC,MAAM;IACnG,MAAM,CAAC,GACL,IAAI,CAAC,GAAG,CAAC,MAAM,IAAI,CAAC,EAAE,IAAI,CAAC,GAAG,kCAAkC;QAChE,KAAK,CAAC,MAAM,CAAC;IACf,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QACV,QAAQ,SAAS,EAAE,CAAC;YAClB,KAAK,MAAM,CAAC,CAAC,CAAC;gBACZ,OAAO,GAAG,KAAK,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;YACpC,CAAC;YACD,KAAK,OAAO,CAAC,CAAC,CAAC;gBACb,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC;YACpC,CAAC;YACD,KAAK,QAAQ,CAAC,CAAC,CAAC;gBACd,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;gBAC/B,MAAM,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC;gBACvB,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,GAAG,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YAC3D,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,wBAAwB;AACxB,MAAM,UAAU,SAAS,CAAa,KAAa,EAAE,QAAW,EAAE,MAAS;IACzE,OAAO,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC;AACzC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@terrazzo/token-tools",
3
+ "description": "Various utilities for token types",
4
+ "version": "0.0.0",
5
+ "author": {
6
+ "name": "Drew Powers",
7
+ "email": "drew@pow.rs"
8
+ },
9
+ "keywords": [
10
+ "design tokens",
11
+ "design tokens community group",
12
+ "design tokens format module",
13
+ "dtcg",
14
+ "cli",
15
+ "w3c design tokens",
16
+ "design system",
17
+ "typescript",
18
+ "sass",
19
+ "css",
20
+ "style tokens",
21
+ "style system",
22
+ "linting",
23
+ "lint"
24
+ ],
25
+ "license": "MIT",
26
+ "type": "module",
27
+ "main": "./dist/index.js",
28
+ "exports": {
29
+ ".": "./dist/index.js",
30
+ "./css": "./dist/css/index.js",
31
+ "./*": "./*"
32
+ },
33
+ "dependencies": {
34
+ "@types/culori": "^2.1.0",
35
+ "culori": "^4.0.1",
36
+ "scule": "^1.3.0",
37
+ "wildcard-match": "^5.1.3"
38
+ },
39
+ "scripts": {
40
+ "build": "pnpm run build:clean && pnpm run build:ts && pnpm run build:license",
41
+ "build:clean": "del dist",
42
+ "build:ts": "tsc -p tsconfig.build.json",
43
+ "build:license": "node ../../scripts/inject-license.js @cobalt-ui/core dist/index.js",
44
+ "dev": "pnpm run --parallel \"/^dev:.*/\"",
45
+ "lint": "biome check .",
46
+ "dev:ts": "tsc -p tsconfig.build.json --watch",
47
+ "test": "pnpm run \"/^test:.*/\"",
48
+ "test:js": "vitest run",
49
+ "test:ts": "tsc --noEmit"
50
+ }
51
+ }
package/src/color.ts ADDED
@@ -0,0 +1,128 @@
1
+ import 'culori/css';
2
+ import { type Color, parse } from 'culori/fn';
3
+
4
+ export interface ColorValueNormalized {
5
+ /** Colorspace (default: `srgb`) @see https://www.w3.org/TR/css-color-4/#predefined */
6
+ colorSpace: ColorSpace;
7
+ /** Color channels. Will be normalized to 1 unless the colorspace prevents it (e.g. XYZ, LAB) */
8
+ channels: [number, number, number];
9
+ /** Alpha channel, normalized from 0 – 1 */
10
+ alpha: number;
11
+ }
12
+
13
+ export type ColorSpace =
14
+ | 'a98'
15
+ | 'display-p3'
16
+ | 'hsb'
17
+ | 'hsl'
18
+ | 'hsv'
19
+ | 'hwb'
20
+ | 'lab'
21
+ | 'lch'
22
+ | 'oklab'
23
+ | 'oklch'
24
+ | 'prophoto-rgb'
25
+ | 'rec2020'
26
+ | 'srgb-linear'
27
+ | 'srgb'
28
+ | 'xyz-d50'
29
+ | 'xyz-d65';
30
+
31
+ export const CULORI_TO_CSS: Record<
32
+ Extract<
33
+ Color['mode'],
34
+ | 'a98'
35
+ | 'lab'
36
+ | 'lch'
37
+ | 'oklab'
38
+ | 'oklch'
39
+ | 'hsl'
40
+ | 'hsv'
41
+ | 'hwb'
42
+ | 'lrgb'
43
+ | 'p3'
44
+ | 'prophoto'
45
+ | 'rec2020'
46
+ | 'rgb'
47
+ | 'xyz50'
48
+ | 'xyz65'
49
+ >,
50
+ ColorSpace
51
+ > = {
52
+ a98: 'a98',
53
+ hsl: 'hsl',
54
+ hsv: 'hsv',
55
+ hwb: 'hwb',
56
+ lab: 'lab',
57
+ lch: 'lch',
58
+ lrgb: 'srgb-linear',
59
+ oklab: 'oklab',
60
+ oklch: 'oklch',
61
+ p3: 'display-p3',
62
+ prophoto: 'prophoto-rgb',
63
+ rec2020: 'rec2020',
64
+ rgb: 'srgb',
65
+ xyz50: 'xyz-d50',
66
+ xyz65: 'xyz-d65',
67
+ };
68
+
69
+ export const CSS_TO_CULORI: Record<string, string> = {};
70
+ for (const k in CULORI_TO_CSS) {
71
+ CSS_TO_CULORI[CULORI_TO_CSS[k as keyof typeof CULORI_TO_CSS]] = k;
72
+ }
73
+
74
+ /** Parse any color */
75
+ export function parseColor(color: string): ColorValueNormalized {
76
+ const result = parse(color);
77
+ if (!result) {
78
+ throw new Error(`Unable to parse color "${color}"`);
79
+ }
80
+ if (!(result.mode in CULORI_TO_CSS)) {
81
+ throw new Error(`Unsupported color space: ${result.mode}`);
82
+ }
83
+ const colorSpace = CULORI_TO_CSS[result.mode as keyof typeof CULORI_TO_CSS];
84
+ let channels: [number, number, number] = [0, 0, 0];
85
+ switch (result.mode) {
86
+ case 'a98':
87
+ case 'rec2020':
88
+ case 'p3':
89
+ case 'prophoto':
90
+ case 'lrgb':
91
+ case 'rgb': {
92
+ channels = [result.r, result.g, result.b];
93
+ break;
94
+ }
95
+ case 'hsl': {
96
+ channels = [result.h ?? 0, result.s, result.l];
97
+ break;
98
+ }
99
+ case 'hsv': {
100
+ channels = [result.h ?? 0, result.s, result.v];
101
+ break;
102
+ }
103
+ case 'hwb': {
104
+ channels = [result.h ?? 0, result.w, result.b];
105
+ break;
106
+ }
107
+ case 'lab':
108
+ case 'oklab': {
109
+ channels = [result.l, result.a, result.b];
110
+ break;
111
+ }
112
+ case 'lch':
113
+ case 'oklch': {
114
+ channels = [result.l, result.c, result.h ?? 0];
115
+ break;
116
+ }
117
+ case 'xyz50':
118
+ case 'xyz65': {
119
+ channels = [result.x, result.y, result.z];
120
+ break;
121
+ }
122
+ }
123
+ return {
124
+ colorSpace,
125
+ channels,
126
+ alpha: result.alpha ?? 1,
127
+ };
128
+ }