Skip to content

Commit f1a5661

Browse files
pramilkpramik
andauthored
Skip running color match regex, if not required (#186)
* Skip running color match regex, if not required * change log update * version update * update package-lock file --------- Co-authored-by: Pramil Kuchhal <[email protected]>
1 parent 0cca8cf commit f1a5661

File tree

4 files changed

+60
-53
lines changed

4 files changed

+60
-53
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## main
2+
## 19.2.1
3+
### ✨ Features and improvements
24

5+
* Skip running color match regex for hex color or rgb, if not required [#186](https://github.com/maplibre/maplibre-style-spec/pull/186)
36

47
## 19.2.0
58

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@maplibre/maplibre-gl-style-spec",
33
"description": "a specification for maplibre gl styles",
4-
"version": "19.2.0",
4+
"version": "19.2.1",
55
"author": "MapLibre",
66
"keywords": [
77
"mapbox",

src/util/parse_css_color.ts

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -44,63 +44,67 @@ export function parseCssColor(input: string): RGBColor | undefined {
4444
}
4545

4646
// #f0c, #f0cf, #ff00cc, #ff00ccff
47-
const hexRegexp = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/;
48-
if (hexRegexp.test(input)) {
49-
const step = input.length < 6 ? 1 : 2;
50-
let i = 1;
51-
return [ // eslint-disable-line no-return-assign
52-
parseHex(input.slice(i, i += step)),
53-
parseHex(input.slice(i, i += step)),
54-
parseHex(input.slice(i, i += step)),
55-
parseHex(input.slice(i, i + step) || 'ff'),
56-
];
47+
if (input.startsWith('#')) {
48+
const hexRegexp = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/;
49+
if (hexRegexp.test(input)) {
50+
const step = input.length < 6 ? 1 : 2;
51+
let i = 1;
52+
return [ // eslint-disable-line no-return-assign
53+
parseHex(input.slice(i, i += step)),
54+
parseHex(input.slice(i, i += step)),
55+
parseHex(input.slice(i, i += step)),
56+
parseHex(input.slice(i, i + step) || 'ff'),
57+
];
58+
}
5759
}
5860

5961
// rgb(128 0 0), rgb(50% 0% 0%), rgba(255,0,255,0.6), rgb(255 0 255 / 60%), rgb(100% 0% 100% /.6)
60-
const rgbRegExp = /^rgba?\(\s*([\de.+-]+)(%)?(?:\s+|\s*(,)\s*)([\de.+-]+)(%)?(?:\s+|\s*(,)\s*)([\de.+-]+)(%)?(?:\s*([,\/])\s*([\de.+-]+)(%)?)?\s*\)$/;
61-
const rgbMatch = input.match(rgbRegExp);
62-
if (rgbMatch) {
63-
const [
64-
_, // eslint-disable-line @typescript-eslint/no-unused-vars
65-
r, // <numeric>
66-
rp, // % (optional)
67-
f1, // , (optional)
68-
g, // <numeric>
69-
gp, // % (optional)
70-
f2, // , (optional)
71-
b, // <numeric>
72-
bp, // % (optional)
73-
f3, // ,|/ (optional)
74-
a, // <numeric> (optional)
75-
ap, // % (optional)
76-
] = rgbMatch;
62+
if (input.startsWith('rgb')) {
63+
const rgbRegExp = /^rgba?\(\s*([\de.+-]+)(%)?(?:\s+|\s*(,)\s*)([\de.+-]+)(%)?(?:\s+|\s*(,)\s*)([\de.+-]+)(%)?(?:\s*([,\/])\s*([\de.+-]+)(%)?)?\s*\)$/;
64+
const rgbMatch = input.match(rgbRegExp);
65+
if (rgbMatch) {
66+
const [
67+
_, // eslint-disable-line @typescript-eslint/no-unused-vars
68+
r, // <numeric>
69+
rp, // % (optional)
70+
f1, // , (optional)
71+
g, // <numeric>
72+
gp, // % (optional)
73+
f2, // , (optional)
74+
b, // <numeric>
75+
bp, // % (optional)
76+
f3, // ,|/ (optional)
77+
a, // <numeric> (optional)
78+
ap, // % (optional)
79+
] = rgbMatch;
7780

78-
const argFormat = [f1 || ' ', f2 || ' ', f3].join('');
79-
if (
80-
argFormat === ' ' ||
81-
argFormat === ' /' ||
82-
argFormat === ',,' ||
83-
argFormat === ',,,'
84-
) {
85-
const valFormat = [rp, gp, bp].join('');
86-
const maxValue =
87-
(valFormat === '%%%') ? 100 :
88-
(valFormat === '') ? 255 : 0;
89-
if (maxValue) {
90-
const rgba: RGBColor = [
91-
clamp(+r / maxValue, 0, 1),
92-
clamp(+g / maxValue, 0, 1),
93-
clamp(+b / maxValue, 0, 1),
94-
a ? parseAlpha(+a, ap) : 1,
95-
];
96-
if (validateNumbers(rgba)) {
97-
return rgba;
81+
const argFormat = [f1 || ' ', f2 || ' ', f3].join('');
82+
if (
83+
argFormat === ' ' ||
84+
argFormat === ' /' ||
85+
argFormat === ',,' ||
86+
argFormat === ',,,'
87+
) {
88+
const valFormat = [rp, gp, bp].join('');
89+
const maxValue =
90+
(valFormat === '%%%') ? 100 :
91+
(valFormat === '') ? 255 : 0;
92+
if (maxValue) {
93+
const rgba: RGBColor = [
94+
clamp(+r / maxValue, 0, 1),
95+
clamp(+g / maxValue, 0, 1),
96+
clamp(+b / maxValue, 0, 1),
97+
a ? parseAlpha(+a, ap) : 1,
98+
];
99+
if (validateNumbers(rgba)) {
100+
return rgba;
101+
}
102+
// invalid numbers
98103
}
99-
// invalid numbers
104+
// values must be all numbers or all percentages
100105
}
101-
// values must be all numbers or all percentages
106+
return; // comma optional syntax requires no commas at all
102107
}
103-
return; // comma optional syntax requires no commas at all
104108
}
105109

106110
// hsl(120 50% 80%), hsla(120deg,50%,80%,.9), hsl(12e1 50% 80% / 90%)

0 commit comments

Comments
 (0)