ol 8.1.1-dev.1694732464464 → 8.1.1-dev.1694804687683

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/color.d.ts CHANGED
@@ -1,3 +1,11 @@
1
+ /**
2
+ * A color represented as a short array [red, green, blue, alpha].
3
+ * red, green, and blue should be integers in the range 0..255 inclusive.
4
+ * alpha should be a float in the range 0..1 inclusive. If no alpha value is
5
+ * given then `1` will be used.
6
+ * @typedef {Array<number>} Color
7
+ * @api
8
+ */
1
9
  /**
2
10
  * Return the color as an rgba string.
3
11
  * @param {Color|string} color Color.
@@ -5,6 +13,11 @@
5
13
  * @api
6
14
  */
7
15
  export function asString(color: Color | string): string;
16
+ /**
17
+ * @param {string} s String.
18
+ * @return {Color} Color.
19
+ */
20
+ export function fromString(s: string): Color;
8
21
  /**
9
22
  * Return the color as an array. This function maintains a cache of calculated
10
23
  * arrays which means the result should not be modified.
@@ -29,11 +42,6 @@ export function toString(color: Color): string;
29
42
  * @return {boolean} Whether the string is actually a valid color
30
43
  */
31
44
  export function isStringColor(s: string): boolean;
32
- /**
33
- * @param {string} s String.
34
- * @return {Color} Color.
35
- */
36
- export function fromString(s: string): Color;
37
45
  /**
38
46
  * A color represented as a short array [red, green, blue, alpha].
39
47
  * red, green, and blue should be integers in the range 0..255 inclusive.
package/color.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"color.d.ts","sourceRoot":"","sources":["color.js"],"names":[],"mappings":"AA+BA;;;;;GAKG;AACH,gCAJW,QAAM,MAAM,GACX,MAAM,CAQjB;AAwED;;;;;;GAMG;AACH,+BAJW,QAAM,MAAM,SAStB;AAwDD;;;;GAIG;AACH,+CAMC;AAED;;;GAGG;AACH,wCAFY,MAAM,CAiBjB;AAED;;;GAGG;AACH,iCAHW,MAAM,GACL,OAAO,CAOlB;AA1IG;;;GAGG;AACH,8BAHW,MAAM,SAsBhB;;;;;;;oBAnGQ,MAAM,MAAM,CAAC"}
1
+ {"version":3,"file":"color.d.ts","sourceRoot":"","sources":["color.js"],"names":[],"mappings":"AAMA;;;;;;;GAOG;AAEH;;;;;GAKG;AACH,gCAJW,QAAM,MAAM,GACX,MAAM,CAQjB;AAoBD;;;GAGG;AACH,8BAHW,MAAM,SA8BhB;AAED;;;;;;GAMG;AACH,+BAJW,QAAM,MAAM,SAStB;AAED;;;;GAIG;AACH,+CAMC;AAED;;;GAGG;AACH,wCAFY,MAAM,CAiBjB;AAED;;;GAGG;AACH,iCAHW,MAAM,GACL,OAAO,CASlB;;;;;;;oBA/HY,MAAM,MAAM,CAAC"}
package/color.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @module ol/color
3
3
  */
4
- import colorNames from 'color-name';
4
+ import parseRgba from 'color-rgba';
5
5
  import {clamp} from './math.js';
6
6
 
7
7
  /**
@@ -13,22 +13,6 @@ import {clamp} from './math.js';
13
13
  * @api
14
14
  */
15
15
 
16
- /**
17
- * This RegExp matches # followed by 3, 4, 6, or 8 hex digits.
18
- * @const
19
- * @type {RegExp}
20
- * @private
21
- */
22
- const HEX_COLOR_RE_ = /^#([a-f0-9]{3}|[a-f0-9]{4}(?:[a-f0-9]{2}){0,2})$/i;
23
-
24
- /**
25
- * Regular expression for matching potential named color style strings.
26
- * @const
27
- * @type {RegExp}
28
- * @private
29
- */
30
- const NAMED_COLOR_RE_ = /^([a-z]*)$|^hsla?\(.*\)$/i;
31
-
32
16
  /**
33
17
  * Return the color as an rgba string.
34
18
  * @param {Color|string} color Color.
@@ -43,74 +27,55 @@ export function asString(color) {
43
27
  }
44
28
 
45
29
  /**
46
- * Return named color as an rgb(a) string.
47
- * @param {string} color Named color.
48
- * @return {string} RGB(A) string.
30
+ * @type {number}
49
31
  */
50
- function fromNamed(color) {
51
- const name = color.toLowerCase();
52
- if (!colorNames.hasOwnProperty(name)) {
53
- if (name === 'transparent') {
54
- return 'rgba(0,0,0,0)';
55
- }
56
- return '';
57
- }
58
- const rgb = colorNames[name];
59
- return 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')';
60
- }
32
+ const MAX_CACHE_SIZE = 1024;
61
33
 
62
34
  /**
63
- * @param {string} s String.
64
- * @return {Color} Color.
35
+ * We maintain a small cache of parsed strings. Whenever the cache grows too large,
36
+ * we delete an arbitrary set of the entries.
37
+ *
38
+ * @type {Object<string, Color>}
65
39
  */
