This commit is contained in:
132
node_modules/@mapbox/mapbox-gl-style-spec/util/color.js
generated
vendored
Normal file
132
node_modules/@mapbox/mapbox-gl-style-spec/util/color.js
generated
vendored
Normal file
@@ -0,0 +1,132 @@
|
||||
// @flow
|
||||
|
||||
import {parseCSSColor} from 'csscolorparser';
|
||||
|
||||
/**
|
||||
* An RGBA color value. Create instances from color strings using the static
|
||||
* method `Color.parse`. The constructor accepts RGB channel values in the range
|
||||
* `[0, 1]`, premultiplied by A.
|
||||
*
|
||||
* @param {number} r The red channel.
|
||||
* @param {number} g The green channel.
|
||||
* @param {number} b The blue channel.
|
||||
* @param {number} a The alpha channel.
|
||||
* @private
|
||||
*/
|
||||
class Color {
|
||||
r: number;
|
||||
g: number;
|
||||
b: number;
|
||||
a: number;
|
||||
|
||||
constructor(r: number, g: number, b: number, a: number = 1) {
|
||||
this.r = r;
|
||||
this.g = g;
|
||||
this.b = b;
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
static black: Color;
|
||||
static white: Color;
|
||||
static transparent: Color;
|
||||
static red: Color;
|
||||
static blue: Color;
|
||||
|
||||
/**
|
||||
* Parses valid CSS color strings and returns a `Color` instance.
|
||||
* @returns A `Color` instance, or `undefined` if the input is not a valid color string.
|
||||
*/
|
||||
static parse(input?: string | Color | null): Color | void {
|
||||
if (!input) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (input instanceof Color) {
|
||||
return input;
|
||||
}
|
||||
|
||||
if (typeof input !== 'string') {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const rgba = parseCSSColor(input);
|
||||
if (!rgba) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return new Color(
|
||||
rgba[0] / 255 * rgba[3],
|
||||
rgba[1] / 255 * rgba[3],
|
||||
rgba[2] / 255 * rgba[3],
|
||||
rgba[3]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an RGBA string representing the color value.
|
||||
*
|
||||
* @returns An RGBA string.
|
||||
* @example
|
||||
* var purple = new Color.parse('purple');
|
||||
* purple.toString; // = "rgba(128,0,128,1)"
|
||||
* var translucentGreen = new Color.parse('rgba(26, 207, 26, .73)');
|
||||
* translucentGreen.toString(); // = "rgba(26,207,26,0.73)"
|
||||
*/
|
||||
toString(): string {
|
||||
const [r, g, b, a] = this.toArray();
|
||||
return `rgba(${Math.round(r)},${Math.round(g)},${Math.round(b)},${a})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an RGBA array of values representing the color, unpremultiplied by A.
|
||||
*
|
||||
* @returns An array of RGBA color values in the range [0, 255].
|
||||
*/
|
||||
toArray(): [number, number, number, number] {
|
||||
const {r, g, b, a} = this;
|
||||
return a === 0 ? [0, 0, 0, 0] : [
|
||||
r * 255 / a,
|
||||
g * 255 / a,
|
||||
b * 255 / a,
|
||||
a
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a RGBA array of float values representing the color, unpremultiplied by A.
|
||||
*
|
||||
* @returns An array of RGBA color values in the range [0, 1].
|
||||
*/
|
||||
toArray01(): [number, number, number, number] {
|
||||
const {r, g, b, a} = this;
|
||||
return a === 0 ? [0, 0, 0, 0] : [
|
||||
r / a,
|
||||
g / a,
|
||||
b / a,
|
||||
a
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an RGBA array of values representing the color, premultiplied by A.
|
||||
*
|
||||
* @returns An array of RGBA color values in the range [0, 1].
|
||||
*/
|
||||
toArray01PremultipliedAlpha(): [number, number, number, number] {
|
||||
const {r, g, b, a} = this;
|
||||
return [
|
||||
r,
|
||||
g,
|
||||
b,
|
||||
a
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Color.black = new Color(0, 0, 0, 1);
|
||||
Color.white = new Color(1, 1, 1, 1);
|
||||
Color.transparent = new Color(0, 0, 0, 0);
|
||||
Color.red = new Color(1, 0, 0, 1);
|
||||
Color.blue = new Color(0, 0, 1, 1);
|
||||
|
||||
export default Color;
|
||||
139
node_modules/@mapbox/mapbox-gl-style-spec/util/color_spaces.js
generated
vendored
Normal file
139
node_modules/@mapbox/mapbox-gl-style-spec/util/color_spaces.js
generated
vendored
Normal file
@@ -0,0 +1,139 @@
|
||||
// @flow
|
||||
|
||||
import Color from './color.js';
|
||||
|
||||
import {number as interpolateNumber} from './interpolate.js';
|
||||
|
||||
type LABColor = {
|
||||
l: number,
|
||||
a: number,
|
||||
b: number,
|
||||
alpha: number
|
||||
};
|
||||
|
||||
type HCLColor = {
|
||||
h: number,
|
||||
c: number,
|
||||
l: number,
|
||||
alpha: number
|
||||
};
|
||||
|
||||
// Constants
|
||||
const Xn = 0.950470, // D65 standard referent
|
||||
Yn = 1,
|
||||
Zn = 1.088830,
|
||||
t0 = 4 / 29,
|
||||
t1 = 6 / 29,
|
||||
t2 = 3 * t1 * t1,
|
||||
t3 = t1 * t1 * t1,
|
||||
deg2rad = Math.PI / 180,
|
||||
rad2deg = 180 / Math.PI;
|
||||
|
||||
// Utilities
|
||||
function xyz2lab(t: number) {
|
||||
return t > t3 ? Math.pow(t, 1 / 3) : t / t2 + t0;
|
||||
}
|
||||
|
||||
function lab2xyz(t: number) {
|
||||
return t > t1 ? t * t * t : t2 * (t - t0);
|
||||
}
|
||||
|
||||
function xyz2rgb(x: number) {
|
||||
return 255 * (x <= 0.0031308 ? 12.92 * x : 1.055 * Math.pow(x, 1 / 2.4) - 0.055);
|
||||
}
|
||||
|
||||
function rgb2xyz(x: number) {
|
||||
x /= 255;
|
||||
return x <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
|
||||
}
|
||||
|
||||
// LAB
|
||||
function rgbToLab(rgbColor: Color): LABColor {
|
||||
const b = rgb2xyz(rgbColor.r),
|
||||
a = rgb2xyz(rgbColor.g),
|
||||
l = rgb2xyz(rgbColor.b),
|
||||
x = xyz2lab((0.4124564 * b + 0.3575761 * a + 0.1804375 * l) / Xn),
|
||||
y = xyz2lab((0.2126729 * b + 0.7151522 * a + 0.0721750 * l) / Yn),
|
||||
z = xyz2lab((0.0193339 * b + 0.1191920 * a + 0.9503041 * l) / Zn);
|
||||
|
||||
return {
|
||||
l: 116 * y - 16,
|
||||
a: 500 * (x - y),
|
||||
b: 200 * (y - z),
|
||||
alpha: rgbColor.a
|
||||
};
|
||||
}
|
||||
|
||||
function labToRgb(labColor: LABColor): Color {
|
||||
let y = (labColor.l + 16) / 116,
|
||||
x = isNaN(labColor.a) ? y : y + labColor.a / 500,
|
||||
z = isNaN(labColor.b) ? y : y - labColor.b / 200;
|
||||
y = Yn * lab2xyz(y);
|
||||
x = Xn * lab2xyz(x);
|
||||
z = Zn * lab2xyz(z);
|
||||
return new Color(
|
||||
xyz2rgb(3.2404542 * x - 1.5371385 * y - 0.4985314 * z), // D65 -> sRGB
|
||||
xyz2rgb(-0.9692660 * x + 1.8760108 * y + 0.0415560 * z),
|
||||
xyz2rgb(0.0556434 * x - 0.2040259 * y + 1.0572252 * z),
|
||||
labColor.alpha
|
||||
);
|
||||
}
|
||||
|
||||
function interpolateLab(from: LABColor, to: LABColor, t: number): LABColor {
|
||||
return {
|
||||
l: interpolateNumber(from.l, to.l, t),
|
||||
a: interpolateNumber(from.a, to.a, t),
|
||||
b: interpolateNumber(from.b, to.b, t),
|
||||
alpha: interpolateNumber(from.alpha, to.alpha, t)
|
||||
};
|
||||
}
|
||||
|
||||
// HCL
|
||||
function rgbToHcl(rgbColor: Color): HCLColor {
|
||||
const {l, a, b} = rgbToLab(rgbColor);
|
||||
const h = Math.atan2(b, a) * rad2deg;
|
||||
return {
|
||||
h: h < 0 ? h + 360 : h,
|
||||
c: Math.sqrt(a * a + b * b),
|
||||
l,
|
||||
alpha: rgbColor.a
|
||||
};
|
||||
}
|
||||
|
||||
function hclToRgb(hclColor: HCLColor): Color {
|
||||
const h = hclColor.h * deg2rad,
|
||||
c = hclColor.c,
|
||||
l = hclColor.l;
|
||||
return labToRgb({
|
||||
l,
|
||||
a: Math.cos(h) * c,
|
||||
b: Math.sin(h) * c,
|
||||
alpha: hclColor.alpha
|
||||
});
|
||||
}
|
||||
|
||||
function interpolateHue(a: number, b: number, t: number) {
|
||||
const d = b - a;
|
||||
return a + t * (d > 180 || d < -180 ? d - 360 * Math.round(d / 360) : d);
|
||||
}
|
||||
|
||||
function interpolateHcl(from: HCLColor, to: HCLColor, t: number): HCLColor {
|
||||
return {
|
||||
h: interpolateHue(from.h, to.h, t),
|
||||
c: interpolateNumber(from.c, to.c, t),
|
||||
l: interpolateNumber(from.l, to.l, t),
|
||||
alpha: interpolateNumber(from.alpha, to.alpha, t)
|
||||
};
|
||||
}
|
||||
|
||||
export const lab = {
|
||||
forward: rgbToLab,
|
||||
reverse: labToRgb,
|
||||
interpolate: interpolateLab
|
||||
};
|
||||
|
||||
export const hcl = {
|
||||
forward: rgbToHcl,
|
||||
reverse: hclToRgb,
|
||||
interpolate: interpolateHcl
|
||||
};
|
||||
28
node_modules/@mapbox/mapbox-gl-style-spec/util/deep_equal.js
generated
vendored
Normal file
28
node_modules/@mapbox/mapbox-gl-style-spec/util/deep_equal.js
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
// @flow
|
||||
|
||||
/**
|
||||
* Deeply compares two object literals.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
function deepEqual(a: ?mixed, b: ?mixed): boolean {
|
||||
if (Array.isArray(a)) {
|
||||
if (!Array.isArray(b) || a.length !== b.length) return false;
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
if (!deepEqual(a[i], b[i])) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (typeof a === 'object' && a !== null && b !== null) {
|
||||
if (!(typeof b === 'object')) return false;
|
||||
const keys = Object.keys(a);
|
||||
if (keys.length !== Object.keys(b).length) return false;
|
||||
for (const key in a) {
|
||||
if (!deepEqual(a[key], b[key])) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return a === b;
|
||||
}
|
||||
|
||||
export default deepEqual;
|
||||
10
node_modules/@mapbox/mapbox-gl-style-spec/util/extend.js
generated
vendored
Normal file
10
node_modules/@mapbox/mapbox-gl-style-spec/util/extend.js
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
// @flow
|
||||
|
||||
export default function (output: any, ...inputs: Array<any>): any {
|
||||
for (const input of inputs) {
|
||||
for (const k in input) {
|
||||
output[k] = input[k];
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
17
node_modules/@mapbox/mapbox-gl-style-spec/util/get_type.js
generated
vendored
Normal file
17
node_modules/@mapbox/mapbox-gl-style-spec/util/get_type.js
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
// @flow
|
||||
|
||||
export default function getType(val: mixed): string {
|
||||
if (val instanceof Number) {
|
||||
return 'number';
|
||||
} else if (val instanceof String) {
|
||||
return 'string';
|
||||
} else if (val instanceof Boolean) {
|
||||
return 'boolean';
|
||||
} else if (Array.isArray(val)) {
|
||||
return 'array';
|
||||
} else if (val === null) {
|
||||
return 'null';
|
||||
} else {
|
||||
return typeof val;
|
||||
}
|
||||
}
|
||||
22
node_modules/@mapbox/mapbox-gl-style-spec/util/interpolate.js
generated
vendored
Normal file
22
node_modules/@mapbox/mapbox-gl-style-spec/util/interpolate.js
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
// @flow
|
||||
|
||||
import Color from './color.js';
|
||||
|
||||
export function number(a: number, b: number, t: number): number {
|
||||
return (a * (1 - t)) + (b * t);
|
||||
}
|
||||
|
||||
export function color(from: Color, to: Color, t: number): Color {
|
||||
return new Color(
|
||||
number(from.r, to.r, t),
|
||||
number(from.g, to.g, t),
|
||||
number(from.b, to.b, t),
|
||||
number(from.a, to.a, t)
|
||||
);
|
||||
}
|
||||
|
||||
export function array(from: Array<number>, to: Array<number>, t: number): Array<number> {
|
||||
return from.map((d, i) => {
|
||||
return number(d, to[i], t);
|
||||
});
|
||||
}
|
||||
15
node_modules/@mapbox/mapbox-gl-style-spec/util/properties.js
generated
vendored
Normal file
15
node_modules/@mapbox/mapbox-gl-style-spec/util/properties.js
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
// @flow
|
||||
|
||||
import type {StylePropertySpecification} from '../style-spec.js';
|
||||
|
||||
export function supportsPropertyExpression(spec: StylePropertySpecification): boolean {
|
||||
return spec['property-type'] === 'data-driven';
|
||||
}
|
||||
|
||||
export function supportsZoomExpression(spec: StylePropertySpecification): boolean {
|
||||
return !!spec.expression && spec.expression.parameters.indexOf('zoom') > -1;
|
||||
}
|
||||
|
||||
export function supportsInterpolation(spec: StylePropertySpecification): boolean {
|
||||
return !!spec.expression && spec.expression.interpolated;
|
||||
}
|
||||
2
node_modules/@mapbox/mapbox-gl-style-spec/util/ref_properties.js
generated
vendored
Normal file
2
node_modules/@mapbox/mapbox-gl-style-spec/util/ref_properties.js
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
// @flow
|
||||
export default ['type', 'source', 'source-layer', 'minzoom', 'maxzoom', 'filter', 'layout'];
|
||||
19
node_modules/@mapbox/mapbox-gl-style-spec/util/result.js
generated
vendored
Normal file
19
node_modules/@mapbox/mapbox-gl-style-spec/util/result.js
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
// @flow
|
||||
|
||||
/**
|
||||
* A type used for returning and propagating errors. The first element of the union
|
||||
* represents success and contains a value, and the second represents an error and
|
||||
* contains an error value.
|
||||
* @private
|
||||
*/
|
||||
export type Result<T, E> =
|
||||
| {| result: 'success', value: T |}
|
||||
| {| result: 'error', value: E |};
|
||||
|
||||
export function success<T, E>(value: T): Result<T, E> {
|
||||
return {result: 'success', value};
|
||||
}
|
||||
|
||||
export function error<T, E>(value: E): Result<T, E> {
|
||||
return {result: 'error', value};
|
||||
}
|
||||
24
node_modules/@mapbox/mapbox-gl-style-spec/util/unbundle_jsonlint.js
generated
vendored
Normal file
24
node_modules/@mapbox/mapbox-gl-style-spec/util/unbundle_jsonlint.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
// @flow
|
||||
|
||||
// Turn jsonlint-lines-primitives objects into primitive objects
|
||||
export function unbundle(value: mixed): mixed {
|
||||
if (value instanceof Number || value instanceof String || value instanceof Boolean) {
|
||||
return value.valueOf();
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
export function deepUnbundle(value: mixed): mixed {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(deepUnbundle);
|
||||
} else if (value instanceof Object && !(value instanceof Number || value instanceof String || value instanceof Boolean)) {
|
||||
const unbundledValue: { [key: string]: mixed } = {};
|
||||
for (const key in value) {
|
||||
unbundledValue[key] = deepUnbundle(value[key]);
|
||||
}
|
||||
return unbundledValue;
|
||||
}
|
||||
|
||||
return unbundle(value);
|
||||
}
|
||||
Reference in New Issue
Block a user