@mapbox/mapbox-gl-style-spec 14.12.0 → 14.13.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/util/color.ts CHANGED
@@ -56,9 +56,9 @@ class Color {
56
56
  }
57
57
 
58
58
  return new Color(
59
- rgba[0] / 255 * rgba[3],
60
- rgba[1] / 255 * rgba[3],
61
- rgba[2] / 255 * rgba[3],
59
+ rgba[0] / 255,
60
+ rgba[1] / 255,
61
+ rgba[2] / 255,
62
62
  rgba[3]
63
63
  );
64
64
  }
@@ -73,16 +73,6 @@ class Color {
73
73
  * var translucentGreen = new Color.parse('rgba(26, 207, 26, .73)');
74
74
  * translucentGreen.toString(); // = "rgba(26,207,26,0.73)"
75
75
  */
76
- toStringPremultipliedAlpha(): string {
77
- const [r, g, b, a] = this.a === 0 ? [0, 0, 0, 0] : [
78
- this.r * 255 / this.a,
79
- this.g * 255 / this.a,
80
- this.b * 255 / this.a,
81
- this.a
82
- ];
83
- return `rgba(${Math.round(r)},${Math.round(g)},${Math.round(b)},${a})`;
84
- }
85
-
86
76
  toString(): string {
87
77
  const [r, g, b, a] = [
88
78
  this.r,
@@ -93,9 +83,14 @@ class Color {
93
83
  return `rgba(${Math.round(r * 255)},${Math.round(g * 255)},${Math.round(b * 255)},${a})`;
94
84
  }
95
85
 
96
- toRenderColor(lut: LUT | null): RenderColor {
86
+ toNonPremultipliedRenderColor(lut: LUT | null): NonPremultipliedRenderColor {
87
+ const {r, g, b, a} = this;
88
+ return new NonPremultipliedRenderColor(lut, r, g, b, a);
89
+ }
90
+
91
+ toPremultipliedRenderColor(lut: LUT | null): NonPremultipliedRenderColor {
97
92
  const {r, g, b, a} = this;
98
- return new RenderColor(lut, r, g, b, a);
93
+ return new PremultipliedRenderColor(lut, r * a, g * a, b * a, a);
99
94
  }
100
95
 
101
96
  clone(): Color {
@@ -103,16 +98,16 @@ class Color {
103
98
  }
104
99
  }
105
100
 
106
- /**
107
- * Renderable color created from a Color and an optional LUT value
108
- */
109
- export class RenderColor {
101
+ export abstract class RenderColor {
102
+ premultiplied: boolean = false;
103
+
110
104
  r: number;
111
105
  g: number;
112
106
  b: number;
113
107
  a: number;
114
108
 
115
- constructor(lut: LUT | null, r: number, g: number, b: number, a: number) {
109
+ constructor(lut: LUT | null, r: number, g: number, b: number, a: number, premultiplied: boolean = false) {
110
+ this.premultiplied = premultiplied;
116
111
  if (!lut) {
117
112
  this.r = r;
118
113
  this.g = g;
@@ -121,10 +116,18 @@ export class RenderColor {
121
116
  } else {
122
117
  const N = lut.image.height;
123
118
  const N2 = N * N;
119
+
124
120
  // Normalize to cube dimensions.
125
- r = a === 0 ? 0 : (r / a) * (N - 1);
126
- g = a === 0 ? 0 : (g / a) * (N - 1);
127
- b = a === 0 ? 0 : (b / a) * (N - 1);
121
+
122
+ if (this.premultiplied) {
123
+ r = a === 0 ? 0 : (r / a) * (N - 1);
124
+ g = a === 0 ? 0 : (g / a) * (N - 1);
125
+ b = a === 0 ? 0 : (b / a) * (N - 1);
126
+ } else {
127
+ r = r * (N - 1);
128
+ g = g * (N - 1);
129
+ b = b * (N - 1);
130
+ }
128
131
 
129
132
  // Determine boundary values for the cube the color is in.
130
133
  const r0 = Math.floor(r);
@@ -159,54 +162,59 @@ export class RenderColor {
159
162
  lerp(data[i2], data[i3], bw), gw),
160
163
  lerp(
161
164
  lerp(data[i4], data[i5], bw),
162
- lerp(data[i6], data[i7], bw), gw), rw) / 255 * a;
165
+ lerp(data[i6], data[i7], bw), gw), rw) / 255 * (this.premultiplied ? a : 1);
163
166
  this.g = lerp(
164
167
  lerp(
165
168
  lerp(data[i0 + 1], data[i1 + 1], bw),
166
169
  lerp(data[i2 + 1], data[i3 + 1], bw), gw),
167
170
  lerp(
168
171
  lerp(data[i4 + 1], data[i5 + 1], bw),
169
- lerp(data[i6 + 1], data[i7 + 1], bw), gw), rw) / 255 * a;
172
+ lerp(data[i6 + 1], data[i7 + 1], bw), gw), rw) / 255 * (this.premultiplied ? a : 1);
170
173
  this.b = lerp(
171
174
  lerp(
172
175
  lerp(data[i0 + 2], data[i1 + 2], bw),
173
176
  lerp(data[i2 + 2], data[i3 + 2], bw), gw),
174
177
  lerp(
175
178
  lerp(data[i4 + 2], data[i5 + 2], bw),
176
- lerp(data[i6 + 2], data[i7 + 2], bw), gw), rw) / 255 * a;
179
+ lerp(data[i6 + 2], data[i7 + 2], bw), gw), rw) / 255 * (this.premultiplied ? a : 1);
177
180
  this.a = a;
178
181
  }
179
182
  }
180
183
 
181
184
  /**
182
- * Returns an RGBA array of values representing the color, unpremultiplied by A.
183
- *
185
+ * Returns an RGBA array of values representing the color.
184
186
  * @returns An array of RGBA color values in the range [0, 255].
185
187
  */
186
188
  toArray(): [number, number, number, number] {
187
189
  const {r, g, b, a} = this;
188
- return a === 0 ? [0, 0, 0, 0] : [
189
- r * 255 / a,
190
- g * 255 / a,
191
- b * 255 / a,
190
+
191
+ return [
192
+ r * 255,
193
+ g * 255,
194
+ b * 255,
192
195
  a
193
196
  ];
197
+
194
198
  }
195
199
 
196
200
  /**
197
201
  * Returns an HSLA array of values representing the color, unpremultiplied by A.
198
- *
199
202
  * @returns An array of HSLA color values.
200
203
  */
201
204
  toHslaArray(): [number, number, number, number] {
202
- if (this.a === 0) {
203
- return [0, 0, 0, 0];
205
+ let {r, g, b, a} = this;
206
+
207
+ if (this.premultiplied) {
208
+ if (a === 0) return [0, 0, 0, 0];
209
+
210
+ r /= a;
211
+ g /= a;
212
+ b /= a;
204
213
  }
205
- const {r, g, b, a} = this;
206
214
 
207
- const red = Math.min(Math.max(r / a, 0.0), 1.0);
208
- const green = Math.min(Math.max(g / a, 0.0), 1.0);
209
- const blue = Math.min(Math.max(b / a, 0.0), 1.0);
215
+ const red = Math.min(Math.max(r, 0.0), 1.0);
216
+ const green = Math.min(Math.max(g, 0.0), 1.0);
217
+ const blue = Math.min(Math.max(b, 0.0), 1.0);
210
218
 
211
219
  const min = Math.min(red, green, blue);
212
220
  const max = Math.max(red, green, blue);
@@ -241,16 +249,17 @@ export class RenderColor {
241
249
  }
242
250
 
243
251
  /**
244
- * Returns a RGBA array of float values representing the color, unpremultiplied by A.
252
+ * Returns a RGBA array of float values representing the color.
245
253
  *
246
254
  * @returns An array of RGBA color values in the range [0, 1].
247
255
  */
248
256
  toArray01(): [number, number, number, number] {
249
257
  const {r, g, b, a} = this;
250
- return a === 0 ? [0, 0, 0, 0] : [
251
- r / a,
252
- g / a,
253
- b / a,
258
+
259
+ return [
260
+ r,
261
+ g,
262
+ b,
254
263
  a
255
264
  ];
256
265
  }
@@ -262,46 +271,53 @@ export class RenderColor {
262
271
  * @returns An array of RGB color values in the range [0, 1].
263
272
  */
264
273
  toArray01Scaled(scale: number): [number, number, number] {
265
- const {r, g, b, a} = this;
266
- return a === 0 ? [0, 0, 0] : [
267
- (r / a) * scale,
268
- (g / a) * scale,
269
- (b / a) * scale
270
- ];
271
- }
274
+ const {r, g, b} = this;
272
275
 
273
- /**
274
- * Returns an RGBA array of values representing the color, premultiplied by A.
275
- *
276
- * @returns An array of RGBA color values in the range [0, 1].
277
- */
278
- toArray01PremultipliedAlpha(): [number, number, number, number] {
279
- const {r, g, b, a} = this;
280
276
  return [
281
- r,
282
- g,
283
- b,
284
- a
277
+ r * scale,
278
+ g * scale,
279
+ b * scale
285
280
  ];
286
281
  }
287
282
 
288
283
  /**
289
- * Returns an RGBA array of values representing the color, unpremultiplied by A, and converted to linear color space.
290
- * The color is defined by sRGB primaries, but the sRGB transfer function is reversed to obtain linear energy.
291
- *
284
+ * Returns an RGBA array of values representing the color converted to linear color space.
285
+ * The color is defined by sRGB primaries, but the sRGB transfer function
286
+ * is reversed to obtain linear energy.
292
287
  * @returns An array of RGBA color values in the range [0, 1].
293
288
  */
294
289
  toArray01Linear(): [number, number, number, number] {
295
290
  const {r, g, b, a} = this;
296
- return a === 0 ? [0, 0, 0, 0] : [
297
- Math.pow((r / a), 2.2),
298
- Math.pow((g / a), 2.2),
299
- Math.pow((b / a), 2.2),
291
+
292
+ return [
293
+ Math.pow(r, 2.2),
294
+ Math.pow(g, 2.2),
295
+ Math.pow(b, 2.2),
300
296
  a
301
297
  ];
302
298
  }
303
299
  }
304
300
 
301
+ /**
302
+ * Renderable color created from a Color and an optional LUT value.
303
+ * Represent a color value with non-premultiplied alpha.
304
+ */
305
+ export class NonPremultipliedRenderColor extends RenderColor {
306
+ constructor(lut: LUT | null, r: number, g: number, b: number, a: number) {
307
+ super(lut, r, g, b, a, false);
308
+ }
309
+ }
310
+
311
+ /**
312
+ * Renderable color created from a Color and an optional LUT value.
313
+ * Represent a color value with premultiplied alpha.
314
+ */
315
+ export class PremultipliedRenderColor extends RenderColor {
316
+ constructor(lut: LUT | null, r: number, g: number, b: number, a: number) {
317
+ super(lut, r, g, b, a, true);
318
+ }
319
+ }
320
+
305
321
  Color.black = new Color(0, 0, 0, 1);
306
322
  Color.white = new Color(1, 1, 1, 1);
307
323
  Color.transparent = new Color(0, 0, 0, 0);
@@ -18,7 +18,3 @@ export function array(from: Array<number>, to: Array<number>, t: number): Array<
18
18
  return number(d, to[i], t);
19
19
  });
20
20
  }
21
-
22
- export function easeIn(x: number) {
23
- return x * x * x * x * x;
24
- }
@@ -70,7 +70,26 @@ export default function validateLights(options: Options): Array<ValidationError>
70
70
  return errors;
71
71
  }
72
72
  for (const propertyKey in properties) {
73
- if (!lightPropertySpec[propertyKey]) {
73
+ const transitionMatch = propertyKey.match(/^(.*)-transition$/);
74
+ const useThemeMatch = propertyKey.match(/^(.*)-use-theme$/);
75
+
76
+ if (useThemeMatch && lightPropertySpec[useThemeMatch[1]]) {
77
+ errors = errors.concat(validate({
78
+ key,
79
+ value: properties[propertyKey],
80
+ valueSpec: {type: 'string'},
81
+ style,
82
+ styleSpec
83
+ }));
84
+ } else if (transitionMatch && lightPropertySpec[transitionMatch[1]] && lightPropertySpec[transitionMatch[1]].transition) {
85
+ errors = errors.concat(validate({
86
+ key,
87
+ value: light[key],
88
+ valueSpec: styleSpec.transition,
89
+ style,
90
+ styleSpec
91
+ }));
92
+ } else if (!lightPropertySpec[propertyKey]) {
74
93
  errors = errors.concat([new ValidationWarning(options.key, properties[propertyKey], `unknown property "${propertyKey}"`)]);
75
94
  } else {
76
95
  errors = errors.concat(validate({
@@ -83,26 +102,7 @@ export default function validateLights(options: Options): Array<ValidationError>
83
102
  }
84
103
  }
85
104
  } else {
86
- const transitionMatch = key.match(/^(.*)-transition$/);
87
- const useThemeMatch = key.match(/^(.*)-use-theme$/);
88
-
89
- if (useThemeMatch && lightSpec[useThemeMatch[1]]) {
90
- errors = errors.concat(validate({
91
- key,
92
- value: light[key],
93
- valueSpec: {type: 'string'},
94
- style,
95
- styleSpec
96
- }));
97
- } else if (transitionMatch && lightSpec[transitionMatch[1]] && lightSpec[transitionMatch[1]].transition) {
98
- errors = errors.concat(validate({
99
- key,
100
- value: light[key],
101
- valueSpec: styleSpec.transition,
102
- style,
103
- styleSpec
104
- }));
105
- } else if (lightSpec[key]) {
105
+ if (lightSpec[key]) {
106
106
  errors = errors.concat(validate({
107
107
  key,
108
108
  value: light[key],