mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
vectorBase: a colour is drawn as the rgba() it parses to, so the canvas never ignores one CSS would not read (an hsl() without %, an rgb() alpha without a slash); hue angles in turn, rad and grad are read in their units; a value evaluating to NaN is the property's default again, so a label never reads NaN
This commit is contained in:
1 parent
9160e48b40
commit
74e4ebcf65
3 files changed
+101
-15
No files matched your search
@@ -107,6 +107,22 @@ const NAMED_COLORS = Object.fromEntries([
|
|||||||
['transparent', [0, 0, 0, 0]],
|
['transparent', [0, 0, 0, 0]],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
/** Degrees in a turn, by CSS angle unit. */
|
||||||
|
const ANGLE_UNITS = { deg: 360, turn: 1, rad: 2 * Math.PI, grad: 400 };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A CSS hue (a number of degrees, or an angle in deg, turn, rad or grad)
|
||||||
|
* as a fraction of a turn in [0, 1); NaN for anything else.
|
||||||
|
* @param {string} text
|
||||||
|
*/
|
||||||
|
function hueOf(text) {
|
||||||
|
const m =
|
||||||
|
/^([+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)(deg|turn|rad|grad)?$/.exec(text);
|
||||||
|
if (!m) return NaN;
|
||||||
|
const turns = Number(m[1]) / ANGLE_UNITS[m[2] ?? 'deg'];
|
||||||
|
return ((turns % 1) + 1) % 1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A CSS colour as `[r, g, b, a]` (0..255, alpha 0..1): hex, `rgb()`,
|
* A CSS colour as `[r, g, b, a]` (0..255, alpha 0..1): hex, `rgb()`,
|
||||||
* `rgba()`, `hsl()`, `hsla()` and the CSS colour names; null for anything
|
* `rgba()`, `hsl()`, `hsla()` and the CSS colour names; null for anything
|
||||||
@@ -152,7 +168,7 @@ export function parseColor(text) {
|
|||||||
? null
|
? null
|
||||||
: [...rgb, alpha];
|
: [...rgb, alpha];
|
||||||
}
|
}
|
||||||
const hue = (((parseFloat(parts[0]) % 360) + 360) % 360) / 360;
|
const hue = hueOf(parts[0]);
|
||||||
const sat = parseFloat(parts[1]) / 100;
|
const sat = parseFloat(parts[1]) / 100;
|
||||||
const light = parseFloat(parts[2]) / 100;
|
const light = parseFloat(parts[2]) / 100;
|
||||||
if (![hue, sat, light, alpha].every(Number.isFinite)) return null;
|
if (![hue, sat, light, alpha].every(Number.isFinite)) return null;
|
||||||
|
|||||||
@@ -114,8 +114,8 @@ const KEYWORDS = {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* A value of a layer for a feature, or the property's default where it
|
* A value of a layer for a feature, or the property's default where it
|
||||||
* cannot be evaluated. MapLibre then also takes the default for one that is
|
* cannot be evaluated or is nothing (null, undefined or NaN). MapLibre then
|
||||||
* not of the property's type, which {@link numberOf}, {@link colorOf} and
|
* also takes the default for one that is not of the property's type, which {@link numberOf}, {@link colorOf} and
|
||||||
* {@link keywordOf} check: a canvas given a value it cannot use ignores it,
|
* {@link keywordOf} check: a canvas given a value it cannot use ignores it,
|
||||||
* so the last feature's would be drawn.
|
* so the last feature's would be drawn.
|
||||||
*/
|
*/
|
||||||
@@ -126,7 +126,9 @@ export function valueOf(layer, key, f) {
|
|||||||
} catch {
|
} catch {
|
||||||
v = undefined;
|
v = undefined;
|
||||||
}
|
}
|
||||||
return v === null || v === undefined ? layer.defaults?.[key] : v;
|
return v === null || v === undefined || Number.isNaN(v)
|
||||||
|
? layer.defaults?.[key]
|
||||||
|
: v;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A number value: a finite number, else the property's default. */
|
/** A number value: a finite number, else the property's default. */
|
||||||
@@ -138,14 +140,14 @@ export function numberOf(layer, key, f) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A colour value as CSS: one `parseColor` reads, else the default (or
|
* A colour value as CSS: one `parseColor` reads, written back as
|
||||||
* `or`).
|
* `rgba()` so the canvas reads it the same way, else the default (or `or`).
|
||||||
*/
|
*/
|
||||||
export function colorOf(layer, key, f, or = layer.defaults?.[key]) {
|
export function colorOf(layer, key, f, or = layer.defaults?.[key]) {
|
||||||
const css = (v) => {
|
const css = (v) => {
|
||||||
const parsed = parseColor(v);
|
const parsed = parseColor(v);
|
||||||
if (!parsed || !parsed.every(Number.isFinite)) return null;
|
if (!parsed || !parsed.every(Number.isFinite)) return null;
|
||||||
return typeof v === 'string' ? v : cssOf(parsed);
|
return cssOf(parsed);
|
||||||
};
|
};
|
||||||
return css(valueOf(layer, key, f)) ?? css(or);
|
return css(valueOf(layer, key, f)) ?? css(or);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -243,7 +243,7 @@ test('a style loads its TileJSON source, compiles its layers, and lists what it
|
|||||||
]);
|
]);
|
||||||
assert.equal(style.sources.base.maxzoom, 4);
|
assert.equal(style.sources.base.maxzoom, 4);
|
||||||
assert.deepEqual(style.attribution, ['Example']);
|
assert.deepEqual(style.attribution, ['Example']);
|
||||||
assert.deepEqual(style.background, { color: '#123456', opacity: 1 });
|
assert.deepEqual(style.background, { color: 'rgba(18,52,86,1)', opacity: 1 });
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
style.skipped.map((s) => s.id),
|
style.skipped.map((s) => s.id),
|
||||||
['lakes', 'patterned', 'hills'],
|
['lakes', 'patterned', 'hills'],
|
||||||
@@ -261,8 +261,11 @@ test('a tile paints its background, fills, lines and labels in style order', asy
|
|||||||
await paintVectorTile(style, 2, 1, 1, { size: 256, fetch, canvas });
|
await paintVectorTile(style, 2, 1, 1, { size: 256, fetch, canvas });
|
||||||
const set = (key) =>
|
const set = (key) =>
|
||||||
calls.filter(([k]) => k === `=${key}`).map(([, value]) => value);
|
calls.filter(([k]) => k === `=${key}`).map(([, value]) => value);
|
||||||
assert.deepEqual(set('fillStyle').slice(0, 2), ['#123456', '#0000ff']);
|
assert.deepEqual(set('fillStyle').slice(0, 2), [
|
||||||
assert.deepEqual(set('strokeStyle'), ['#ff0000']);
|
'rgba(18,52,86,1)',
|
||||||
|
'rgba(0,0,255,1)',
|
||||||
|
]);
|
||||||
|
assert.deepEqual(set('strokeStyle'), ['rgba(255,0,0,1)']);
|
||||||
assert.deepEqual(set('lineWidth'), [4]);
|
assert.deepEqual(set('lineWidth'), [4]);
|
||||||
const text = calls.filter(([k]) => k === 'fillText');
|
const text = calls.filter(([k]) => k === 'fillText');
|
||||||
assert.equal(text.length, 1);
|
assert.equal(text.length, 1);
|
||||||
@@ -462,7 +465,7 @@ test('a colour or width that evaluates to nothing is the property’s default, n
|
|||||||
assert.equal(fills.filter(([k]) => k === 'fill').length, 2);
|
assert.equal(fills.filter(([k]) => k === 'fill').length, 2);
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
fills.filter(([k]) => k === '=fillStyle').map(([, v]) => v),
|
fills.filter(([k]) => k === '=fillStyle').map(([, v]) => v),
|
||||||
['#00f', '#000000'],
|
['rgba(0,0,255,1)', 'rgba(0,0,0,1)'],
|
||||||
);
|
);
|
||||||
const line = [
|
const line = [
|
||||||
[
|
[
|
||||||
@@ -488,7 +491,7 @@ test('a colour or width that evaluates to nothing is the property’s default, n
|
|||||||
assert.equal(lines.filter(([k]) => k === 'stroke').length, 3);
|
assert.equal(lines.filter(([k]) => k === 'stroke').length, 3);
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
lines.filter(([k]) => k === '=strokeStyle').map(([, v]) => v),
|
lines.filter(([k]) => k === '=strokeStyle').map(([, v]) => v),
|
||||||
['#f00', '#000000', '#0f0'],
|
['rgba(255,0,0,1)', 'rgba(0,0,0,1)', 'rgba(0,255,0,1)'],
|
||||||
);
|
);
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
lines.filter(([k]) => k === '=lineWidth').map(([, v]) => v),
|
lines.filter(([k]) => k === '=lineWidth').map(([, v]) => v),
|
||||||
@@ -553,7 +556,7 @@ test('a step over a missing value, and a categorical miss with no default, are t
|
|||||||
);
|
);
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
calls.filter(([k]) => k === '=strokeStyle').map(([, v]) => v),
|
calls.filter(([k]) => k === '=strokeStyle').map(([, v]) => v),
|
||||||
['#f00', '#000000'],
|
['rgba(255,0,0,1)', 'rgba(0,0,0,1)'],
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -624,7 +627,7 @@ test('a colour, keyword or number of the wrong kind is the property’s default,
|
|||||||
);
|
);
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
fills.filter(([k]) => k === '=fillStyle').map(([, v]) => v),
|
fills.filter(([k]) => k === '=fillStyle').map(([, v]) => v),
|
||||||
['#00f', '#000000', 'orange'],
|
['rgba(0,0,255,1)', 'rgba(0,0,0,1)', 'rgba(255,165,0,1)'],
|
||||||
);
|
);
|
||||||
const line = [
|
const line = [
|
||||||
[
|
[
|
||||||
@@ -673,7 +676,7 @@ test('a style’s background takes the defaults where its values cannot be used'
|
|||||||
return (await loadVectorStyle('https://example.test/s.json', fetch))
|
return (await loadVectorStyle('https://example.test/s.json', fetch))
|
||||||
.background;
|
.background;
|
||||||
};
|
};
|
||||||
const black = { color: '#000000', opacity: 1 };
|
const black = { color: 'rgba(0,0,0,1)', opacity: 1 };
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
await background({ 'background-color': ['get', 'none'] }),
|
await background({ 'background-color': ['get', 'none'] }),
|
||||||
black,
|
black,
|
||||||
@@ -684,3 +687,68 @@ test('a style’s background takes the defaults where its values cannot be used'
|
|||||||
black,
|
black,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('a colour is drawn as the colour it reads as, in any syntax and hue unit', async () => {
|
||||||
|
const colours = [
|
||||||
|
'#00f',
|
||||||
|
'hsl(120, 50, 50)',
|
||||||
|
'rgb(255 0 0 0.5)',
|
||||||
|
'hsl(0.5turn 100% 50%)',
|
||||||
|
'hsl(3.14159rad, 100%, 50%)',
|
||||||
|
'hsl(200grad 100% 50%)',
|
||||||
|
'hsl(-120deg, 100%, 50%)',
|
||||||
|
'hsl(12 apples, 100%, 50%)',
|
||||||
|
];
|
||||||
|
const fills = await paintOne(
|
||||||
|
{
|
||||||
|
id: 'water',
|
||||||
|
type: 'fill',
|
||||||
|
'source-layer': 'water',
|
||||||
|
paint: { 'fill-color': ['get', 'c'] },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
water: colours.map((c) => ({
|
||||||
|
type: 'Polygon',
|
||||||
|
geometry: FULL,
|
||||||
|
properties: { c },
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
assert.deepEqual(
|
||||||
|
fills.filter(([k]) => k === '=fillStyle').map(([, v]) => v),
|
||||||
|
[
|
||||||
|
'rgba(0,0,255,1)',
|
||||||
|
'rgba(64,191,64,1)',
|
||||||
|
'rgba(255,0,0,0.5)',
|
||||||
|
'rgba(0,255,255,1)',
|
||||||
|
'rgba(0,255,255,1)',
|
||||||
|
'rgba(0,255,255,1)',
|
||||||
|
'rgba(0,0,255,1)',
|
||||||
|
'rgba(0,0,0,1)',
|
||||||
|
],
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a label whose text evaluates to NaN is the default, not "NaN"', async () => {
|
||||||
|
const calls = await paintOne(
|
||||||
|
{
|
||||||
|
id: 'places',
|
||||||
|
type: 'symbol',
|
||||||
|
'source-layer': 'places',
|
||||||
|
layout: {
|
||||||
|
'text-field': ['/', ['get', 'n'], ['get', 'n']],
|
||||||
|
'text-size': 12,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
places: [
|
||||||
|
{ type: 'Point', geometry: [[[1000, 1000]]], properties: { n: 0 } },
|
||||||
|
{ type: 'Point', geometry: [[[3000, 3000]]], properties: { n: 2 } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
assert.deepEqual(
|
||||||
|
calls.filter(([k]) => k === 'fillText').map(([, text]) => text),
|
||||||
|
['1'],
|
||||||
|
);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user