@tenphi/tasty 0.0.0-snapshot.08a6610
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/LICENSE +21 -0
- package/README.md +666 -0
- package/dist/_virtual/_rolldown/runtime.js +8 -0
- package/dist/chunks/cacheKey.js +70 -0
- package/dist/chunks/cacheKey.js.map +1 -0
- package/dist/chunks/definitions.d.ts +37 -0
- package/dist/chunks/definitions.js +259 -0
- package/dist/chunks/definitions.js.map +1 -0
- package/dist/chunks/renderChunk.js +61 -0
- package/dist/chunks/renderChunk.js.map +1 -0
- package/dist/config.d.ts +311 -0
- package/dist/config.js +458 -0
- package/dist/config.js.map +1 -0
- package/dist/core/index.d.ts +32 -0
- package/dist/core/index.js +26 -0
- package/dist/debug.d.ts +204 -0
- package/dist/debug.js +733 -0
- package/dist/debug.js.map +1 -0
- package/dist/hooks/useGlobalStyles.d.ts +30 -0
- package/dist/hooks/useGlobalStyles.js +83 -0
- package/dist/hooks/useGlobalStyles.js.map +1 -0
- package/dist/hooks/useKeyframes.d.ts +56 -0
- package/dist/hooks/useKeyframes.js +69 -0
- package/dist/hooks/useKeyframes.js.map +1 -0
- package/dist/hooks/useProperty.d.ts +79 -0
- package/dist/hooks/useProperty.js +114 -0
- package/dist/hooks/useProperty.js.map +1 -0
- package/dist/hooks/useRawCSS.d.ts +53 -0
- package/dist/hooks/useRawCSS.js +40 -0
- package/dist/hooks/useRawCSS.js.map +1 -0
- package/dist/hooks/useStyles.d.ts +45 -0
- package/dist/hooks/useStyles.js +248 -0
- package/dist/hooks/useStyles.js.map +1 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +33 -0
- package/dist/injector/index.d.ts +165 -0
- package/dist/injector/index.js +162 -0
- package/dist/injector/index.js.map +1 -0
- package/dist/injector/injector.d.ts +148 -0
- package/dist/injector/injector.js +430 -0
- package/dist/injector/injector.js.map +1 -0
- package/dist/injector/sheet-manager.d.ts +136 -0
- package/dist/injector/sheet-manager.js +729 -0
- package/dist/injector/sheet-manager.js.map +1 -0
- package/dist/injector/types.d.ts +144 -0
- package/dist/keyframes/index.js +206 -0
- package/dist/keyframes/index.js.map +1 -0
- package/dist/parser/classify.js +319 -0
- package/dist/parser/classify.js.map +1 -0
- package/dist/parser/const.js +33 -0
- package/dist/parser/const.js.map +1 -0
- package/dist/parser/lru.js +109 -0
- package/dist/parser/lru.js.map +1 -0
- package/dist/parser/parser.d.ts +25 -0
- package/dist/parser/parser.js +116 -0
- package/dist/parser/parser.js.map +1 -0
- package/dist/parser/tokenizer.js +69 -0
- package/dist/parser/tokenizer.js.map +1 -0
- package/dist/parser/types.d.ts +51 -0
- package/dist/parser/types.js +46 -0
- package/dist/parser/types.js.map +1 -0
- package/dist/pipeline/conditions.d.ts +134 -0
- package/dist/pipeline/conditions.js +406 -0
- package/dist/pipeline/conditions.js.map +1 -0
- package/dist/pipeline/exclusive.js +231 -0
- package/dist/pipeline/exclusive.js.map +1 -0
- package/dist/pipeline/index.d.ts +53 -0
- package/dist/pipeline/index.js +660 -0
- package/dist/pipeline/index.js.map +1 -0
- package/dist/pipeline/materialize.js +856 -0
- package/dist/pipeline/materialize.js.map +1 -0
- package/dist/pipeline/parseStateKey.d.ts +15 -0
- package/dist/pipeline/parseStateKey.js +451 -0
- package/dist/pipeline/parseStateKey.js.map +1 -0
- package/dist/pipeline/simplify.js +516 -0
- package/dist/pipeline/simplify.js.map +1 -0
- package/dist/pipeline/warnings.js +18 -0
- package/dist/pipeline/warnings.js.map +1 -0
- package/dist/plugins/okhsl-plugin.d.ts +35 -0
- package/dist/plugins/okhsl-plugin.js +371 -0
- package/dist/plugins/okhsl-plugin.js.map +1 -0
- package/dist/plugins/types.d.ts +76 -0
- package/dist/properties/index.js +236 -0
- package/dist/properties/index.js.map +1 -0
- package/dist/properties/property-type-resolver.d.ts +24 -0
- package/dist/properties/property-type-resolver.js +91 -0
- package/dist/properties/property-type-resolver.js.map +1 -0
- package/dist/ssr/astro.d.ts +29 -0
- package/dist/ssr/astro.js +65 -0
- package/dist/ssr/astro.js.map +1 -0
- package/dist/ssr/async-storage.d.ts +17 -0
- package/dist/ssr/async-storage.js +35 -0
- package/dist/ssr/async-storage.js.map +1 -0
- package/dist/ssr/collect-auto-properties.js +40 -0
- package/dist/ssr/collect-auto-properties.js.map +1 -0
- package/dist/ssr/collector.d.ts +85 -0
- package/dist/ssr/collector.js +183 -0
- package/dist/ssr/collector.js.map +1 -0
- package/dist/ssr/context.d.ts +8 -0
- package/dist/ssr/context.js +14 -0
- package/dist/ssr/context.js.map +1 -0
- package/dist/ssr/format-global-rules.js +22 -0
- package/dist/ssr/format-global-rules.js.map +1 -0
- package/dist/ssr/format-keyframes.js +70 -0
- package/dist/ssr/format-keyframes.js.map +1 -0
- package/dist/ssr/format-property.js +48 -0
- package/dist/ssr/format-property.js.map +1 -0
- package/dist/ssr/format-rules.js +70 -0
- package/dist/ssr/format-rules.js.map +1 -0
- package/dist/ssr/hydrate.d.ts +22 -0
- package/dist/ssr/hydrate.js +50 -0
- package/dist/ssr/hydrate.js.map +1 -0
- package/dist/ssr/index.d.ts +5 -0
- package/dist/ssr/index.js +12 -0
- package/dist/ssr/index.js.map +1 -0
- package/dist/ssr/next.d.ts +45 -0
- package/dist/ssr/next.js +71 -0
- package/dist/ssr/next.js.map +1 -0
- package/dist/ssr/ssr-collector-ref.js +12 -0
- package/dist/ssr/ssr-collector-ref.js.map +1 -0
- package/dist/states/index.d.ts +49 -0
- package/dist/states/index.js +416 -0
- package/dist/states/index.js.map +1 -0
- package/dist/static/index.d.ts +5 -0
- package/dist/static/index.js +5 -0
- package/dist/static/tastyStatic.d.ts +46 -0
- package/dist/static/tastyStatic.js +31 -0
- package/dist/static/tastyStatic.js.map +1 -0
- package/dist/static/types.d.ts +49 -0
- package/dist/static/types.js +24 -0
- package/dist/static/types.js.map +1 -0
- package/dist/styles/align.d.ts +15 -0
- package/dist/styles/align.js +14 -0
- package/dist/styles/align.js.map +1 -0
- package/dist/styles/border.d.ts +25 -0
- package/dist/styles/border.js +114 -0
- package/dist/styles/border.js.map +1 -0
- package/dist/styles/color.d.ts +14 -0
- package/dist/styles/color.js +23 -0
- package/dist/styles/color.js.map +1 -0
- package/dist/styles/createStyle.js +77 -0
- package/dist/styles/createStyle.js.map +1 -0
- package/dist/styles/dimension.js +97 -0
- package/dist/styles/dimension.js.map +1 -0
- package/dist/styles/display.d.ts +37 -0
- package/dist/styles/display.js +67 -0
- package/dist/styles/display.js.map +1 -0
- package/dist/styles/fade.d.ts +15 -0
- package/dist/styles/fade.js +58 -0
- package/dist/styles/fade.js.map +1 -0
- package/dist/styles/fill.d.ts +42 -0
- package/dist/styles/fill.js +52 -0
- package/dist/styles/fill.js.map +1 -0
- package/dist/styles/flow.d.ts +16 -0
- package/dist/styles/flow.js +12 -0
- package/dist/styles/flow.js.map +1 -0
- package/dist/styles/gap.d.ts +31 -0
- package/dist/styles/gap.js +37 -0
- package/dist/styles/gap.js.map +1 -0
- package/dist/styles/height.d.ts +17 -0
- package/dist/styles/height.js +20 -0
- package/dist/styles/height.js.map +1 -0
- package/dist/styles/index.d.ts +2 -0
- package/dist/styles/index.js +9 -0
- package/dist/styles/index.js.map +1 -0
- package/dist/styles/inset.d.ts +52 -0
- package/dist/styles/inset.js +150 -0
- package/dist/styles/inset.js.map +1 -0
- package/dist/styles/justify.d.ts +15 -0
- package/dist/styles/justify.js +14 -0
- package/dist/styles/justify.js.map +1 -0
- package/dist/styles/list.d.ts +16 -0
- package/dist/styles/list.js +98 -0
- package/dist/styles/list.js.map +1 -0
- package/dist/styles/margin.d.ts +24 -0
- package/dist/styles/margin.js +104 -0
- package/dist/styles/margin.js.map +1 -0
- package/dist/styles/outline.d.ts +29 -0
- package/dist/styles/outline.js +65 -0
- package/dist/styles/outline.js.map +1 -0
- package/dist/styles/padding.d.ts +24 -0
- package/dist/styles/padding.js +104 -0
- package/dist/styles/padding.js.map +1 -0
- package/dist/styles/predefined.d.ts +71 -0
- package/dist/styles/predefined.js +238 -0
- package/dist/styles/predefined.js.map +1 -0
- package/dist/styles/preset.d.ts +47 -0
- package/dist/styles/preset.js +126 -0
- package/dist/styles/preset.js.map +1 -0
- package/dist/styles/radius.d.ts +14 -0
- package/dist/styles/radius.js +51 -0
- package/dist/styles/radius.js.map +1 -0
- package/dist/styles/scrollbar.d.ts +25 -0
- package/dist/styles/scrollbar.js +48 -0
- package/dist/styles/scrollbar.js.map +1 -0
- package/dist/styles/shadow.d.ts +14 -0
- package/dist/styles/shadow.js +24 -0
- package/dist/styles/shadow.js.map +1 -0
- package/dist/styles/transition.d.ts +14 -0
- package/dist/styles/transition.js +158 -0
- package/dist/styles/transition.js.map +1 -0
- package/dist/styles/types.d.ts +508 -0
- package/dist/styles/width.d.ts +17 -0
- package/dist/styles/width.js +20 -0
- package/dist/styles/width.js.map +1 -0
- package/dist/tasty.d.ts +981 -0
- package/dist/tasty.js +206 -0
- package/dist/tasty.js.map +1 -0
- package/dist/types.d.ts +184 -0
- package/dist/utils/cache-wrapper.js +26 -0
- package/dist/utils/cache-wrapper.js.map +1 -0
- package/dist/utils/case-converter.js +8 -0
- package/dist/utils/case-converter.js.map +1 -0
- package/dist/utils/colors.d.ts +5 -0
- package/dist/utils/colors.js +9 -0
- package/dist/utils/colors.js.map +1 -0
- package/dist/utils/css-types.d.ts +7 -0
- package/dist/utils/dotize.d.ts +26 -0
- package/dist/utils/dotize.js +122 -0
- package/dist/utils/dotize.js.map +1 -0
- package/dist/utils/filter-base-props.d.ts +15 -0
- package/dist/utils/filter-base-props.js +45 -0
- package/dist/utils/filter-base-props.js.map +1 -0
- package/dist/utils/get-display-name.d.ts +7 -0
- package/dist/utils/get-display-name.js +10 -0
- package/dist/utils/get-display-name.js.map +1 -0
- package/dist/utils/hsl-to-rgb.js +38 -0
- package/dist/utils/hsl-to-rgb.js.map +1 -0
- package/dist/utils/is-dev-env.js +19 -0
- package/dist/utils/is-dev-env.js.map +1 -0
- package/dist/utils/is-valid-element-type.js +15 -0
- package/dist/utils/is-valid-element-type.js.map +1 -0
- package/dist/utils/merge-styles.d.ts +7 -0
- package/dist/utils/merge-styles.js +146 -0
- package/dist/utils/merge-styles.js.map +1 -0
- package/dist/utils/mod-attrs.d.ts +8 -0
- package/dist/utils/mod-attrs.js +21 -0
- package/dist/utils/mod-attrs.js.map +1 -0
- package/dist/utils/okhsl-to-rgb.js +296 -0
- package/dist/utils/okhsl-to-rgb.js.map +1 -0
- package/dist/utils/process-tokens.d.ts +31 -0
- package/dist/utils/process-tokens.js +171 -0
- package/dist/utils/process-tokens.js.map +1 -0
- package/dist/utils/resolve-recipes.d.ts +17 -0
- package/dist/utils/resolve-recipes.js +147 -0
- package/dist/utils/resolve-recipes.js.map +1 -0
- package/dist/utils/selector-transform.js +32 -0
- package/dist/utils/selector-transform.js.map +1 -0
- package/dist/utils/string.js +8 -0
- package/dist/utils/string.js.map +1 -0
- package/dist/utils/styles.d.ts +177 -0
- package/dist/utils/styles.js +730 -0
- package/dist/utils/styles.js.map +1 -0
- package/dist/utils/typography.d.ts +47 -0
- package/dist/utils/typography.js +43 -0
- package/dist/utils/typography.js.map +1 -0
- package/dist/utils/warnings.d.ts +16 -0
- package/dist/utils/warnings.js +16 -0
- package/dist/utils/warnings.js.map +1 -0
- package/dist/zero/babel.d.ts +147 -0
- package/dist/zero/babel.js +331 -0
- package/dist/zero/babel.js.map +1 -0
- package/dist/zero/css-writer.d.ts +45 -0
- package/dist/zero/css-writer.js +74 -0
- package/dist/zero/css-writer.js.map +1 -0
- package/dist/zero/extractor.d.ts +24 -0
- package/dist/zero/extractor.js +215 -0
- package/dist/zero/extractor.js.map +1 -0
- package/dist/zero/index.d.ts +3 -0
- package/dist/zero/index.js +4 -0
- package/dist/zero/next.d.ts +74 -0
- package/dist/zero/next.js +129 -0
- package/dist/zero/next.js.map +1 -0
- package/docs/adoption.md +286 -0
- package/docs/comparison.md +413 -0
- package/docs/configuration.md +242 -0
- package/docs/debug.md +505 -0
- package/docs/design-system.md +401 -0
- package/docs/dsl.md +540 -0
- package/docs/getting-started.md +201 -0
- package/docs/injector.md +528 -0
- package/docs/methodology.md +501 -0
- package/docs/runtime.md +291 -0
- package/docs/ssr.md +382 -0
- package/docs/styles.md +574 -0
- package/docs/tasty-static.md +421 -0
- package/package.json +209 -0
- package/tasty.config.ts +14 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
//#region src/utils/okhsl-to-rgb.ts
|
|
2
|
+
const OKLab_to_LMS_M = [
|
|
3
|
+
[
|
|
4
|
+
1,
|
|
5
|
+
.3963377773761749,
|
|
6
|
+
.2158037573099136
|
|
7
|
+
],
|
|
8
|
+
[
|
|
9
|
+
1,
|
|
10
|
+
-.1055613458156586,
|
|
11
|
+
-.0638541728258133
|
|
12
|
+
],
|
|
13
|
+
[
|
|
14
|
+
1,
|
|
15
|
+
-.0894841775298119,
|
|
16
|
+
-1.2914855480194092
|
|
17
|
+
]
|
|
18
|
+
];
|
|
19
|
+
const LMS_to_linear_sRGB_M = [
|
|
20
|
+
[
|
|
21
|
+
4.076741636075959,
|
|
22
|
+
-3.307711539258062,
|
|
23
|
+
.2309699031821041
|
|
24
|
+
],
|
|
25
|
+
[
|
|
26
|
+
-1.2684379732850313,
|
|
27
|
+
2.6097573492876878,
|
|
28
|
+
-.3413193760026569
|
|
29
|
+
],
|
|
30
|
+
[
|
|
31
|
+
-.004196076138675526,
|
|
32
|
+
-.703418617935936,
|
|
33
|
+
1.7076146940746113
|
|
34
|
+
]
|
|
35
|
+
];
|
|
36
|
+
const OKLab_to_linear_sRGB_coefficients = [
|
|
37
|
+
[[-1.8817030993265873, -.8093650129914302], [
|
|
38
|
+
1.19086277,
|
|
39
|
+
1.76576728,
|
|
40
|
+
.59662641,
|
|
41
|
+
.75515197,
|
|
42
|
+
.56771245
|
|
43
|
+
]],
|
|
44
|
+
[[1.8144407988010998, -1.194452667805235], [
|
|
45
|
+
.73956515,
|
|
46
|
+
-.45954404,
|
|
47
|
+
.08285427,
|
|
48
|
+
.12541073,
|
|
49
|
+
-.14503204
|
|
50
|
+
]],
|
|
51
|
+
[[.13110757611180954, 1.813339709266608], [
|
|
52
|
+
1.35733652,
|
|
53
|
+
-.00915799,
|
|
54
|
+
-1.1513021,
|
|
55
|
+
-.50559606,
|
|
56
|
+
.00692167
|
|
57
|
+
]]
|
|
58
|
+
];
|
|
59
|
+
const TAU = 2 * Math.PI;
|
|
60
|
+
const K1 = .206;
|
|
61
|
+
const K2 = .03;
|
|
62
|
+
const K3 = (1 + K1) / (1 + K2);
|
|
63
|
+
const constrainAngle = (angle) => (angle % 360 + 360) % 360;
|
|
64
|
+
const toeInv = (x) => (x ** 2 + K1 * x) / (K3 * (x + K2));
|
|
65
|
+
const dot3 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
66
|
+
const dotXY = (a, b) => a[0] * b[0] + a[1] * b[1];
|
|
67
|
+
const transform = (input, matrix) => [
|
|
68
|
+
dot3(input, matrix[0]),
|
|
69
|
+
dot3(input, matrix[1]),
|
|
70
|
+
dot3(input, matrix[2])
|
|
71
|
+
];
|
|
72
|
+
const cubed3 = (lms) => [
|
|
73
|
+
lms[0] ** 3,
|
|
74
|
+
lms[1] ** 3,
|
|
75
|
+
lms[2] ** 3
|
|
76
|
+
];
|
|
77
|
+
const OKLabToLinearSRGB = (lab) => {
|
|
78
|
+
return transform(cubed3(transform(lab, OKLab_to_LMS_M)), LMS_to_linear_sRGB_M);
|
|
79
|
+
};
|
|
80
|
+
const sRGBLinearToGamma = (val) => {
|
|
81
|
+
const sign = val < 0 ? -1 : 1;
|
|
82
|
+
const abs = Math.abs(val);
|
|
83
|
+
return abs > .0031308 ? sign * (1.055 * Math.pow(abs, 1 / 2.4) - .055) : 12.92 * val;
|
|
84
|
+
};
|
|
85
|
+
const computeMaxSaturationOKLC = (a, b) => {
|
|
86
|
+
const okCoeff = OKLab_to_linear_sRGB_coefficients;
|
|
87
|
+
const lmsToRgb = LMS_to_linear_sRGB_M;
|
|
88
|
+
const tmp2 = [a, b];
|
|
89
|
+
const tmp3 = [
|
|
90
|
+
0,
|
|
91
|
+
a,
|
|
92
|
+
b
|
|
93
|
+
];
|
|
94
|
+
let chnlCoeff;
|
|
95
|
+
let chnlLMS;
|
|
96
|
+
if (dotXY(okCoeff[0][0], tmp2) > 1) {
|
|
97
|
+
chnlCoeff = okCoeff[0][1];
|
|
98
|
+
chnlLMS = lmsToRgb[0];
|
|
99
|
+
} else if (dotXY(okCoeff[1][0], tmp2) > 1) {
|
|
100
|
+
chnlCoeff = okCoeff[1][1];
|
|
101
|
+
chnlLMS = lmsToRgb[1];
|
|
102
|
+
} else {
|
|
103
|
+
chnlCoeff = okCoeff[2][1];
|
|
104
|
+
chnlLMS = lmsToRgb[2];
|
|
105
|
+
}
|
|
106
|
+
const [k0, k1, k2, k3, k4] = chnlCoeff;
|
|
107
|
+
const [wl, wm, ws] = chnlLMS;
|
|
108
|
+
let sat = k0 + k1 * a + k2 * b + k3 * (a * a) + k4 * a * b;
|
|
109
|
+
const dotYZ = (mat, vec) => mat[1] * vec[1] + mat[2] * vec[2];
|
|
110
|
+
const kl = dotYZ(OKLab_to_LMS_M[0], tmp3);
|
|
111
|
+
const km = dotYZ(OKLab_to_LMS_M[1], tmp3);
|
|
112
|
+
const ks = dotYZ(OKLab_to_LMS_M[2], tmp3);
|
|
113
|
+
const l_ = 1 + sat * kl;
|
|
114
|
+
const m_ = 1 + sat * km;
|
|
115
|
+
const s_ = 1 + sat * ks;
|
|
116
|
+
const l = l_ ** 3;
|
|
117
|
+
const m = m_ ** 3;
|
|
118
|
+
const s = s_ ** 3;
|
|
119
|
+
const lds = 3 * kl * l_ * l_;
|
|
120
|
+
const mds = 3 * km * m_ * m_;
|
|
121
|
+
const sds = 3 * ks * s_ * s_;
|
|
122
|
+
const lds2 = 6 * kl * kl * l_;
|
|
123
|
+
const mds2 = 6 * km * km * m_;
|
|
124
|
+
const sds2 = 6 * ks * ks * s_;
|
|
125
|
+
const f = wl * l + wm * m + ws * s;
|
|
126
|
+
const f1 = wl * lds + wm * mds + ws * sds;
|
|
127
|
+
const f2 = wl * lds2 + wm * mds2 + ws * sds2;
|
|
128
|
+
sat = sat - f * f1 / (f1 * f1 - .5 * f * f2);
|
|
129
|
+
return sat;
|
|
130
|
+
};
|
|
131
|
+
const findCuspOKLCH = (a, b) => {
|
|
132
|
+
const S_cusp = computeMaxSaturationOKLC(a, b);
|
|
133
|
+
const rgb_at_max = OKLabToLinearSRGB([
|
|
134
|
+
1,
|
|
135
|
+
S_cusp * a,
|
|
136
|
+
S_cusp * b
|
|
137
|
+
]);
|
|
138
|
+
const L_cusp = Math.cbrt(1 / Math.max(Math.max(rgb_at_max[0], rgb_at_max[1]), Math.max(rgb_at_max[2], 0)));
|
|
139
|
+
return [L_cusp, L_cusp * S_cusp];
|
|
140
|
+
};
|
|
141
|
+
const findGamutIntersectionOKLCH = (a, b, l1, c1, l0, cusp) => {
|
|
142
|
+
const lmsToRgb = LMS_to_linear_sRGB_M;
|
|
143
|
+
const tmp3 = [
|
|
144
|
+
0,
|
|
145
|
+
a,
|
|
146
|
+
b
|
|
147
|
+
];
|
|
148
|
+
const floatMax = Number.MAX_VALUE;
|
|
149
|
+
let t;
|
|
150
|
+
const dotYZ = (mat, vec) => mat[1] * vec[1] + mat[2] * vec[2];
|
|
151
|
+
const dotXYZ = (vec, x, y, z) => vec[0] * x + vec[1] * y + vec[2] * z;
|
|
152
|
+
if ((l1 - l0) * cusp[1] - (cusp[0] - l0) * c1 <= 0) {
|
|
153
|
+
const denom = c1 * cusp[0] + cusp[1] * (l0 - l1);
|
|
154
|
+
t = denom === 0 ? 0 : cusp[1] * l0 / denom;
|
|
155
|
+
} else {
|
|
156
|
+
const denom = c1 * (cusp[0] - 1) + cusp[1] * (l0 - l1);
|
|
157
|
+
t = denom === 0 ? 0 : cusp[1] * (l0 - 1) / denom;
|
|
158
|
+
const dl = l1 - l0;
|
|
159
|
+
const dc = c1;
|
|
160
|
+
const kl = dotYZ(OKLab_to_LMS_M[0], tmp3);
|
|
161
|
+
const km = dotYZ(OKLab_to_LMS_M[1], tmp3);
|
|
162
|
+
const ks = dotYZ(OKLab_to_LMS_M[2], tmp3);
|
|
163
|
+
const L = l0 * (1 - t) + t * l1;
|
|
164
|
+
const C = t * c1;
|
|
165
|
+
const l_ = L + C * kl;
|
|
166
|
+
const m_ = L + C * km;
|
|
167
|
+
const s_ = L + C * ks;
|
|
168
|
+
const l = l_ ** 3;
|
|
169
|
+
const m = m_ ** 3;
|
|
170
|
+
const s = s_ ** 3;
|
|
171
|
+
const ldt = 3 * (dl + dc * kl) * l_ * l_;
|
|
172
|
+
const mdt = 3 * (dl + dc * km) * m_ * m_;
|
|
173
|
+
const sdt = 3 * (dl + dc * ks) * s_ * s_;
|
|
174
|
+
const ldt2 = 6 * (dl + dc * kl) ** 2 * l_;
|
|
175
|
+
const mdt2 = 6 * (dl + dc * km) ** 2 * m_;
|
|
176
|
+
const sdt2 = 6 * (dl + dc * ks) ** 2 * s_;
|
|
177
|
+
const r_ = dotXYZ(lmsToRgb[0], l, m, s) - 1;
|
|
178
|
+
const r1 = dotXYZ(lmsToRgb[0], ldt, mdt, sdt);
|
|
179
|
+
const r2 = dotXYZ(lmsToRgb[0], ldt2, mdt2, sdt2);
|
|
180
|
+
const ur = r1 / (r1 * r1 - .5 * r_ * r2);
|
|
181
|
+
let tr = -r_ * ur;
|
|
182
|
+
const g_ = dotXYZ(lmsToRgb[1], l, m, s) - 1;
|
|
183
|
+
const g1 = dotXYZ(lmsToRgb[1], ldt, mdt, sdt);
|
|
184
|
+
const g2 = dotXYZ(lmsToRgb[1], ldt2, mdt2, sdt2);
|
|
185
|
+
const ug = g1 / (g1 * g1 - .5 * g_ * g2);
|
|
186
|
+
let tg = -g_ * ug;
|
|
187
|
+
const b_ = dotXYZ(lmsToRgb[2], l, m, s) - 1;
|
|
188
|
+
const b1 = dotXYZ(lmsToRgb[2], ldt, mdt, sdt);
|
|
189
|
+
const b2 = dotXYZ(lmsToRgb[2], ldt2, mdt2, sdt2);
|
|
190
|
+
const ub = b1 / (b1 * b1 - .5 * b_ * b2);
|
|
191
|
+
let tb = -b_ * ub;
|
|
192
|
+
tr = ur >= 0 ? tr : floatMax;
|
|
193
|
+
tg = ug >= 0 ? tg : floatMax;
|
|
194
|
+
tb = ub >= 0 ? tb : floatMax;
|
|
195
|
+
t += Math.min(tr, Math.min(tg, tb));
|
|
196
|
+
}
|
|
197
|
+
return t;
|
|
198
|
+
};
|
|
199
|
+
const computeSt = (cusp) => [cusp[1] / cusp[0], cusp[1] / (1 - cusp[0])];
|
|
200
|
+
const computeStMid = (a, b) => [.11516993 + 1 / (7.4477897 + 4.1590124 * b + a * (-2.19557347 + 1.75198401 * b + a * (-2.13704948 - 10.02301043 * b + a * (-4.24894561 + 5.38770819 * b + 4.69891013 * a)))), .11239642 + 1 / (1.6132032 - .68124379 * b + a * (.40370612 + .90148123 * b + a * (-.27087943 + .6122399 * b + a * (.00299215 - .45399568 * b - .14661872 * a))))];
|
|
201
|
+
const getCs = (L, a, b, cusp) => {
|
|
202
|
+
const cMax = findGamutIntersectionOKLCH(a, b, L, 1, L, cusp);
|
|
203
|
+
const stMax = computeSt(cusp);
|
|
204
|
+
const k = cMax / Math.min(L * stMax[0], (1 - L) * stMax[1]);
|
|
205
|
+
const stMid = computeStMid(a, b);
|
|
206
|
+
let ca = L * stMid[0];
|
|
207
|
+
let cb = (1 - L) * stMid[1];
|
|
208
|
+
const cMid = .9 * k * Math.sqrt(Math.sqrt(1 / (1 / ca ** 4 + 1 / cb ** 4)));
|
|
209
|
+
ca = L * .4;
|
|
210
|
+
cb = (1 - L) * .8;
|
|
211
|
+
return [
|
|
212
|
+
Math.sqrt(1 / (1 / ca ** 2 + 1 / cb ** 2)),
|
|
213
|
+
cMid,
|
|
214
|
+
cMax
|
|
215
|
+
];
|
|
216
|
+
};
|
|
217
|
+
const OKHSLToOKLab = (hsl) => {
|
|
218
|
+
let h = hsl[0];
|
|
219
|
+
const s = hsl[1];
|
|
220
|
+
const l = hsl[2];
|
|
221
|
+
const L = toeInv(l);
|
|
222
|
+
let a = 0;
|
|
223
|
+
let b = 0;
|
|
224
|
+
h = constrainAngle(h) / 360;
|
|
225
|
+
if (L !== 0 && L !== 1 && s !== 0) {
|
|
226
|
+
const a_ = Math.cos(TAU * h);
|
|
227
|
+
const b_ = Math.sin(TAU * h);
|
|
228
|
+
const [c0, cMid, cMax] = getCs(L, a_, b_, findCuspOKLCH(a_, b_));
|
|
229
|
+
const mid = .8;
|
|
230
|
+
const midInv = 1.25;
|
|
231
|
+
let t, k0, k1, k2;
|
|
232
|
+
if (s < mid) {
|
|
233
|
+
t = midInv * s;
|
|
234
|
+
k0 = 0;
|
|
235
|
+
k1 = mid * c0;
|
|
236
|
+
k2 = 1 - k1 / cMid;
|
|
237
|
+
} else {
|
|
238
|
+
t = 5 * (s - .8);
|
|
239
|
+
k0 = cMid;
|
|
240
|
+
k1 = .2 * cMid ** 2 * 1.25 ** 2 / c0;
|
|
241
|
+
k2 = 1 - k1 / (cMax - cMid);
|
|
242
|
+
}
|
|
243
|
+
const c = k0 + t * k1 / (1 - k2 * t);
|
|
244
|
+
a = c * a_;
|
|
245
|
+
b = c * b_;
|
|
246
|
+
}
|
|
247
|
+
return [
|
|
248
|
+
L,
|
|
249
|
+
a,
|
|
250
|
+
b
|
|
251
|
+
];
|
|
252
|
+
};
|
|
253
|
+
function okhslToSrgbInternal(h, s, l) {
|
|
254
|
+
const linearRGB = OKLabToLinearSRGB(OKHSLToOKLab([
|
|
255
|
+
h,
|
|
256
|
+
s,
|
|
257
|
+
l
|
|
258
|
+
]));
|
|
259
|
+
return [
|
|
260
|
+
sRGBLinearToGamma(linearRGB[0]),
|
|
261
|
+
sRGBLinearToGamma(linearRGB[1]),
|
|
262
|
+
sRGBLinearToGamma(linearRGB[2])
|
|
263
|
+
];
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Convert OKHSL color string to RGB.
|
|
267
|
+
* Uses the same algorithm as the okhsl-plugin.
|
|
268
|
+
*/
|
|
269
|
+
function okhslToRgb(okhslStr) {
|
|
270
|
+
const match = okhslStr.match(/okhsl\(([^)]+)\)/i);
|
|
271
|
+
if (!match) return null;
|
|
272
|
+
const [colorPart, alphaPart] = match[1].trim().split("/");
|
|
273
|
+
const parts = colorPart.trim().split(/[,\s]+/).filter(Boolean);
|
|
274
|
+
if (parts.length < 3) return null;
|
|
275
|
+
let h = parseFloat(parts[0]);
|
|
276
|
+
const hueStr = parts[0].toLowerCase();
|
|
277
|
+
if (hueStr.endsWith("turn")) h = parseFloat(hueStr) * 360;
|
|
278
|
+
else if (hueStr.endsWith("rad")) h = parseFloat(hueStr) * 180 / Math.PI;
|
|
279
|
+
else if (hueStr.endsWith("deg")) h = parseFloat(hueStr);
|
|
280
|
+
const parsePercent = (val) => {
|
|
281
|
+
const num = parseFloat(val);
|
|
282
|
+
return val.includes("%") ? num / 100 : num;
|
|
283
|
+
};
|
|
284
|
+
const s = Math.max(0, Math.min(1, parsePercent(parts[1])));
|
|
285
|
+
const l = Math.max(0, Math.min(1, parsePercent(parts[2])));
|
|
286
|
+
const [r, g, b] = okhslToSrgbInternal(h, s, l);
|
|
287
|
+
const r255 = Math.round(Math.max(0, Math.min(1, r)) * 255);
|
|
288
|
+
const g255 = Math.round(Math.max(0, Math.min(1, g)) * 255);
|
|
289
|
+
const b255 = Math.round(Math.max(0, Math.min(1, b)) * 255);
|
|
290
|
+
if (alphaPart) return `rgba(${r255}, ${g255}, ${b255}, ${parseFloat(alphaPart.trim())})`;
|
|
291
|
+
return `rgb(${r255} ${g255} ${b255})`;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
//#endregion
|
|
295
|
+
export { okhslToRgb };
|
|
296
|
+
//# sourceMappingURL=okhsl-to-rgb.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"okhsl-to-rgb.js","names":[],"sources":["../../src/utils/okhsl-to-rgb.ts"],"sourcesContent":["// ============================================================================\n// OKHSL to RGB conversion (inlined from okhsl-plugin for static conversion)\n// ============================================================================\n\ntype Vec3 = [number, number, number];\n\n// OKHSL conversion internals (from texel-color, same as okhsl-plugin)\nconst OKLab_to_LMS_M: Vec3[] = [\n [1.0, 0.3963377773761749, 0.2158037573099136],\n [1.0, -0.1055613458156586, -0.0638541728258133],\n [1.0, -0.0894841775298119, -1.2914855480194092],\n];\n\nconst LMS_to_linear_sRGB_M: Vec3[] = [\n [4.076741636075959, -3.307711539258062, 0.2309699031821041],\n [-1.2684379732850313, 2.6097573492876878, -0.3413193760026569],\n [-0.004196076138675526, -0.703418617935936, 1.7076146940746113],\n];\n\nconst OKLab_to_linear_sRGB_coefficients: [\n [[number, number], number[]],\n [[number, number], number[]],\n [[number, number], number[]],\n] = [\n [\n [-1.8817030993265873, -0.8093650129914302],\n [1.19086277, 1.76576728, 0.59662641, 0.75515197, 0.56771245],\n ],\n [\n [1.8144407988010998, -1.194452667805235],\n [0.73956515, -0.45954404, 0.08285427, 0.12541073, -0.14503204],\n ],\n [\n [0.13110757611180954, 1.813339709266608],\n [1.35733652, -0.00915799, -1.1513021, -0.50559606, 0.00692167],\n ],\n];\n\nconst TAU = 2 * Math.PI;\nconst K1 = 0.206;\nconst K2 = 0.03;\nconst K3 = (1.0 + K1) / (1.0 + K2);\n\nconst constrainAngle = (angle: number): number => ((angle % 360) + 360) % 360;\nconst toeInv = (x: number): number => (x ** 2 + K1 * x) / (K3 * (x + K2));\nconst dot3 = (a: Vec3, b: Vec3): number =>\n a[0] * b[0] + a[1] * b[1] + a[2] * b[2];\nconst dotXY = (a: [number, number], b: [number, number]): number =>\n a[0] * b[0] + a[1] * b[1];\nconst transform = (input: Vec3, matrix: Vec3[]): Vec3 => [\n dot3(input, matrix[0]),\n dot3(input, matrix[1]),\n dot3(input, matrix[2]),\n];\nconst cubed3 = (lms: Vec3): Vec3 => [lms[0] ** 3, lms[1] ** 3, lms[2] ** 3];\n\nconst OKLabToLinearSRGB = (lab: Vec3): Vec3 => {\n const lms = transform(lab, OKLab_to_LMS_M);\n return transform(cubed3(lms), LMS_to_linear_sRGB_M);\n};\n\nconst sRGBLinearToGamma = (val: number): number => {\n const sign = val < 0 ? -1 : 1;\n const abs = Math.abs(val);\n return abs > 0.0031308\n ? sign * (1.055 * Math.pow(abs, 1 / 2.4) - 0.055)\n : 12.92 * val;\n};\n\nconst computeMaxSaturationOKLC = (a: number, b: number): number => {\n const okCoeff = OKLab_to_linear_sRGB_coefficients;\n const lmsToRgb = LMS_to_linear_sRGB_M;\n const tmp2: [number, number] = [a, b];\n const tmp3: Vec3 = [0, a, b];\n\n let chnlCoeff: number[];\n let chnlLMS: Vec3;\n\n if (dotXY(okCoeff[0][0], tmp2) > 1) {\n chnlCoeff = okCoeff[0][1];\n chnlLMS = lmsToRgb[0];\n } else if (dotXY(okCoeff[1][0], tmp2) > 1) {\n chnlCoeff = okCoeff[1][1];\n chnlLMS = lmsToRgb[1];\n } else {\n chnlCoeff = okCoeff[2][1];\n chnlLMS = lmsToRgb[2];\n }\n\n const [k0, k1, k2, k3, k4] = chnlCoeff;\n const [wl, wm, ws] = chnlLMS;\n\n let sat = k0 + k1 * a + k2 * b + k3 * (a * a) + k4 * a * b;\n\n const dotYZ = (mat: Vec3, vec: Vec3): number =>\n mat[1] * vec[1] + mat[2] * vec[2];\n\n const kl = dotYZ(OKLab_to_LMS_M[0], tmp3);\n const km = dotYZ(OKLab_to_LMS_M[1], tmp3);\n const ks = dotYZ(OKLab_to_LMS_M[2], tmp3);\n\n const l_ = 1.0 + sat * kl;\n const m_ = 1.0 + sat * km;\n const s_ = 1.0 + sat * ks;\n\n const l = l_ ** 3;\n const m = m_ ** 3;\n const s = s_ ** 3;\n\n const lds = 3.0 * kl * l_ * l_;\n const mds = 3.0 * km * m_ * m_;\n const sds = 3.0 * ks * s_ * s_;\n\n const lds2 = 6.0 * kl * kl * l_;\n const mds2 = 6.0 * km * km * m_;\n const sds2 = 6.0 * ks * ks * s_;\n\n const f = wl * l + wm * m + ws * s;\n const f1 = wl * lds + wm * mds + ws * sds;\n const f2 = wl * lds2 + wm * mds2 + ws * sds2;\n\n sat = sat - (f * f1) / (f1 * f1 - 0.5 * f * f2);\n\n return sat;\n};\n\nconst findCuspOKLCH = (a: number, b: number): [number, number] => {\n const S_cusp = computeMaxSaturationOKLC(a, b);\n const lab: Vec3 = [1, S_cusp * a, S_cusp * b];\n const rgb_at_max = OKLabToLinearSRGB(lab);\n const L_cusp = Math.cbrt(\n 1 /\n Math.max(\n Math.max(rgb_at_max[0], rgb_at_max[1]),\n Math.max(rgb_at_max[2], 0.0),\n ),\n );\n return [L_cusp, L_cusp * S_cusp];\n};\n\nconst findGamutIntersectionOKLCH = (\n a: number,\n b: number,\n l1: number,\n c1: number,\n l0: number,\n cusp: [number, number],\n): number => {\n const lmsToRgb = LMS_to_linear_sRGB_M;\n const tmp3: Vec3 = [0, a, b];\n const floatMax = Number.MAX_VALUE;\n\n let t: number;\n\n const dotYZ = (mat: Vec3, vec: Vec3): number =>\n mat[1] * vec[1] + mat[2] * vec[2];\n const dotXYZ = (vec: Vec3, x: number, y: number, z: number): number =>\n vec[0] * x + vec[1] * y + vec[2] * z;\n\n if ((l1 - l0) * cusp[1] - (cusp[0] - l0) * c1 <= 0.0) {\n const denom = c1 * cusp[0] + cusp[1] * (l0 - l1);\n t = denom === 0 ? 0 : (cusp[1] * l0) / denom;\n } else {\n const denom = c1 * (cusp[0] - 1.0) + cusp[1] * (l0 - l1);\n t = denom === 0 ? 0 : (cusp[1] * (l0 - 1.0)) / denom;\n\n const dl = l1 - l0;\n const dc = c1;\n const kl = dotYZ(OKLab_to_LMS_M[0], tmp3);\n const km = dotYZ(OKLab_to_LMS_M[1], tmp3);\n const ks = dotYZ(OKLab_to_LMS_M[2], tmp3);\n\n const L = l0 * (1.0 - t) + t * l1;\n const C = t * c1;\n\n const l_ = L + C * kl;\n const m_ = L + C * km;\n const s_ = L + C * ks;\n\n const l = l_ ** 3;\n const m = m_ ** 3;\n const s = s_ ** 3;\n\n const ldt = 3 * (dl + dc * kl) * l_ * l_;\n const mdt = 3 * (dl + dc * km) * m_ * m_;\n const sdt = 3 * (dl + dc * ks) * s_ * s_;\n\n const ldt2 = 6 * (dl + dc * kl) ** 2 * l_;\n const mdt2 = 6 * (dl + dc * km) ** 2 * m_;\n const sdt2 = 6 * (dl + dc * ks) ** 2 * s_;\n\n const r_ = dotXYZ(lmsToRgb[0], l, m, s) - 1;\n const r1 = dotXYZ(lmsToRgb[0], ldt, mdt, sdt);\n const r2 = dotXYZ(lmsToRgb[0], ldt2, mdt2, sdt2);\n const ur = r1 / (r1 * r1 - 0.5 * r_ * r2);\n let tr = -r_ * ur;\n\n const g_ = dotXYZ(lmsToRgb[1], l, m, s) - 1;\n const g1 = dotXYZ(lmsToRgb[1], ldt, mdt, sdt);\n const g2 = dotXYZ(lmsToRgb[1], ldt2, mdt2, sdt2);\n const ug = g1 / (g1 * g1 - 0.5 * g_ * g2);\n let tg = -g_ * ug;\n\n const b_ = dotXYZ(lmsToRgb[2], l, m, s) - 1;\n const b1 = dotXYZ(lmsToRgb[2], ldt, mdt, sdt);\n const b2 = dotXYZ(lmsToRgb[2], ldt2, mdt2, sdt2);\n const ub = b1 / (b1 * b1 - 0.5 * b_ * b2);\n let tb = -b_ * ub;\n\n tr = ur >= 0.0 ? tr : floatMax;\n tg = ug >= 0.0 ? tg : floatMax;\n tb = ub >= 0.0 ? tb : floatMax;\n\n t += Math.min(tr, Math.min(tg, tb));\n }\n\n return t;\n};\n\nconst computeSt = (cusp: [number, number]): [number, number] => [\n cusp[1] / cusp[0],\n cusp[1] / (1 - cusp[0]),\n];\n\nconst computeStMid = (a: number, b: number): [number, number] => [\n 0.11516993 +\n 1.0 /\n (7.4477897 +\n 4.1590124 * b +\n a *\n (-2.19557347 +\n 1.75198401 * b +\n a *\n (-2.13704948 -\n 10.02301043 * b +\n a * (-4.24894561 + 5.38770819 * b + 4.69891013 * a)))),\n 0.11239642 +\n 1.0 /\n (1.6132032 -\n 0.68124379 * b +\n a *\n (0.40370612 +\n 0.90148123 * b +\n a *\n (-0.27087943 +\n 0.6122399 * b +\n a * (0.00299215 - 0.45399568 * b - 0.14661872 * a)))),\n];\n\nconst getCs = (\n L: number,\n a: number,\n b: number,\n cusp: [number, number],\n): [number, number, number] => {\n const cMax = findGamutIntersectionOKLCH(a, b, L, 1, L, cusp);\n const stMax = computeSt(cusp);\n const k = cMax / Math.min(L * stMax[0], (1 - L) * stMax[1]);\n const stMid = computeStMid(a, b);\n let ca = L * stMid[0];\n let cb = (1.0 - L) * stMid[1];\n const cMid =\n 0.9 * k * Math.sqrt(Math.sqrt(1.0 / (1.0 / ca ** 4 + 1.0 / cb ** 4)));\n ca = L * 0.4;\n cb = (1.0 - L) * 0.8;\n const c0 = Math.sqrt(1.0 / (1.0 / ca ** 2 + 1.0 / cb ** 2));\n return [c0, cMid, cMax];\n};\n\nconst OKHSLToOKLab = (hsl: Vec3): Vec3 => {\n let h = hsl[0];\n const s = hsl[1];\n const l = hsl[2];\n\n const L = toeInv(l);\n let a = 0;\n let b = 0;\n\n h = constrainAngle(h) / 360.0;\n\n if (L !== 0.0 && L !== 1.0 && s !== 0) {\n const a_ = Math.cos(TAU * h);\n const b_ = Math.sin(TAU * h);\n\n const cusp = findCuspOKLCH(a_, b_);\n const Cs = getCs(L, a_, b_, cusp);\n const [c0, cMid, cMax] = Cs;\n\n const mid = 0.8;\n const midInv = 1.25;\n let t: number, k0: number, k1: number, k2: number;\n\n if (s < mid) {\n t = midInv * s;\n k0 = 0.0;\n k1 = mid * c0;\n k2 = 1.0 - k1 / cMid;\n } else {\n t = 5 * (s - 0.8);\n k0 = cMid;\n k1 = (0.2 * cMid ** 2 * 1.25 ** 2) / c0;\n k2 = 1.0 - k1 / (cMax - cMid);\n }\n\n const c = k0 + (t * k1) / (1.0 - k2 * t);\n a = c * a_;\n b = c * b_;\n }\n\n return [L, a, b];\n};\n\nfunction okhslToSrgbInternal(h: number, s: number, l: number): Vec3 {\n const oklab = OKHSLToOKLab([h, s, l]);\n const linearRGB = OKLabToLinearSRGB(oklab);\n return [\n sRGBLinearToGamma(linearRGB[0]),\n sRGBLinearToGamma(linearRGB[1]),\n sRGBLinearToGamma(linearRGB[2]),\n ];\n}\n\n/**\n * Convert OKHSL color string to RGB.\n * Uses the same algorithm as the okhsl-plugin.\n */\nexport function okhslToRgb(okhslStr: string): string | null {\n const match = okhslStr.match(/okhsl\\(([^)]+)\\)/i);\n if (!match) return null;\n\n const inner = match[1].trim();\n const [colorPart, alphaPart] = inner.split('/');\n const parts = colorPart\n .trim()\n .split(/[,\\s]+/)\n .filter(Boolean);\n\n if (parts.length < 3) return null;\n\n // Parse hue (can have units)\n let h = parseFloat(parts[0]);\n const hueStr = parts[0].toLowerCase();\n if (hueStr.endsWith('turn')) h = parseFloat(hueStr) * 360;\n else if (hueStr.endsWith('rad')) h = (parseFloat(hueStr) * 180) / Math.PI;\n else if (hueStr.endsWith('deg')) h = parseFloat(hueStr);\n\n // Parse saturation and lightness (percentages)\n const parsePercent = (val: string): number => {\n const num = parseFloat(val);\n return val.includes('%') ? num / 100 : num;\n };\n const s = Math.max(0, Math.min(1, parsePercent(parts[1])));\n const l = Math.max(0, Math.min(1, parsePercent(parts[2])));\n\n // OKHSL to sRGB conversion\n const [r, g, b] = okhslToSrgbInternal(h, s, l);\n\n // Convert to 0-255 range\n const r255 = Math.round(Math.max(0, Math.min(1, r)) * 255);\n const g255 = Math.round(Math.max(0, Math.min(1, g)) * 255);\n const b255 = Math.round(Math.max(0, Math.min(1, b)) * 255);\n\n if (alphaPart) {\n const alpha = parseFloat(alphaPart.trim());\n return `rgba(${r255}, ${g255}, ${b255}, ${alpha})`;\n }\n\n return `rgb(${r255} ${g255} ${b255})`;\n}\n"],"mappings":";AAOA,MAAM,iBAAyB;CAC7B;EAAC;EAAK;EAAoB;EAAmB;CAC7C;EAAC;EAAK;EAAqB;EAAoB;CAC/C;EAAC;EA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AW,MAAM,GAAG;CAC5B,MAAM,SAAS,MAAM,GAAG,aAAa;AACrC,KAAI,OAAO,SAAS,OAAO,CAAE,KAAI,WAAW,OAAO,GAAG;UAC7C,OAAO,SAAS,MAAM,CAAE,KAAK,WAAW,OAAO,GAAG,MAAO,KAAK;UAC9D,OAAO,SAAS,MAAM,CAAE,KAAI,WAAW,OAAO;CAGvD,MAAM,gBAAgB,QAAwB;EAC5C,MAAM,MAAM,WAAW,IAAI;AAC3B,SAAO,IAAI,SAAS,IAAI,GAAG,MAAM,MAAM;;CAEzC,MAAM,IAAI,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,aAAa,MAAM,GAAG,CAAC,CAAC;CAC1D,MAAM,IAAI,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,aAAa,MAAM,GAAG,CAAC,CAAC;CAG1D,MAAM,CAAC,GAAG,GAAG,KAAK,oBAAoB,GAAG,GAAG,EAAE;CAG9C,MAAM,OAAO,KAAK,MAAM,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,EAAE,CAAC,GAAG,IAAI;CAC1D,MAAM,OAAO,KAAK,MAAM,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,EAAE,CAAC,GAAG,IAAI;CAC1D,MAAM,OAAO,KAAK,MAAM,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,EAAE,CAAC,GAAG,IAAI;AAE1D,KAAI,UAEF,QAAO,QAAQ,KAAK,IAAI,KAAK,IAAI,KAAK,IADxB,WAAW,UAAU,MAAM,CAAC,CACM;AAGlD,QAAO,OAAO,KAAK,GAAG,KAAK,GAAG,KAAK"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { CSSProperties } from "./css-types.js";
|
|
2
|
+
import { Tokens } from "../types.js";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/process-tokens.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Convert HSL to RGB (sRGB).
|
|
7
|
+
* Algorithm from: https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative
|
|
8
|
+
* Same as used in CSS Color 4 spec.
|
|
9
|
+
*
|
|
10
|
+
* @param h - Hue in degrees (0-360)
|
|
11
|
+
* @param s - Saturation (0-1)
|
|
12
|
+
* @param l - Lightness (0-1)
|
|
13
|
+
* @returns RGB values in 0-255 range (may have fractional values)
|
|
14
|
+
*/
|
|
15
|
+
declare function hslToRgbValues(h: number, s: number, l: number): [number, number, number];
|
|
16
|
+
/**
|
|
17
|
+
* Process tokens object into inline style properties.
|
|
18
|
+
* - $name -> --name with parsed value
|
|
19
|
+
* - #name -> --name-color AND --name-color-rgb with parsed values
|
|
20
|
+
*
|
|
21
|
+
* @param tokens - The tokens object to process
|
|
22
|
+
* @returns CSSProperties object or undefined if no tokens to process
|
|
23
|
+
*/
|
|
24
|
+
declare function processTokens(tokens: Tokens | undefined): CSSProperties | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Stringify tokens for memoization key.
|
|
27
|
+
*/
|
|
28
|
+
declare function stringifyTokens(tokens: Tokens | undefined): string;
|
|
29
|
+
//#endregion
|
|
30
|
+
export { hslToRgbValues, processTokens, stringifyTokens };
|
|
31
|
+
//# sourceMappingURL=process-tokens.d.ts.map
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { okhslToRgb } from "./okhsl-to-rgb.js";
|
|
2
|
+
import { getRgbValuesFromRgbaString, hexToRgb, normalizeColorTokenValue, parseStyle } from "./styles.js";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/process-tokens.ts
|
|
5
|
+
/**
|
|
6
|
+
* Parse HSL values from an hsl()/hsla() string.
|
|
7
|
+
* Supports both comma-separated (legacy) and space-separated (modern) syntax:
|
|
8
|
+
* hsl(200, 40%, 50%)
|
|
9
|
+
* hsl(200 40% 50%)
|
|
10
|
+
* hsl(200 40% 50% / 0.5)
|
|
11
|
+
*
|
|
12
|
+
* Returns [h, s, l] where h is 0-360, s and l are 0-1, or null if parsing fails.
|
|
13
|
+
*/
|
|
14
|
+
function parseHslValues(str) {
|
|
15
|
+
const match = str.match(/hsla?\(([^)]+)\)/i);
|
|
16
|
+
if (!match) return null;
|
|
17
|
+
const [colorPart] = match[1].trim().split("/");
|
|
18
|
+
const parts = colorPart.trim().split(/[,\s]+/).filter(Boolean);
|
|
19
|
+
if (parts.length < 3) return null;
|
|
20
|
+
let h = parseFloat(parts[0]);
|
|
21
|
+
const hueStr = parts[0].toLowerCase();
|
|
22
|
+
if (hueStr.endsWith("turn")) h = parseFloat(hueStr) * 360;
|
|
23
|
+
else if (hueStr.endsWith("rad")) h = parseFloat(hueStr) * 180 / Math.PI;
|
|
24
|
+
else if (hueStr.endsWith("grad")) h = parseFloat(hueStr) * .9;
|
|
25
|
+
h = h % 360;
|
|
26
|
+
if (h < 0) h += 360;
|
|
27
|
+
const parsePercent = (val) => {
|
|
28
|
+
const num = parseFloat(val);
|
|
29
|
+
return val.includes("%") ? num / 100 : num;
|
|
30
|
+
};
|
|
31
|
+
const s = Math.max(0, Math.min(1, parsePercent(parts[1])));
|
|
32
|
+
const l = Math.max(0, Math.min(1, parsePercent(parts[2])));
|
|
33
|
+
return [
|
|
34
|
+
h,
|
|
35
|
+
s,
|
|
36
|
+
l
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Convert HSL to RGB (sRGB).
|
|
41
|
+
* Algorithm from: https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative
|
|
42
|
+
* Same as used in CSS Color 4 spec.
|
|
43
|
+
*
|
|
44
|
+
* @param h - Hue in degrees (0-360)
|
|
45
|
+
* @param s - Saturation (0-1)
|
|
46
|
+
* @param l - Lightness (0-1)
|
|
47
|
+
* @returns RGB values in 0-255 range (may have fractional values)
|
|
48
|
+
*/
|
|
49
|
+
function hslToRgbValues(h, s, l) {
|
|
50
|
+
const a = s * Math.min(l, 1 - l);
|
|
51
|
+
const f = (n) => {
|
|
52
|
+
const k = (n + h / 30) % 12;
|
|
53
|
+
return l - a * Math.max(-1, Math.min(k - 3, 9 - k, 1));
|
|
54
|
+
};
|
|
55
|
+
return [
|
|
56
|
+
f(0) * 255,
|
|
57
|
+
f(8) * 255,
|
|
58
|
+
f(4) * 255
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Format a number to a string with up to 1 decimal place, removing trailing zeros.
|
|
63
|
+
*/
|
|
64
|
+
function formatRgbComponent(n) {
|
|
65
|
+
return parseFloat(n.toFixed(1)).toString();
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Extract RGB triplet from a color value.
|
|
69
|
+
* Returns the RGB values as a space-separated string (e.g., "255 128 0")
|
|
70
|
+
* or a CSS variable reference for token colors.
|
|
71
|
+
*/
|
|
72
|
+
function extractRgbValue(colorValue, parsedOutput) {
|
|
73
|
+
const varMatch = parsedOutput.match(/var\(--([a-z0-9-]+)-color\)/);
|
|
74
|
+
if (varMatch) return `var(--${varMatch[1]}-color-rgb)`;
|
|
75
|
+
if (parsedOutput.startsWith("rgb(")) {
|
|
76
|
+
const rgbValues = getRgbValuesFromRgbaString(parsedOutput);
|
|
77
|
+
if (rgbValues && rgbValues.length >= 3) return rgbValues.join(" ");
|
|
78
|
+
}
|
|
79
|
+
if (parsedOutput.startsWith("hsl(") || parsedOutput.startsWith("hsla(") || colorValue.startsWith("hsl(") || colorValue.startsWith("hsla(")) {
|
|
80
|
+
const hslValues = parseHslValues(parsedOutput) || parseHslValues(colorValue);
|
|
81
|
+
if (hslValues) {
|
|
82
|
+
const [r, g, b] = hslToRgbValues(hslValues[0], hslValues[1], hslValues[2]);
|
|
83
|
+
return `${formatRgbComponent(r)} ${formatRgbComponent(g)} ${formatRgbComponent(b)}`;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (parsedOutput.startsWith("okhsl(") || colorValue.startsWith("okhsl(")) {
|
|
87
|
+
const rgbResult = okhslToRgb(parsedOutput) || okhslToRgb(colorValue);
|
|
88
|
+
if (rgbResult) {
|
|
89
|
+
const rgbValues = getRgbValuesFromRgbaString(rgbResult);
|
|
90
|
+
if (rgbValues && rgbValues.length >= 3) return rgbValues.join(" ");
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (colorValue.startsWith("#") && /^#[0-9a-fA-F]{3,8}$/.test(colorValue)) {
|
|
94
|
+
const rgbResult = hexToRgb(colorValue);
|
|
95
|
+
if (rgbResult) {
|
|
96
|
+
const rgbValues = getRgbValuesFromRgbaString(rgbResult);
|
|
97
|
+
if (rgbValues && rgbValues.length >= 3) return rgbValues.join(" ");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return parsedOutput;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Check if a value is a valid token value (string, number, or boolean - not object).
|
|
104
|
+
* Returns false for `false` values (they mean "skip this token").
|
|
105
|
+
*/
|
|
106
|
+
function isValidTokenValue(value) {
|
|
107
|
+
if (value === void 0 || value === null || value === false) return false;
|
|
108
|
+
if (typeof value === "object") {
|
|
109
|
+
console.warn("CubeUIKit: Object values are not allowed in tokens prop. Tokens do not support state-based styling. Use a primitive value instead.");
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Process a single token value through the tasty parser.
|
|
116
|
+
* Numbers are converted to strings; 0 stays as "0".
|
|
117
|
+
*/
|
|
118
|
+
function processTokenValue(value) {
|
|
119
|
+
if (typeof value === "number") {
|
|
120
|
+
if (value === 0) return "0";
|
|
121
|
+
return parseStyle(String(value)).output;
|
|
122
|
+
}
|
|
123
|
+
return parseStyle(value).output;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Process tokens object into inline style properties.
|
|
127
|
+
* - $name -> --name with parsed value
|
|
128
|
+
* - #name -> --name-color AND --name-color-rgb with parsed values
|
|
129
|
+
*
|
|
130
|
+
* @param tokens - The tokens object to process
|
|
131
|
+
* @returns CSSProperties object or undefined if no tokens to process
|
|
132
|
+
*/
|
|
133
|
+
function processTokens(tokens) {
|
|
134
|
+
if (!tokens) return;
|
|
135
|
+
const keys = Object.keys(tokens);
|
|
136
|
+
if (keys.length === 0) return;
|
|
137
|
+
let result;
|
|
138
|
+
for (const key of keys) {
|
|
139
|
+
const value = tokens[key];
|
|
140
|
+
if (!isValidTokenValue(value)) continue;
|
|
141
|
+
if (key.startsWith("$")) {
|
|
142
|
+
const propName = `--${key.slice(1)}`;
|
|
143
|
+
const processedValue = processTokenValue(value === true ? "" : value);
|
|
144
|
+
if (!result) result = {};
|
|
145
|
+
result[propName] = processedValue;
|
|
146
|
+
} else if (key.startsWith("#")) {
|
|
147
|
+
const colorName = key.slice(1);
|
|
148
|
+
const effectiveValue = normalizeColorTokenValue(value);
|
|
149
|
+
if (effectiveValue === null) continue;
|
|
150
|
+
const originalValue = typeof effectiveValue === "number" ? String(effectiveValue) : effectiveValue;
|
|
151
|
+
const lowerValue = originalValue.toLowerCase();
|
|
152
|
+
const processedValue = processTokenValue(effectiveValue);
|
|
153
|
+
if (!result) result = {};
|
|
154
|
+
result[`--${colorName}-color`] = processedValue;
|
|
155
|
+
if (/^#current(?:\.|$)/i.test(lowerValue)) continue;
|
|
156
|
+
result[`--${colorName}-color-rgb`] = extractRgbValue(originalValue, processedValue);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Stringify tokens for memoization key.
|
|
163
|
+
*/
|
|
164
|
+
function stringifyTokens(tokens) {
|
|
165
|
+
if (!tokens) return "";
|
|
166
|
+
return JSON.stringify(tokens);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
//#endregion
|
|
170
|
+
export { hslToRgbValues, processTokens, stringifyTokens };
|
|
171
|
+
//# sourceMappingURL=process-tokens.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-tokens.js","names":[],"sources":["../../src/utils/process-tokens.ts"],"sourcesContent":["import type { Tokens, TokenValue } from '../types';\n\nimport type { CSSProperties } from './css-types';\n\nimport { okhslToRgb } from './okhsl-to-rgb';\nimport {\n getRgbValuesFromRgbaString,\n hexToRgb,\n normalizeColorTokenValue,\n parseStyle,\n} from './styles';\n\nconst devMode = process.env.NODE_ENV !== 'production';\n\n/**\n * Parse HSL values from an hsl()/hsla() string.\n * Supports both comma-separated (legacy) and space-separated (modern) syntax:\n * hsl(200, 40%, 50%)\n * hsl(200 40% 50%)\n * hsl(200 40% 50% / 0.5)\n *\n * Returns [h, s, l] where h is 0-360, s and l are 0-1, or null if parsing fails.\n */\nfunction parseHslValues(str: string): [number, number, number] | null {\n const match = str.match(/hsla?\\(([^)]+)\\)/i);\n if (!match) return null;\n\n const inner = match[1].trim();\n // Split by slash first (for alpha), then handle color components\n const [colorPart] = inner.split('/');\n // Split by comma or whitespace\n const parts = colorPart\n .trim()\n .split(/[,\\s]+/)\n .filter(Boolean);\n\n if (parts.length < 3) return null;\n\n // Parse hue (can be unitless degrees, deg, turn, rad, or grad)\n let h = parseFloat(parts[0]);\n const hueStr = parts[0].toLowerCase();\n if (hueStr.endsWith('turn')) {\n h = parseFloat(hueStr) * 360;\n } else if (hueStr.endsWith('rad')) {\n h = (parseFloat(hueStr) * 180) / Math.PI;\n } else if (hueStr.endsWith('grad')) {\n h = parseFloat(hueStr) * 0.9; // 400 grad = 360 deg\n }\n // deg or unitless are already in degrees\n\n // Normalize hue to 0-360 range\n h = h % 360;\n if (h < 0) h += 360;\n\n // Parse saturation and lightness (percentages)\n const parsePercent = (val: string): number => {\n const num = parseFloat(val);\n return val.includes('%') ? num / 100 : num;\n };\n\n const s = Math.max(0, Math.min(1, parsePercent(parts[1])));\n const l = Math.max(0, Math.min(1, parsePercent(parts[2])));\n\n return [h, s, l];\n}\n\n/**\n * Convert HSL to RGB (sRGB).\n * Algorithm from: https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative\n * Same as used in CSS Color 4 spec.\n *\n * @param h - Hue in degrees (0-360)\n * @param s - Saturation (0-1)\n * @param l - Lightness (0-1)\n * @returns RGB values in 0-255 range (may have fractional values)\n */\nexport function hslToRgbValues(\n h: number,\n s: number,\n l: number,\n): [number, number, number] {\n const a = s * Math.min(l, 1 - l);\n\n const f = (n: number): number => {\n const k = (n + h / 30) % 12;\n return l - a * Math.max(-1, Math.min(k - 3, 9 - k, 1));\n };\n\n // Convert 0-1 range to 0-255\n return [f(0) * 255, f(8) * 255, f(4) * 255];\n}\n\n/**\n * Format a number to a string with up to 1 decimal place, removing trailing zeros.\n */\nfunction formatRgbComponent(n: number): string {\n return parseFloat(n.toFixed(1)).toString();\n}\n\n/**\n * Extract RGB triplet from a color value.\n * Returns the RGB values as a space-separated string (e.g., \"255 128 0\")\n * or a CSS variable reference for token colors.\n */\nfunction extractRgbValue(colorValue: string, parsedOutput: string): string {\n // If the parsed output references a color variable, use the -rgb variant\n const varMatch = parsedOutput.match(/var\\(--([a-z0-9-]+)-color\\)/);\n if (varMatch) {\n return `var(--${varMatch[1]}-color-rgb)`;\n }\n\n // For rgb(...) values, extract the triplet\n if (parsedOutput.startsWith('rgb(')) {\n const rgbValues = getRgbValuesFromRgbaString(parsedOutput);\n if (rgbValues && rgbValues.length >= 3) {\n return rgbValues.join(' ');\n }\n }\n\n // For hsl(...) values, convert to RGB triplet\n if (\n parsedOutput.startsWith('hsl(') ||\n parsedOutput.startsWith('hsla(') ||\n colorValue.startsWith('hsl(') ||\n colorValue.startsWith('hsla(')\n ) {\n // Try parsedOutput first, then original colorValue\n const hslValues =\n parseHslValues(parsedOutput) || parseHslValues(colorValue);\n if (hslValues) {\n const [r, g, b] = hslToRgbValues(\n hslValues[0],\n hslValues[1],\n hslValues[2],\n );\n return `${formatRgbComponent(r)} ${formatRgbComponent(g)} ${formatRgbComponent(b)}`;\n }\n }\n\n // For okhsl(...) values, convert to RGB triplet\n if (parsedOutput.startsWith('okhsl(') || colorValue.startsWith('okhsl(')) {\n // Try parsedOutput first, then original colorValue\n const rgbResult = okhslToRgb(parsedOutput) || okhslToRgb(colorValue);\n if (rgbResult) {\n const rgbValues = getRgbValuesFromRgbaString(rgbResult);\n if (rgbValues && rgbValues.length >= 3) {\n return rgbValues.join(' ');\n }\n }\n }\n\n // For hex values, convert to RGB triplet\n if (colorValue.startsWith('#') && /^#[0-9a-fA-F]{3,8}$/.test(colorValue)) {\n const rgbResult = hexToRgb(colorValue);\n if (rgbResult) {\n const rgbValues = getRgbValuesFromRgbaString(rgbResult);\n if (rgbValues && rgbValues.length >= 3) {\n return rgbValues.join(' ');\n }\n }\n }\n\n // Fallback: return the parsed output (may not be ideal but covers edge cases)\n return parsedOutput;\n}\n\n/**\n * Check if a value is a valid token value (string, number, or boolean - not object).\n * Returns false for `false` values (they mean \"skip this token\").\n */\nfunction isValidTokenValue(\n value: unknown,\n): value is Exclude<TokenValue, undefined | null | false> {\n if (value === undefined || value === null || value === false) {\n return false;\n }\n\n if (typeof value === 'object') {\n if (devMode) {\n console.warn(\n 'CubeUIKit: Object values are not allowed in tokens prop. ' +\n 'Tokens do not support state-based styling. Use a primitive value instead.',\n );\n }\n return false;\n }\n\n return (\n typeof value === 'string' ||\n typeof value === 'number' ||\n typeof value === 'boolean'\n );\n}\n\n/**\n * Process a single token value through the tasty parser.\n * Numbers are converted to strings; 0 stays as \"0\".\n */\nfunction processTokenValue(value: string | number): string {\n if (typeof value === 'number') {\n // 0 should remain as \"0\", not converted to any unit\n if (value === 0) {\n return '0';\n }\n return parseStyle(String(value)).output;\n }\n return parseStyle(value).output;\n}\n\n/**\n * Process tokens object into inline style properties.\n * - $name -> --name with parsed value\n * - #name -> --name-color AND --name-color-rgb with parsed values\n *\n * @param tokens - The tokens object to process\n * @returns CSSProperties object or undefined if no tokens to process\n */\nexport function processTokens(\n tokens: Tokens | undefined,\n): CSSProperties | undefined {\n if (!tokens) {\n return undefined;\n }\n\n const keys = Object.keys(tokens);\n if (keys.length === 0) {\n return undefined;\n }\n\n let result: Record<string, string> | undefined;\n\n for (const key of keys) {\n const value = tokens[key as keyof Tokens];\n\n // Skip undefined/null values\n if (!isValidTokenValue(value)) {\n continue;\n }\n\n if (key.startsWith('$')) {\n // Custom property token: $name -> --name\n const propName = `--${key.slice(1)}`;\n // Boolean true for custom properties converts to empty string (valid CSS value)\n const effectiveValue = value === true ? '' : value;\n const processedValue = processTokenValue(effectiveValue);\n\n if (!result) result = {};\n result[propName] = processedValue;\n } else if (key.startsWith('#')) {\n // Color token: #name -> --name-color and --name-color-rgb\n const colorName = key.slice(1);\n\n // Normalize color token value (true → 'transparent', false is already filtered by isValidTokenValue)\n const effectiveValue = normalizeColorTokenValue(value);\n // Skip if normalized to null (shouldn't happen since false is filtered by isValidTokenValue)\n if (effectiveValue === null) continue;\n\n const originalValue =\n typeof effectiveValue === 'number'\n ? String(effectiveValue)\n : effectiveValue;\n const lowerValue = originalValue.toLowerCase();\n const processedValue = processTokenValue(effectiveValue);\n\n if (!result) result = {};\n result[`--${colorName}-color`] = processedValue;\n\n // Skip RGB generation for #current values (currentcolor is dynamic, cannot extract RGB)\n // Match only #current or #current.opacity, not #current-theme or #currently-used\n if (/^#current(?:\\.|$)/i.test(lowerValue)) {\n continue;\n }\n\n result[`--${colorName}-color-rgb`] = extractRgbValue(\n originalValue,\n processedValue,\n );\n }\n }\n\n return result as CSSProperties | undefined;\n}\n\n/**\n * Stringify tokens for memoization key.\n */\nexport function stringifyTokens(tokens: Tokens | undefined): string {\n if (!tokens) return '';\n return JSON.stringify(tokens);\n}\n"],"mappings":";;;;;;;;;;;;;AAuBA,SAAS,eAAe,KAA8C;CACpE,MAAM,QAAQ,IAAI,MAAM,oBAAoB;AAC5C,KAAI,CAAC,MAAO,QAAO;CAInB,MAAM,CAAC,aAFO,MAAM,GAAG,MAAM,CAEH,MAAM,IAAI;CAEpC,MAAM,QAAQ,UACX,MAAM,CACN,MAAM,SAAS,CACf,OAAO,QAAQ;AAElB,KAAI,MA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CAAC,CAAC;;AAEnC,QAAO,WAAW,MAAM,CAAC;;;;;;;;;;AAW3B,SAAgB,cACd,QAC2B;AAC3B,KAAI,CAAC,OACH;CAGF,MAAM,OAAO,OAAO,KAAK,OAAO;AAChC,KAAI,KAAK,WAAW,EAClB;CAGF,IAAI;AAEJ,MAAK,MAAM,OAAO,MAAM;EACtB,MAAM,QAAQ,OAAO;AAGrB,MAAI,CAAC,kBAAkB,MAAM,CAC3B;AAGF,MAAI,IAAI,WAAW,IAAI,EAAE;GAEvB,MAAM,WAAW,KAAK,IAAI,MAAM,EAAE;GAGlC,MAAM,iBAAiB,kBADA,UAAU,OAAO,KAAK,MACW;AAExD,OAAI,CAAC,OAAQ,UAAS,EAAE;AACxB,UAAO,YAAY;aACV,IAAI,WAAW,IAAI,EAAE;GAE9B,MAAM,YAAY,IAAI,MAAM,EAAE;GAG9B,MAAM,iBAAiB,yBAAyB,MAAM;AAEtD,OAAI,mBAAmB,KAAM;GAE7B,MAAM,gBACJ,OAAO,mBAAmB,WACtB,OAAO,eAAe,GACtB;GACN,MAAM,aAAa,cAAc,aAAa;GAC9C,MAAM,iBAAiB,kBAAkB,eAAe;AAExD,OAAI,CAAC,OAAQ,UAAS,EAAE;AACxB,UAAO,KAAK,UAAU,WAAW;AAIjC,OAAI,qBAAqB,KAAK,WAAW,CACvC;AAGF,UAAO,KAAK,UAAU,eAAe,gBACnC,eACA,eACD;;;AAIL,QAAO;;;;;AAMT,SAAgB,gBAAgB,QAAoC;AAClE,KAAI,CAAC,OAAQ,QAAO;AACpB,QAAO,KAAK,UAAU,OAAO"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Styles } from "../styles/types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/resolve-recipes.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Resolve all `recipe` style properties in a styles object.
|
|
6
|
+
*
|
|
7
|
+
* Handles both top-level and sub-element recipe references.
|
|
8
|
+
* Returns the same object reference if no recipes are present anywhere
|
|
9
|
+
* (zero overhead for the common case).
|
|
10
|
+
*
|
|
11
|
+
* @param styles - The styles object potentially containing `recipe` keys
|
|
12
|
+
* @returns Resolved styles with recipe values merged in, or the original object if unchanged
|
|
13
|
+
*/
|
|
14
|
+
declare function resolveRecipes(styles: Styles): Styles;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { resolveRecipes };
|
|
17
|
+
//# sourceMappingURL=resolve-recipes.d.ts.map
|