66
- export const fromString = (function () {
67
- // We maintain a small cache of parsed strings. To provide cheap LRU-like
68
- // semantics, whenever the cache grows too large we simply delete an
69
- // arbitrary 25% of the entries.
40
+ const cache = {};
70
41
 
71
- /**
72
- * @const
73
- * @type {number}
74
- */
75
- const MAX_CACHE_SIZE = 1024;
76
-
77
- /**
78
- * @type {Object<string, Color>}
79
- */
80
- const cache = {};
81
-
82
- /**
83
- * @type {number}
84
- */
85
- let cacheSize = 0;
42
+ /**
43
+ * @type {number}
44
+ */
45
+ let cacheSize = 0;
86
46
 
87
- return (
88
- /**
89
- * @param {string} s String.
90
- * @return {Color} Color.
91
- */
92
- function (s) {
93
- let color;
94
- if (cache.hasOwnProperty(s)) {
95
- color = cache[s];
96
- } else {
97
- if (cacheSize >= MAX_CACHE_SIZE) {
98
- let i = 0;
99
- for (const key in cache) {
100
- if ((i++ & 3) === 0) {
101
- delete cache[key];
102
- --cacheSize;
103
- }
104
- }
105
- }
106
- color = fromStringInternal_(s);
107
- cache[s] = color;
108
- ++cacheSize;
47
+ /**
48
+ * @param {string} s String.
49
+ * @return {Color} Color.
50
+ */
51
+ export function fromString(s) {
52
+ if (cache.hasOwnProperty(s)) {
53
+ return cache[s];
54
+ }
55
+ if (cacheSize >= MAX_CACHE_SIZE) {
56
+ let i = 0;
57
+ for (const key in cache) {
58
+ if ((i++ & 3) === 0) {
59
+ delete cache[key];
60
+ --cacheSize;
109
61
  }
110
- return color;
111
62
  }
112
- );
113
- })();
63
+ }
64
+
65
+ const color = parseRgba(s);
66
+ if (color.length !== 4) {
67
+ throw new Error('Failed to parse "' + s + '" as color');
68
+ }
69
+ for (const c of color) {
70
+ if (isNaN(c)) {
71
+ throw new Error('Failed to parse "' + s + '" as color');
72
+ }
73
+ }
74
+ normalize(color);
75
+ cache[s] = color;
76
+ ++cacheSize;
77
+ return color;
78
+ }
114
79
 
115
80
  /**
116
81
  * Return the color as an array. This function maintains a cache of calculated
@@ -126,60 +91,6 @@ export function asArray(color) {
126
91
  return fromString(color);
127
92
  }
128
93
 
129
- /**
130
- * @param {string} s String.
131
- * @private
132
- * @return {Color} Color.
133
- */
134
- function fromStringInternal_(s) {
135
- let r, g, b, a, color;
136
-
137
- if (NAMED_COLOR_RE_.exec(s)) {
138
- s = fromNamed(s);
139
- }
140
-
141
- if (HEX_COLOR_RE_.exec(s)) {
142
- // hex
143
- const n = s.length - 1; // number of hex digits
144
- let d; // number of digits per channel
145
- if (n <= 4) {
146
- d = 1;
147
- } else {
148
- d = 2;
149
- }
150
- const hasAlpha = n === 4 || n === 8;
151
- r = parseInt(s.substr(1 + 0 * d, d), 16);
152
- g = parseInt(s.substr(1 + 1 * d, d), 16);
153
- b = parseInt(s.substr(1 + 2 * d, d), 16);
154
- if (hasAlpha) {
155
- a = parseInt(s.substr(1 + 3 * d, d), 16);
156
- } else {
157
- a = 255;
158
- }
159
- if (d == 1) {
160
- r = (r << 4) + r;
161
- g = (g << 4) + g;
162
- b = (b << 4) + b;
163
- if (hasAlpha) {
164
- a = (a << 4) + a;
165
- }
166
- }
167
- color = [r, g, b, a / 255];
168
- } else if (s.startsWith('rgba(')) {
169
- // rgba()
170
- color = s.slice(5, -1).split(',').map(Number);
171
- normalize(color);
172
- } else if (s.startsWith('rgb(')) {
173
- // rgb()
174
- color = s.slice(4, -1).split(',').map(Number);
175
- color.push(1);
176
- normalize(color);
177
- } else {
178
- throw new Error('Invalid color');
179
- }
180
- return color;
181
- }
182
-
183
94
  /**
184
95
  * Exported for the tests.
185
96
  * @param {Color} color Color.
@@ -219,8 +130,10 @@ export function toString(color) {
219
130
  * @return {boolean} Whether the string is actually a valid color
220
131
  */
221
132
  export function isStringColor(s) {
222
- if (NAMED_COLOR_RE_.test(s)) {
223
- s = fromNamed(s);
133
+ try {
134
+ fromString(s);
135
+ return true;
136
+ } catch (_) {
137
+ return false;
224
138
  }
225
- return HEX_COLOR_RE_.test(s) || s.startsWith('rgba(') || s.startsWith('rgb(');
226
139
  }