@tbela99/css-parser 1.5.0 → 1.6.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/CHANGELOG.md +33 -0
- package/README.md +7 -7
- package/dist/index-umd-web.js +5562 -4731
- package/dist/index.cjs +5565 -4736
- package/dist/index.d.ts +51 -37
- package/dist/lib/ast/expand.js +21 -7
- package/dist/lib/ast/features/calc.js +17 -55
- package/dist/lib/ast/features/if.js +7 -3
- package/dist/lib/ast/features/inlinecssvariables.js +6 -5
- package/dist/lib/ast/features/prefix.js +41 -44
- package/dist/lib/ast/features/shorthand.js +10 -5
- package/dist/lib/ast/features/transform.js +1 -1
- package/dist/lib/ast/math/expression.js +190 -32
- package/dist/lib/ast/minify.js +37 -14
- package/dist/lib/ast/node.js +5 -3
- package/dist/lib/ast/transform/compute.js +63 -4
- package/dist/lib/ast/transform/minify.js +6 -6
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.js +46 -18
- package/dist/lib/ast/walk.js +28 -4
- package/dist/lib/fs/resolve.js +11 -2
- package/dist/lib/parser/declaration/list.js +22 -7
- package/dist/lib/parser/declaration/map.js +26 -8
- package/dist/lib/parser/declaration/set.js +3 -1
- package/dist/lib/parser/linesmap.js +2 -4
- package/dist/lib/parser/parse.js +388 -227
- package/dist/lib/parser/tokenize.js +1478 -818
- package/dist/lib/parser/utils/at-rule-container.js +28 -196
- package/dist/lib/parser/utils/at-rule-generic.js +6 -7
- package/dist/lib/parser/utils/at-rule-import.js +17 -13
- package/dist/lib/parser/utils/at-rule-media.js +36 -19
- package/dist/lib/parser/utils/at-rule-support.js +19 -9
- package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
- package/dist/lib/parser/utils/at-rule.js +0 -18
- package/dist/lib/parser/utils/declaration.js +30 -95
- package/dist/lib/parser/utils/hash.js +4 -4
- package/dist/lib/parser/utils/selector.js +89 -144
- package/dist/lib/parser/utils/text.js +4 -2
- package/dist/lib/renderer/render.js +102 -72
- package/dist/lib/renderer/sourcemap/sourcemap.js +2 -5
- package/dist/lib/syntax/color/a98rgb.js +14 -4
- package/dist/lib/syntax/color/cmyk.js +10 -18
- package/dist/lib/syntax/color/color-mix.js +21 -34
- package/dist/lib/syntax/color/color.js +32 -49
- package/dist/lib/syntax/color/hsl.js +15 -15
- package/dist/lib/syntax/color/hwb.js +18 -17
- package/dist/lib/syntax/color/lab.js +17 -17
- package/dist/lib/syntax/color/lch.js +13 -13
- package/dist/lib/syntax/color/oklab.js +10 -11
- package/dist/lib/syntax/color/oklch.js +15 -18
- package/dist/lib/syntax/color/p3.js +18 -12
- package/dist/lib/syntax/color/prophotorgb.js +26 -28
- package/dist/lib/syntax/color/rec2020.js +8 -4
- package/dist/lib/syntax/color/relative-color.js +26 -10
- package/dist/lib/syntax/color/srgb.js +5 -4
- package/dist/lib/syntax/color/utils/distance.js +1 -1
- package/dist/lib/syntax/color/xyz.js +2 -2
- package/dist/lib/syntax/color/xyzd50.js +2 -2
- package/dist/lib/syntax/constants.js +12 -2
- package/dist/lib/syntax/syntax.js +32 -108
- package/dist/lib/validation/config.json.js +3 -0
- package/dist/lib/validation/match.js +29 -26
- package/dist/node.js +16 -9
- package/dist/types.d.ts +5 -1
- package/dist/types.js +4 -0
- package/dist/utils/sync.js +22 -1
- package/dist/web.js +13 -4
- package/package.json +1 -1
- package/llms.txt +0 -80
|
@@ -3,12 +3,13 @@ import { reduceHexValue } from '../syntax/color/hex.js';
|
|
|
3
3
|
import { EnumToken, ColorType } from '../ast/types.js';
|
|
4
4
|
import { expand } from '../ast/expand.js';
|
|
5
5
|
import { SourceMap } from './sourcemap/sourcemap.js';
|
|
6
|
-
import { pseudoElements, urlTokenMatcher, PARENT, tokensfuncSet,
|
|
7
|
-
import { minifyNumber, reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor,
|
|
6
|
+
import { pseudoElements, anglePrecision, urlTokenMatcher, PARENT, tokensfuncSet, LOCSTA, LOCSRCID } from '../syntax/constants.js';
|
|
7
|
+
import { minifyNumber, toPrecisionAngle, reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor, isWhiteSpace, toPrecisionValue } from '../syntax/syntax.js';
|
|
8
8
|
import { equalsIgnoreCase } from '../parser/utils/text.js';
|
|
9
9
|
import { toDegrees } from '../parser/utils/angle.js';
|
|
10
10
|
import { LineMap } from '../parser/linesmap.js';
|
|
11
11
|
import { dirname } from '../fs/resolve.js';
|
|
12
|
+
import { SourceFile } from '../parser/source.js';
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* render ast
|
|
@@ -114,7 +115,7 @@ function doRender(data, options = {}, mapping) {
|
|
|
114
115
|
source = options.sourcesMap.get(sourceId);
|
|
115
116
|
sourcemap.addSourceContent(source.id, source.getFileName(), source.getContent());
|
|
116
117
|
}
|
|
117
|
-
sourcemap.add(
|
|
118
|
+
sourcemap.add(sourcemaps.maps);
|
|
118
119
|
result.map = sourcemap;
|
|
119
120
|
if (options.sourcemap === "inline") {
|
|
120
121
|
result.code += `\n/*# sourceMappingURL=${result.map.toUrl()} */`;
|
|
@@ -136,43 +137,33 @@ function doRender(data, options = {}, mapping) {
|
|
|
136
137
|
*/
|
|
137
138
|
function updateSourceMap(node, options, cache, sourcemaps, sourceLocation, linesMap, str) {
|
|
138
139
|
let offset = 0;
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
if (str.charAt(offset) == options.indent) {
|
|
145
|
-
offset += options.indent.length;
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
break;
|
|
140
|
+
// eat leanding whitespace
|
|
141
|
+
while (offset < str.length && isWhiteSpace(str.charCodeAt(offset))) {
|
|
142
|
+
offset++;
|
|
149
143
|
}
|
|
150
144
|
if (offset > 0) {
|
|
151
|
-
move(sourceLocation, linesMap, str
|
|
145
|
+
move(sourceLocation, linesMap, str, 0, offset + 1);
|
|
152
146
|
}
|
|
153
|
-
if (node[
|
|
154
|
-
[
|
|
155
|
-
EnumToken.RuleNodeType,
|
|
156
|
-
EnumToken.AtRuleNodeType,
|
|
157
|
-
EnumToken.KeyframesRuleNodeType,
|
|
158
|
-
EnumToken.KeyframesAtRuleNodeType,
|
|
159
|
-
].includes(node.typ)) {
|
|
160
|
-
const source = options.sourcesMap.get(node[LOC].srcId);
|
|
147
|
+
if (node[LOCSTA] != null) {
|
|
148
|
+
const source = options.sourcesMap.get(node[LOCSRCID]);
|
|
161
149
|
const inputSourceMap = source.getInputSourceMap();
|
|
162
|
-
const offsets = source.getOffsets(node[
|
|
150
|
+
const offsets = source.getOffsets(node[LOCSTA]);
|
|
163
151
|
const [newLine, newColumn] = linesMap.getOffsets(sourceLocation.end);
|
|
164
152
|
let records = null;
|
|
165
|
-
let srcId = node[
|
|
153
|
+
let srcId = node[LOCSRCID];
|
|
166
154
|
let sourceFileName = source.getFileName() || null;
|
|
167
|
-
source.getContent() || null;
|
|
155
|
+
let sourceContent; // = (source.getContent() as string) || null;
|
|
168
156
|
if (inputSourceMap != null && (records = inputSourceMap.find(offsets[0], offsets[1])) != null) {
|
|
157
|
+
let newId = null;
|
|
169
158
|
for (const record of records) {
|
|
159
|
+
newId = null;
|
|
170
160
|
// @ts-ignore
|
|
171
161
|
sourceFileName = record[0] || null;
|
|
172
162
|
// @ts-ignore
|
|
173
163
|
offsets[0] = record[1];
|
|
174
164
|
// @ts-ignore
|
|
175
165
|
offsets[1] = record[2];
|
|
166
|
+
sourceContent = record[3] || null;
|
|
176
167
|
if (sourceFileName != null && options.output != null && !sourceFileName.startsWith("data:")) {
|
|
177
168
|
if (cache[sourceFileName] == null) {
|
|
178
169
|
const absolute = options.resolve(dirname(options.output), options.cwd)
|
|
@@ -185,30 +176,46 @@ function updateSourceMap(node, options, cache, sourcemaps, sourceLocation, lines
|
|
|
185
176
|
}
|
|
186
177
|
sourceFileName = cache[sourceFileName];
|
|
187
178
|
}
|
|
179
|
+
for (const [id, file] of options.sourcesMap.entries()) {
|
|
180
|
+
if (file.getFileName() === sourceFileName) {
|
|
181
|
+
newId = id;
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
if (sourceFileName == null && file.getContent() === sourceContent) {
|
|
185
|
+
newId = id;
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (newId == null) {
|
|
190
|
+
const source = new SourceFile(sourceContent, [], sourceFileName);
|
|
191
|
+
options.sourcesMap.set(source.id, source);
|
|
192
|
+
newId = source.id;
|
|
193
|
+
}
|
|
194
|
+
srcId = newId;
|
|
188
195
|
if (!sourcemaps.sources.includes(srcId)) {
|
|
189
196
|
sourcemaps.sources.push(srcId);
|
|
190
197
|
}
|
|
191
|
-
sourcemaps.maps.push([newLine, newColumn, srcId,
|
|
198
|
+
sourcemaps.maps.push([newLine, newColumn, srcId, offsets[0], offsets[1]]);
|
|
192
199
|
}
|
|
193
200
|
}
|
|
194
201
|
else {
|
|
195
|
-
if (sourceFileName != null && options.output != null && !sourceFileName.startsWith("data:")) {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}
|
|
202
|
+
// if (sourceFileName != null && options.output != null && !sourceFileName.startsWith("data:")) {
|
|
203
|
+
// if (cache[sourceFileName] == null) {
|
|
204
|
+
// const absolute = options.resolve!(dirname(options.output as string), options.cwd as string)
|
|
205
|
+
// .absolute as string;
|
|
206
|
+
// const absoluteSourceFileName = options.resolve!(sourceFileName, options.cwd as string)
|
|
207
|
+
// .absolute as string;
|
|
208
|
+
// cache[sourceFileName] = options.resolve!(absoluteSourceFileName, absolute).relative as string;
|
|
209
|
+
// }
|
|
210
|
+
// sourceFileName = cache[sourceFileName] as string;
|
|
211
|
+
// }
|
|
205
212
|
if (!sourcemaps.sources.includes(srcId)) {
|
|
206
213
|
sourcemaps.sources.push(srcId);
|
|
207
214
|
}
|
|
208
|
-
sourcemaps.maps.push([newLine, newColumn, srcId,
|
|
215
|
+
sourcemaps.maps.push([newLine, newColumn, srcId, offsets[0], offsets[1]]);
|
|
209
216
|
}
|
|
210
217
|
}
|
|
211
|
-
move(sourceLocation, linesMap,
|
|
218
|
+
move(sourceLocation, linesMap, str, offset);
|
|
212
219
|
}
|
|
213
220
|
/**
|
|
214
221
|
* Update position
|
|
@@ -216,11 +223,12 @@ function updateSourceMap(node, options, cache, sourcemaps, sourceLocation, lines
|
|
|
216
223
|
* @param linesMap
|
|
217
224
|
* @param str
|
|
218
225
|
*/
|
|
219
|
-
function move(sourceLocation, linesMap, str) {
|
|
220
|
-
let i = 0;
|
|
226
|
+
function move(sourceLocation, linesMap, str, start, end) {
|
|
227
|
+
let i = start ?? 0;
|
|
228
|
+
let j = end ?? str.length;
|
|
221
229
|
let codepoint;
|
|
222
230
|
let char;
|
|
223
|
-
for (; i <
|
|
231
|
+
for (; i < j; i++) {
|
|
224
232
|
char = str.charAt(i);
|
|
225
233
|
codepoint = char.charCodeAt(0);
|
|
226
234
|
sourceLocation.end += char.length;
|
|
@@ -344,7 +352,6 @@ function renderAstNode(data, options, sourcemaps, sourceLocation, linesMap, erro
|
|
|
344
352
|
str = options.newLine + indentSub + str;
|
|
345
353
|
children += str;
|
|
346
354
|
if (sourcemaps != null && str !== "") {
|
|
347
|
-
move(sourceLocation, linesMap, str);
|
|
348
355
|
if (node.typ == EnumToken.DeclarationNodeType && recordDeclarationSourceMap) {
|
|
349
356
|
// if declaration is child of at-rule, then record it
|
|
350
357
|
// .rule {
|
|
@@ -352,15 +359,11 @@ function renderAstNode(data, options, sourcemaps, sourceLocation, linesMap, erro
|
|
|
352
359
|
// color: red;
|
|
353
360
|
// }
|
|
354
361
|
// }
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
...linesMap.getOffsets(sourceLocation.end - str.length + options.newLine.length + indentSub.length),
|
|
361
|
-
node[LOC].srcId,
|
|
362
|
-
...source.getOffsets(node[LOC].sta),
|
|
363
|
-
]);
|
|
362
|
+
// @ts-ignore
|
|
363
|
+
updateSourceMap(node, options, cache, sourcemaps, sourceLocation, linesMap, str);
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
move(sourceLocation, linesMap, str);
|
|
364
367
|
}
|
|
365
368
|
}
|
|
366
369
|
}
|
|
@@ -665,7 +668,9 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
665
668
|
// }
|
|
666
669
|
}
|
|
667
670
|
if (slice[i]?.typ === EnumToken.ColorTokenType) {
|
|
668
|
-
|
|
671
|
+
for (const token of reduceColorStops(slice.splice(i, slice.length - i))) {
|
|
672
|
+
slice.push(token);
|
|
673
|
+
}
|
|
669
674
|
}
|
|
670
675
|
}
|
|
671
676
|
break;
|
|
@@ -856,32 +861,45 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
856
861
|
}
|
|
857
862
|
const result = [];
|
|
858
863
|
if (form.length > 0) {
|
|
859
|
-
|
|
864
|
+
for (const token of form) {
|
|
865
|
+
result.push(token);
|
|
866
|
+
}
|
|
860
867
|
}
|
|
861
868
|
if (size.length > 0) {
|
|
862
869
|
if (result.length > 0) {
|
|
863
870
|
result.push({ typ: EnumToken.WhitespaceTokenType });
|
|
864
871
|
}
|
|
865
|
-
|
|
872
|
+
for (const token of size) {
|
|
873
|
+
result.push(token);
|
|
874
|
+
}
|
|
866
875
|
}
|
|
867
876
|
if (positions.length > 0) {
|
|
868
877
|
if (result.length > 0) {
|
|
869
878
|
result.push({ typ: EnumToken.WhitespaceTokenType });
|
|
870
879
|
}
|
|
871
|
-
result.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType }
|
|
880
|
+
result.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType });
|
|
881
|
+
for (const token of positions) {
|
|
882
|
+
result.push(token);
|
|
883
|
+
}
|
|
872
884
|
}
|
|
873
885
|
if (colorSpaceDef.length > 0) {
|
|
874
886
|
if (result.length > 0) {
|
|
875
887
|
result.push({ typ: EnumToken.WhitespaceTokenType });
|
|
876
888
|
}
|
|
877
|
-
|
|
889
|
+
for (const token of colorSpaceDef) {
|
|
890
|
+
result.push(token);
|
|
891
|
+
}
|
|
878
892
|
}
|
|
879
893
|
if (result.length > 0) {
|
|
880
894
|
result.push({ typ: EnumToken.CommaTokenType });
|
|
881
895
|
}
|
|
882
|
-
|
|
896
|
+
for (const token of reduceColorStops(slice.slice(i))) {
|
|
897
|
+
result.push(token);
|
|
898
|
+
}
|
|
883
899
|
slice.length = 0;
|
|
884
|
-
|
|
900
|
+
for (const token of result) {
|
|
901
|
+
slice.push(token);
|
|
902
|
+
}
|
|
885
903
|
}
|
|
886
904
|
break;
|
|
887
905
|
case "conic-gradient":
|
|
@@ -986,24 +1004,36 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
986
1004
|
if (angles.length > 0) {
|
|
987
1005
|
angles.push({ typ: EnumToken.WhitespaceTokenType });
|
|
988
1006
|
}
|
|
989
|
-
angles.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType }
|
|
1007
|
+
angles.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType });
|
|
1008
|
+
for (const position of positions) {
|
|
1009
|
+
angles.push(position);
|
|
1010
|
+
}
|
|
990
1011
|
}
|
|
991
1012
|
}
|
|
992
1013
|
if (angles.length > 0) {
|
|
993
|
-
|
|
1014
|
+
for (const angle of angles) {
|
|
1015
|
+
result.push(angle);
|
|
1016
|
+
}
|
|
1017
|
+
result.push({ typ: EnumToken.CommaTokenType });
|
|
994
1018
|
}
|
|
995
1019
|
if (colorSpaceDef.length > 0) {
|
|
996
1020
|
if (colorSpaceDef.length > 0) {
|
|
997
1021
|
if (result.length > 0) {
|
|
998
1022
|
result.push({ typ: EnumToken.WhitespaceTokenType });
|
|
999
1023
|
}
|
|
1000
|
-
|
|
1024
|
+
for (const token of colorSpaceDef) {
|
|
1025
|
+
result.push(token);
|
|
1026
|
+
}
|
|
1001
1027
|
}
|
|
1002
1028
|
result.push({ typ: EnumToken.CommaTokenType });
|
|
1003
1029
|
}
|
|
1004
|
-
|
|
1030
|
+
for (const token of reduceConicColorStops(slice.slice(i))) {
|
|
1031
|
+
result.push(token);
|
|
1032
|
+
}
|
|
1005
1033
|
slice.length = 0;
|
|
1006
|
-
|
|
1034
|
+
for (let j = 0; j < result.length; j++) {
|
|
1035
|
+
slice.push(result[j]);
|
|
1036
|
+
}
|
|
1007
1037
|
}
|
|
1008
1038
|
break;
|
|
1009
1039
|
}
|
|
@@ -1137,29 +1167,29 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1137
1167
|
const angle = getAngle(token);
|
|
1138
1168
|
let v;
|
|
1139
1169
|
let value = val + unit;
|
|
1140
|
-
for (const u of ["
|
|
1170
|
+
for (const u of ["deg", "turn", "rad", "grad"]) {
|
|
1141
1171
|
if (token.unit == u) {
|
|
1142
1172
|
continue;
|
|
1143
1173
|
}
|
|
1144
1174
|
switch (u) {
|
|
1145
|
-
case "
|
|
1146
|
-
v = minifyNumber(toPrecisionAngle(angle,
|
|
1147
|
-
if (v.length +
|
|
1175
|
+
case "deg":
|
|
1176
|
+
v = minifyNumber(toPrecisionAngle(angle * 360, anglePrecision, false).toFixed(anglePrecision));
|
|
1177
|
+
if (v.length + 3 < value.length) {
|
|
1148
1178
|
val = v;
|
|
1149
1179
|
unit = u;
|
|
1150
1180
|
value = v + u;
|
|
1151
1181
|
}
|
|
1152
1182
|
break;
|
|
1153
|
-
case "
|
|
1154
|
-
v = minifyNumber(toPrecisionAngle(angle
|
|
1155
|
-
if (v.length +
|
|
1183
|
+
case "turn":
|
|
1184
|
+
v = minifyNumber(toPrecisionAngle(angle, anglePrecision, false).toFixed(anglePrecision));
|
|
1185
|
+
if (v.length + 4 < value.length) {
|
|
1156
1186
|
val = v;
|
|
1157
1187
|
unit = u;
|
|
1158
1188
|
value = v + u;
|
|
1159
1189
|
}
|
|
1160
1190
|
break;
|
|
1161
1191
|
case "rad":
|
|
1162
|
-
v = minifyNumber(toPrecisionAngle(angle * (2 * Math.PI),
|
|
1192
|
+
v = minifyNumber(toPrecisionAngle(angle * (2 * Math.PI), anglePrecision, false).toFixed(anglePrecision));
|
|
1163
1193
|
if (v.length + 3 < value.length) {
|
|
1164
1194
|
val = v;
|
|
1165
1195
|
unit = u;
|
|
@@ -1167,7 +1197,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1167
1197
|
}
|
|
1168
1198
|
break;
|
|
1169
1199
|
case "grad":
|
|
1170
|
-
v = minifyNumber(toPrecisionAngle(angle * 400,
|
|
1200
|
+
v = minifyNumber(toPrecisionAngle(angle * 400, anglePrecision, false).toFixed(anglePrecision));
|
|
1171
1201
|
if (v.length + 4 < value.length) {
|
|
1172
1202
|
val = v;
|
|
1173
1203
|
unit = u;
|
|
@@ -107,15 +107,12 @@ class SourceMap {
|
|
|
107
107
|
this.sourcesContent[this.sourcesContent.length] = content || null;
|
|
108
108
|
}
|
|
109
109
|
/**
|
|
110
|
-
* Add
|
|
110
|
+
* Add multiple sourcemaps
|
|
111
111
|
* @param maps
|
|
112
112
|
* @throws
|
|
113
113
|
*/
|
|
114
|
-
add(
|
|
114
|
+
add(maps) {
|
|
115
115
|
let srcIndex;
|
|
116
|
-
if (typeof maps[0] === "number") {
|
|
117
|
-
maps = [maps];
|
|
118
|
-
}
|
|
119
116
|
for (let [newLine, newColumn, srcId, ln, col] of maps) {
|
|
120
117
|
const key = `${srcId}:${ln}:${col}:${newLine}:${newColumn}`;
|
|
121
118
|
if (this.keys.has(key)) {
|
|
@@ -3,12 +3,22 @@ import { multiplyMatrices } from './utils/matrix.js';
|
|
|
3
3
|
import { srgb2xyz } from './xyz.js';
|
|
4
4
|
|
|
5
5
|
function a98rgb2srgbvalues(r, g, b, a = null) {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
let values = a98rgb2la98(r, g, b);
|
|
7
|
+
values = la98rgb2xyz(values[0], values[1], values[2]);
|
|
8
|
+
values = xyz2srgb(values[0], values[1], values[2]);
|
|
9
|
+
if (a != null && a < 1) {
|
|
10
|
+
values.push(a);
|
|
11
|
+
}
|
|
12
|
+
return values;
|
|
8
13
|
}
|
|
9
14
|
function srgb2a98values(r, g, b, a = null) {
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
let values = srgb2xyz(r, g, b);
|
|
16
|
+
values = xyz2la98rgb(values[0], values[1], values[2]);
|
|
17
|
+
values = la98rgb2a98rgb(values[0], values[1], values[2]);
|
|
18
|
+
if (a != null && a < 1) {
|
|
19
|
+
values.push(a);
|
|
20
|
+
}
|
|
21
|
+
return values;
|
|
12
22
|
}
|
|
13
23
|
// a98-rgb functions
|
|
14
24
|
function a98rgb2la98(r, g, b, a = null) {
|
|
@@ -4,68 +4,60 @@ import { lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, hwb
|
|
|
4
4
|
import { hsl2srgbvalues } from './rgb.js';
|
|
5
5
|
|
|
6
6
|
function rgb2cmykToken(token) {
|
|
7
|
-
|
|
7
|
+
let components = rgb2srgbvalues(token);
|
|
8
8
|
if (components == null || components.length < 3) {
|
|
9
9
|
return null;
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
return cmyktoken(srgb2cmykvalues(...components));
|
|
11
|
+
return cmyktoken(srgb2cmykvalues(components[0], components[1], components[2], components[3]));
|
|
13
12
|
}
|
|
14
13
|
function hsl2cmykToken(token) {
|
|
15
|
-
|
|
14
|
+
let values = hsl2srgbvalues(token);
|
|
16
15
|
if (values == null) {
|
|
17
16
|
return null;
|
|
18
17
|
}
|
|
19
|
-
|
|
20
|
-
return cmyktoken(srgb2cmykvalues(...values));
|
|
18
|
+
return cmyktoken(srgb2cmykvalues(values[0], values[1], values[2], values[3]));
|
|
21
19
|
}
|
|
22
20
|
function hwb2cmykToken(token) {
|
|
23
21
|
const values = hwb2srgbvalues(token);
|
|
24
22
|
if (values == null) {
|
|
25
23
|
return null;
|
|
26
24
|
}
|
|
27
|
-
|
|
28
|
-
return cmyktoken(srgb2cmykvalues(...values));
|
|
25
|
+
return cmyktoken(srgb2cmykvalues(values[0], values[1], values[2], values[3]));
|
|
29
26
|
}
|
|
30
27
|
function lab2cmykToken(token) {
|
|
31
28
|
const components = lab2srgbvalues(token);
|
|
32
29
|
if (components == null || components.length < 3) {
|
|
33
30
|
return null;
|
|
34
31
|
}
|
|
35
|
-
|
|
36
|
-
return cmyktoken(srgb2cmykvalues(...components));
|
|
32
|
+
return cmyktoken(srgb2cmykvalues(components[0], components[1], components[2], components[3]));
|
|
37
33
|
}
|
|
38
34
|
function lch2cmykToken(token) {
|
|
39
35
|
const components = lch2srgbvalues(token);
|
|
40
36
|
if (components == null || components.length < 3) {
|
|
41
37
|
return null;
|
|
42
38
|
}
|
|
43
|
-
|
|
44
|
-
return cmyktoken(srgb2cmykvalues(...components));
|
|
39
|
+
return cmyktoken(srgb2cmykvalues(components[0], components[1], components[2], components[3]));
|
|
45
40
|
}
|
|
46
41
|
function oklab2cmyk(token) {
|
|
47
42
|
const components = oklab2srgbvalues(token);
|
|
48
43
|
if (components == null || components.length < 3) {
|
|
49
44
|
return null;
|
|
50
45
|
}
|
|
51
|
-
|
|
52
|
-
return cmyktoken(srgb2cmykvalues(...components));
|
|
46
|
+
return cmyktoken(srgb2cmykvalues(components[0], components[1], components[2], components[3]));
|
|
53
47
|
}
|
|
54
48
|
function oklch2cmykToken(token) {
|
|
55
49
|
const components = oklch2srgbvalues(token);
|
|
56
50
|
if (components == null || components.length < 3) {
|
|
57
51
|
return null;
|
|
58
52
|
}
|
|
59
|
-
|
|
60
|
-
return cmyktoken(srgb2cmykvalues(...components));
|
|
53
|
+
return cmyktoken(srgb2cmykvalues(components[0], components[1], components[2], components[3]));
|
|
61
54
|
}
|
|
62
55
|
function color2cmykToken(token) {
|
|
63
56
|
const values = color2srgbvalues(token);
|
|
64
57
|
if (values == null) {
|
|
65
58
|
return null;
|
|
66
59
|
}
|
|
67
|
-
|
|
68
|
-
return cmyktoken(srgb2cmykvalues(...values));
|
|
60
|
+
return cmyktoken(srgb2cmykvalues(values[0], values[1], values[2], values[3]));
|
|
69
61
|
}
|
|
70
62
|
function srgb2cmykvalues(r, g, b, a = null) {
|
|
71
63
|
const k = 1 - Math.max(r, g, b);
|
|
@@ -16,6 +16,7 @@ import { XYZ_D65_to_D50, xyzd502lch } from './xyzd50.js';
|
|
|
16
16
|
import { srgb2rec2020values } from './rec2020.js';
|
|
17
17
|
import { isRectangularOrthogonalColorspace, isPolarColorspace } from '../syntax.js';
|
|
18
18
|
import { equalsIgnoreCase } from '../../parser/utils/text.js';
|
|
19
|
+
import { srgb2a98values } from './a98rgb.js';
|
|
19
20
|
|
|
20
21
|
function interpolateHue(interpolationMethod, h1, h2) {
|
|
21
22
|
switch (interpolationMethod) {
|
|
@@ -124,65 +125,53 @@ function colorMix(...args) {
|
|
|
124
125
|
case "srgb":
|
|
125
126
|
break;
|
|
126
127
|
case "display-p3":
|
|
127
|
-
|
|
128
|
-
values = srgb2p3values(...values);
|
|
128
|
+
values = srgb2p3values(values[0], values[1], values[2], values[3]);
|
|
129
129
|
break;
|
|
130
130
|
case "display-p3-linear":
|
|
131
|
-
|
|
132
|
-
values = srgb2lp3values(...values);
|
|
131
|
+
values = srgb2lp3values(values[0], values[1], values[2], values[3]);
|
|
133
132
|
break;
|
|
134
133
|
case "a98-rgb":
|
|
135
|
-
|
|
136
|
-
values = srgb2a98values(...values);
|
|
134
|
+
values = srgb2a98values(values[0], values[1], values[2], values[3]);
|
|
137
135
|
break;
|
|
138
136
|
case "prophoto-rgb":
|
|
139
|
-
|
|
140
|
-
values = srgb2prophotorgbvalues(...values);
|
|
137
|
+
values = srgb2prophotorgbvalues(values[0], values[1], values[2], values[3]);
|
|
141
138
|
break;
|
|
142
139
|
case "srgb-linear":
|
|
143
|
-
|
|
144
|
-
values = srgb2lsrgbvalues(...values);
|
|
140
|
+
values = srgb2lsrgbvalues(values[0], values[1], values[2], values[3]);
|
|
145
141
|
break;
|
|
146
142
|
case "rec2020":
|
|
147
|
-
|
|
148
|
-
values = srgb2rec2020values(...values);
|
|
143
|
+
values = srgb2rec2020values(values[0], values[1], values[2], values[3]);
|
|
149
144
|
break;
|
|
150
145
|
case "xyz":
|
|
151
146
|
case "xyz-d65":
|
|
152
|
-
|
|
153
|
-
values = srgb2xyz_d65(...values);
|
|
147
|
+
values = srgb2xyz_d65(values[0], values[1], values[2], values[3]);
|
|
154
148
|
break;
|
|
155
149
|
case "xyz-d50":
|
|
156
|
-
|
|
157
|
-
values = XYZ_D65_to_D50(
|
|
150
|
+
values = srgb2xyz_d65(values[0], values[1], values[2], values[3]);
|
|
151
|
+
values = XYZ_D65_to_D50(values[0], values[1], values[2], values[3]);
|
|
158
152
|
break;
|
|
159
153
|
case "rgb":
|
|
160
|
-
|
|
161
|
-
|
|
154
|
+
for (let j = 0; j < values.length; j++) {
|
|
155
|
+
values[j] = j == 3 ? values[j] : srgb2rgb(values[j]);
|
|
156
|
+
}
|
|
162
157
|
break;
|
|
163
158
|
case "hsl":
|
|
164
|
-
|
|
165
|
-
values = srgb2hslvalues(...values);
|
|
159
|
+
values = srgb2hslvalues(values[0], values[1], values[2], values[3]);
|
|
166
160
|
break;
|
|
167
161
|
case "hwb":
|
|
168
|
-
|
|
169
|
-
values = srgb2hwb(...values);
|
|
162
|
+
values = srgb2hwb(values[0], values[1], values[2], values[3]);
|
|
170
163
|
break;
|
|
171
164
|
case "lab":
|
|
172
|
-
|
|
173
|
-
values = srgb2labvalues(...values);
|
|
165
|
+
values = srgb2labvalues(values[0], values[1], values[2], values[3]);
|
|
174
166
|
break;
|
|
175
167
|
case "lch":
|
|
176
|
-
|
|
177
|
-
values = srgb2lch(...values);
|
|
168
|
+
values = srgb2lch(values[0], values[1], values[2], values[3]);
|
|
178
169
|
break;
|
|
179
170
|
case "oklab":
|
|
180
|
-
|
|
181
|
-
values = srgb2oklab(...values);
|
|
171
|
+
values = srgb2oklab(values[0], values[1], values[2], values[3]);
|
|
182
172
|
break;
|
|
183
173
|
case "oklch":
|
|
184
|
-
|
|
185
|
-
values = srgb2oklch(...values);
|
|
174
|
+
values = srgb2oklch(values[0], values[1], values[2], values[3]);
|
|
186
175
|
break;
|
|
187
176
|
default:
|
|
188
177
|
return null;
|
|
@@ -331,12 +320,10 @@ function colorMix(...args) {
|
|
|
331
320
|
case "xyz-d65":
|
|
332
321
|
case "xyz-d50":
|
|
333
322
|
if (colorSpace == "xyz-d50") {
|
|
334
|
-
|
|
335
|
-
values = xyzd502lch(...values);
|
|
323
|
+
values = xyzd502lch(values[0], values[1], values[2], values[3]);
|
|
336
324
|
}
|
|
337
325
|
else {
|
|
338
|
-
|
|
339
|
-
values = xyz2lchvalues(...values);
|
|
326
|
+
values = xyz2lchvalues(values[0], values[1], values[2], values[3]);
|
|
340
327
|
}
|
|
341
328
|
// @ts-ignore
|
|
342
329
|
return {
|