@versatiles/style 3.4.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/LICENSE.md +24 -0
- package/README.MD +94 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +38 -0
- package/dist/lib/decorator.d.ts +3 -0
- package/dist/lib/decorator.js +123 -0
- package/dist/lib/decorator.test.d.ts +1 -0
- package/dist/lib/decorator.test.js +56 -0
- package/dist/lib/recolor.d.ts +13 -0
- package/dist/lib/recolor.js +92 -0
- package/dist/lib/recolor.test.d.ts +1 -0
- package/dist/lib/recolor.test.js +179 -0
- package/dist/lib/shortbread/layers.d.ts +7 -0
- package/dist/lib/shortbread/layers.js +508 -0
- package/dist/lib/shortbread/layers.test.d.ts +1 -0
- package/dist/lib/shortbread/layers.test.js +27 -0
- package/dist/lib/shortbread/properties.d.ts +7 -0
- package/dist/lib/shortbread/properties.js +124 -0
- package/dist/lib/shortbread/properties.test.d.ts +1 -0
- package/dist/lib/shortbread/properties.test.js +36 -0
- package/dist/lib/shortbread/template.d.ts +2 -0
- package/dist/lib/shortbread/template.js +339 -0
- package/dist/lib/shortbread/template.test.d.ts +1 -0
- package/dist/lib/shortbread/template.test.js +57 -0
- package/dist/lib/style_builder.d.ts +32 -0
- package/dist/lib/style_builder.js +84 -0
- package/dist/lib/style_builder.test.d.ts +1 -0
- package/dist/lib/style_builder.test.js +77 -0
- package/dist/lib/utils.d.ts +4 -0
- package/dist/lib/utils.js +106 -0
- package/dist/lib/utils.test.d.ts +1 -0
- package/dist/lib/utils.test.js +97 -0
- package/dist/styles/colorful.d.ts +53 -0
- package/dist/styles/colorful.js +871 -0
- package/dist/styles/graybeard.d.ts +5 -0
- package/dist/styles/graybeard.js +9 -0
- package/dist/styles/neutrino.d.ts +24 -0
- package/dist/styles/neutrino.js +356 -0
- package/package.json +60 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to [unlicense.org](https://unlicense.org/)
|
package/README.MD
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# VersaTiles Styles
|
|
2
|
+
|
|
3
|
+
Programatically generates Shortbread StyleJSON for MapLibre.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
## Styles
|
|
8
|
+
|
|
9
|
+
* Colorful - colorful, full featured map
|
|
10
|
+
* Neutrino - light basemap
|
|
11
|
+
* Graybeard - gray basemap
|
|
12
|
+
|
|
13
|
+
## Download
|
|
14
|
+
|
|
15
|
+
You can download the latest StyleJSONs from the [latest release](https://github.com/versatiles-org/versatiles-styles/releases/latest/).
|
|
16
|
+
We provide each style with and without labels, and also in multiple languages.
|
|
17
|
+
|
|
18
|
+
Be aware that styles already include `tiles.versatiles.org` as source for tiles, fonts (glyphs) and icons (sprites). So you might want to update the URLs in the JSON.
|
|
19
|
+
|
|
20
|
+
## as Node.js module
|
|
21
|
+
|
|
22
|
+
Install `versatiles-styles` via NPM:
|
|
23
|
+
```bash
|
|
24
|
+
npm install versatiles-styles
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Use it in Node.js:
|
|
28
|
+
```javascript
|
|
29
|
+
import { colorful } from 'versatiles-styles';
|
|
30
|
+
let style = colorful({language:'de'});
|
|
31
|
+
writeFileSync('style.json', JSON.stringify(style));
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## in a web browser
|
|
35
|
+
|
|
36
|
+
Download latest release:
|
|
37
|
+
```bash
|
|
38
|
+
wget "https://github.com/versatiles-org/versatiles-styles/releases/latest/download/versatiles-styles.js"
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Use it in:
|
|
42
|
+
```html
|
|
43
|
+
<div id="map"></div>
|
|
44
|
+
<script src="maplibre-gl.js"></script>
|
|
45
|
+
<script src="versatiles-styles.js"></script>
|
|
46
|
+
<script>
|
|
47
|
+
const baseUrl = new URL('/', window.location.href).href;
|
|
48
|
+
const map = new maplibregl.Map({
|
|
49
|
+
container: 'map',
|
|
50
|
+
style: versatiles_styles.colorful({
|
|
51
|
+
glyphsUrl: baseUrl + 'fonts/{fontstack}/{range}.pbf',
|
|
52
|
+
spriteUrl: baseUrl + 'sprites/sprites',
|
|
53
|
+
tilesUrl: [baseUrl + 'tiles/{z}/{x}/{y}'],
|
|
54
|
+
})
|
|
55
|
+
});
|
|
56
|
+
<script>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Generate styles
|
|
60
|
+
|
|
61
|
+
`npm run build`
|
|
62
|
+
|
|
63
|
+
## Add/edit styles
|
|
64
|
+
|
|
65
|
+
* style definitions in `src/styles/*.js`
|
|
66
|
+
* shortbread layer definition in `src/lib/shortbread_layers.js`
|
|
67
|
+
* style template in `src/lib/shortbread_template.json`
|
|
68
|
+
|
|
69
|
+
## Licenses
|
|
70
|
+
|
|
71
|
+
* Code: [Unlicense](LICENSE.md)
|
|
72
|
+
* Styles: [CC0 1.0 Universal](https://creativecommons.org/publicdomain/zero/1.0/)
|
|
73
|
+
|
|
74
|
+
# todo
|
|
75
|
+
|
|
76
|
+
* [x] convert 'in' to '==' if only one element
|
|
77
|
+
* [x] outlines for roads, buildings, boundaries
|
|
78
|
+
* [x] :en labels
|
|
79
|
+
* [x] links under other roads, sort by size
|
|
80
|
+
* [x] waterway width
|
|
81
|
+
* [x] rail colorscheme
|
|
82
|
+
* [x] bridge semitransparent
|
|
83
|
+
* [x] cycleways and bicycle-roads
|
|
84
|
+
* [x] trams
|
|
85
|
+
* [x] rail zooms 17+
|
|
86
|
+
* [x] minor rails
|
|
87
|
+
* [x] more sites and landcovers
|
|
88
|
+
* [x] airports
|
|
89
|
+
* [x] pois
|
|
90
|
+
* [x] sprites and patterns, revisit sprites
|
|
91
|
+
* [x] public transport signage
|
|
92
|
+
* [x] road signage
|
|
93
|
+
* [ ] labels for rivers, islands, places
|
|
94
|
+
* [ ] aggregate layers with same filters and style for smaller result
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Colorful } from './index.js';
|
|
2
|
+
import * as builderClasses from './index.js';
|
|
3
|
+
import StyleBuilder from './lib/style_builder.js';
|
|
4
|
+
describe('Style Builders', () => {
|
|
5
|
+
const styleNames = [
|
|
6
|
+
'Colorful',
|
|
7
|
+
'Graybeard',
|
|
8
|
+
'Neutrino',
|
|
9
|
+
];
|
|
10
|
+
it(`should have the correct ${styleNames.length} styles`, () => {
|
|
11
|
+
const keys1 = Array.from(Object.keys(builderClasses)).sort();
|
|
12
|
+
const keys2 = styleNames.sort();
|
|
13
|
+
expect(keys1).toEqual(keys2);
|
|
14
|
+
});
|
|
15
|
+
Object.entries(builderClasses).forEach(([styleName, builderClass]) => {
|
|
16
|
+
it(`should create and test an instance of ${styleName}`, () => {
|
|
17
|
+
const builder = new builderClass();
|
|
18
|
+
expect(builder).toBeInstanceOf(StyleBuilder);
|
|
19
|
+
expect(typeof builder.name).toBe('string');
|
|
20
|
+
builder.baseUrl = 'https://example.org';
|
|
21
|
+
const style = builder.build();
|
|
22
|
+
expect(JSON.stringify(style).length).toBeGreaterThan(50000);
|
|
23
|
+
expect(style.name).toBe('versatiles-' + styleName.toLowerCase());
|
|
24
|
+
expect(style.glyphs).toBe('https://example.org/assets/fonts/{fontstack}/{range}.pbf');
|
|
25
|
+
expect(style.sprite).toBe('https://example.org/assets/sprites/sprites');
|
|
26
|
+
expect(Object.keys(style.sources).join(',')).toBe('versatiles-shortbread');
|
|
27
|
+
// @ts-expect-error: Still Overwhelmed
|
|
28
|
+
expect(style.sources['versatiles-shortbread'].tiles).toEqual(['https://example.org/tiles/osm/{z}/{x}/{y}']);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
describe('Colorful', () => {
|
|
33
|
+
const colorful = new Colorful();
|
|
34
|
+
colorful.baseUrl = 'https://dev.null';
|
|
35
|
+
colorful.colors.commercial = '#f00';
|
|
36
|
+
const style = colorful.build();
|
|
37
|
+
expect(style.glyphs).toBe('https://dev.null/assets/fonts/{fontstack}/{range}.pbf');
|
|
38
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import Color from 'color';
|
|
2
|
+
import expandBraces from 'brace-expansion';
|
|
3
|
+
import maplibreProperties from './shortbread/properties.js';
|
|
4
|
+
import { deepMerge } from './utils.js';
|
|
5
|
+
export function decorate(layers, rules) {
|
|
6
|
+
const layerIds = layers.map(l => l.id);
|
|
7
|
+
const layerIdSet = new Set(layerIds);
|
|
8
|
+
// Initialize a new map to hold final styles for layers
|
|
9
|
+
const layerStyles = new Map();
|
|
10
|
+
// Iterate through the generated layer style rules
|
|
11
|
+
Object.entries(rules).forEach(([idDef, layerStyle]) => {
|
|
12
|
+
// Expand any braces in IDs and filter them through a RegExp if necessary
|
|
13
|
+
const ids = expandBraces(idDef).flatMap(id => {
|
|
14
|
+
if (!id.includes('*'))
|
|
15
|
+
return id;
|
|
16
|
+
const regExpString = id.replace(/[^a-z_:-]/g, c => {
|
|
17
|
+
if (c === '*')
|
|
18
|
+
return '[a-z_-]*';
|
|
19
|
+
throw new Error('unknown char to process. Do not know how to make a RegExp from: ' + JSON.stringify(c));
|
|
20
|
+
});
|
|
21
|
+
const regExp = new RegExp(`^${regExpString}$`, 'i');
|
|
22
|
+
return layerIds.filter(layerId => regExp.test(layerId));
|
|
23
|
+
});
|
|
24
|
+
ids.forEach(id => {
|
|
25
|
+
if (!layerIdSet.has(id))
|
|
26
|
+
return;
|
|
27
|
+
layerStyles.set(id, deepMerge(layerStyles.get(id) ?? {}, layerStyle));
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
// Deep clone the original layers and apply styles
|
|
31
|
+
return layers.flatMap(layer => {
|
|
32
|
+
// Get the id and style of the layer
|
|
33
|
+
const layerStyle = layerStyles.get(layer.id);
|
|
34
|
+
// Don't export layers that have no style
|
|
35
|
+
if (!layerStyle)
|
|
36
|
+
return [];
|
|
37
|
+
processStyling(layer, layerStyle);
|
|
38
|
+
return [layer];
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
// Function to process each style attribute for the layer
|
|
42
|
+
function processStyling(layer, styleRule) {
|
|
43
|
+
for (const [ruleKeyCamelCase, ruleValue] of Object.entries(styleRule)) {
|
|
44
|
+
// CamelCase to not-camel-case
|
|
45
|
+
const ruleKey = ruleKeyCamelCase.replace(/[A-Z]/g, c => '-' + c.toLowerCase());
|
|
46
|
+
const propertyDefs = maplibreProperties.get(layer.type + '/' + ruleKey);
|
|
47
|
+
if (!propertyDefs)
|
|
48
|
+
continue;
|
|
49
|
+
propertyDefs.forEach(propertyDef => {
|
|
50
|
+
const { key } = propertyDef;
|
|
51
|
+
let value = ruleValue;
|
|
52
|
+
switch (propertyDef.valueType) {
|
|
53
|
+
case 'color':
|
|
54
|
+
value = processExpression(value, processColor);
|
|
55
|
+
break;
|
|
56
|
+
case 'fonts':
|
|
57
|
+
value = processExpression(value, processFont);
|
|
58
|
+
break;
|
|
59
|
+
case 'resolvedImage':
|
|
60
|
+
case 'formatted':
|
|
61
|
+
case 'array':
|
|
62
|
+
case 'boolean':
|
|
63
|
+
case 'enum':
|
|
64
|
+
case 'number':
|
|
65
|
+
value = processExpression(value);
|
|
66
|
+
break;
|
|
67
|
+
default: throw new Error(`unknown propertyDef.valueType "${propertyDef.valueType}" for key "${key}"`);
|
|
68
|
+
}
|
|
69
|
+
switch (propertyDef.parent) {
|
|
70
|
+
case 'layer':
|
|
71
|
+
// @ts-expect-error: too complex to handle
|
|
72
|
+
layer[key] = value;
|
|
73
|
+
break;
|
|
74
|
+
case 'layout':
|
|
75
|
+
if (!layer.layout)
|
|
76
|
+
layer.layout = {};
|
|
77
|
+
// @ts-expect-error: too complex to handle
|
|
78
|
+
layer.layout[key] = value;
|
|
79
|
+
break;
|
|
80
|
+
case 'paint':
|
|
81
|
+
if (!layer.paint)
|
|
82
|
+
layer.paint = {};
|
|
83
|
+
// @ts-expect-error: too complex to handle
|
|
84
|
+
layer.paint[key] = value;
|
|
85
|
+
break;
|
|
86
|
+
default:
|
|
87
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
88
|
+
throw new Error(`unknown parent "${propertyDef.parent}" for key "${key}"`);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function processColor(value) {
|
|
94
|
+
if (typeof value === 'string')
|
|
95
|
+
value = Color(value);
|
|
96
|
+
if (value instanceof Color) {
|
|
97
|
+
value = (value.alpha() === 1) ? value.hex() : value.hexa();
|
|
98
|
+
return value.toLowerCase();
|
|
99
|
+
}
|
|
100
|
+
throw new Error(`unknown color type "${typeof value}"`);
|
|
101
|
+
}
|
|
102
|
+
function processFont(value) {
|
|
103
|
+
if (typeof value === 'string')
|
|
104
|
+
return [value];
|
|
105
|
+
throw new Error(`unknown font type "${typeof value}"`);
|
|
106
|
+
}
|
|
107
|
+
function processExpression(value, cbValue) {
|
|
108
|
+
if (typeof value === 'object') {
|
|
109
|
+
if (value instanceof Color)
|
|
110
|
+
return processColor(value);
|
|
111
|
+
if (!Array.isArray(value)) {
|
|
112
|
+
return processZoomStops(value, cbValue);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return cbValue ? cbValue(value) : value;
|
|
116
|
+
}
|
|
117
|
+
function processZoomStops(obj, cbValue) {
|
|
118
|
+
return {
|
|
119
|
+
stops: Object.entries(obj)
|
|
120
|
+
.map(([z, v]) => [parseInt(z, 10), cbValue ? cbValue(v) : v])
|
|
121
|
+
.sort((a, b) => a[0] - b[0]),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
+
import { decorate } from './decorator.js';
|
|
3
|
+
import Color from 'color';
|
|
4
|
+
describe('decorate function', () => {
|
|
5
|
+
const mockLayers = [
|
|
6
|
+
{ id: 'layer1', type: 'fill', layout: {}, paint: {} },
|
|
7
|
+
{ id: 'layer2', type: 'line', layout: {}, paint: {} },
|
|
8
|
+
];
|
|
9
|
+
const mockRules = {
|
|
10
|
+
'layer1': {
|
|
11
|
+
color: '#ff0000',
|
|
12
|
+
visibility: 'none',
|
|
13
|
+
},
|
|
14
|
+
'layer2': {
|
|
15
|
+
color: 'rgba(0, 255, 0, 0.5)',
|
|
16
|
+
visibility: 'visible',
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
it('should return an array of layers', () => {
|
|
20
|
+
const result = decorate(mockLayers, mockRules);
|
|
21
|
+
expect(Array.isArray(result)).toBe(true);
|
|
22
|
+
});
|
|
23
|
+
it('should apply styles from rules to the corresponding layers', () => {
|
|
24
|
+
const result = decorate(mockLayers, mockRules);
|
|
25
|
+
result.forEach(layer => {
|
|
26
|
+
if (layer.id === 'layer1') {
|
|
27
|
+
expect(layer.paint).toHaveProperty('fill-color', '#ff0000');
|
|
28
|
+
expect(layer.layout).toHaveProperty('visibility', 'none');
|
|
29
|
+
}
|
|
30
|
+
if (layer.id === 'layer2') {
|
|
31
|
+
expect(layer.paint).toHaveProperty('line-color', '#00ff0080');
|
|
32
|
+
expect(layer.layout).toHaveProperty('visibility', 'visible');
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
it('should handle color conversion correctly', () => {
|
|
37
|
+
const colorRule = {
|
|
38
|
+
'layer1': {
|
|
39
|
+
paintColor: new Color('#ff0000'),
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
const result = decorate(mockLayers, colorRule);
|
|
43
|
+
const layer0 = result[0];
|
|
44
|
+
expect(layer0).toBeDefined();
|
|
45
|
+
if (typeof layer0 === 'undefined')
|
|
46
|
+
return;
|
|
47
|
+
expect(layer0.paint).toBeDefined();
|
|
48
|
+
if (typeof layer0.paint === 'undefined')
|
|
49
|
+
return;
|
|
50
|
+
expect(layer0.paint).toHaveProperty('fill-color', '#ff0000');
|
|
51
|
+
});
|
|
52
|
+
it('should discard layers that have no style rules applied', () => {
|
|
53
|
+
const result = decorate(mockLayers, {});
|
|
54
|
+
expect(result.length).toBe(0);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { StylemakerColorLookup } from './style_builder.js';
|
|
2
|
+
export interface RecolorOptions {
|
|
3
|
+
invert?: boolean;
|
|
4
|
+
rotate?: number;
|
|
5
|
+
saturate?: number;
|
|
6
|
+
gamma?: number;
|
|
7
|
+
contrast?: number;
|
|
8
|
+
brightness?: number;
|
|
9
|
+
tint?: number;
|
|
10
|
+
tintColor?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function getDefaultRecolorFlags(): RecolorOptions;
|
|
13
|
+
export declare function recolor(colors: StylemakerColorLookup, opt?: RecolorOptions): void;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import Color from 'color';
|
|
2
|
+
export function getDefaultRecolorFlags() {
|
|
3
|
+
return {
|
|
4
|
+
invert: false,
|
|
5
|
+
rotate: 0,
|
|
6
|
+
saturate: 0,
|
|
7
|
+
gamma: 1,
|
|
8
|
+
contrast: 1,
|
|
9
|
+
brightness: 0,
|
|
10
|
+
tint: 0,
|
|
11
|
+
tintColor: '#FF0000',
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export function recolor(colors, opt) {
|
|
15
|
+
if (!opt)
|
|
16
|
+
return;
|
|
17
|
+
if (opt.invert ?? false)
|
|
18
|
+
invert();
|
|
19
|
+
if ((opt.rotate !== undefined) && (opt.rotate !== 0))
|
|
20
|
+
rotate(opt.rotate);
|
|
21
|
+
if ((opt.saturate !== undefined) && (opt.saturate !== 0))
|
|
22
|
+
saturate(opt.saturate);
|
|
23
|
+
if ((opt.gamma !== undefined) && (opt.gamma !== 1))
|
|
24
|
+
gamma(opt.gamma);
|
|
25
|
+
if ((opt.contrast !== undefined) && (opt.contrast !== 1))
|
|
26
|
+
contrast(opt.contrast);
|
|
27
|
+
if ((opt.brightness !== undefined) && (opt.brightness !== 0))
|
|
28
|
+
brightness(opt.brightness);
|
|
29
|
+
if ((opt.tint !== undefined) && (opt.tintColor !== undefined) && (opt.tint !== 0))
|
|
30
|
+
tint(opt.tint, Color(opt.tintColor));
|
|
31
|
+
function forEachColor(callback) {
|
|
32
|
+
Object.entries(colors).forEach(([k, c]) => colors[k] = callback(c));
|
|
33
|
+
}
|
|
34
|
+
function invert() {
|
|
35
|
+
forEachColor(c => c.negate());
|
|
36
|
+
}
|
|
37
|
+
function rotate(value) {
|
|
38
|
+
forEachColor(c => c.rotate(value));
|
|
39
|
+
}
|
|
40
|
+
function saturate(value) {
|
|
41
|
+
forEachColor(c => c.saturate(value));
|
|
42
|
+
}
|
|
43
|
+
function gamma(value) {
|
|
44
|
+
if (value < 1e-3)
|
|
45
|
+
value = 1e-3;
|
|
46
|
+
if (value > 1e3)
|
|
47
|
+
value = 1e3;
|
|
48
|
+
forEachColor(color => {
|
|
49
|
+
const rgb = color.rgb().array();
|
|
50
|
+
return Color.rgb(Math.pow(rgb[0] / 255, value) * 255, Math.pow(rgb[1] / 255, value) * 255, Math.pow(rgb[2] / 255, value) * 255, color.alpha());
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function contrast(value) {
|
|
54
|
+
if (value < 0)
|
|
55
|
+
value = 0;
|
|
56
|
+
if (value > 1e6)
|
|
57
|
+
value = 1e6;
|
|
58
|
+
forEachColor(color => {
|
|
59
|
+
const rgb = color.rgb().array();
|
|
60
|
+
return Color.rgb((rgb[0] - 127.5) * value + 127.5, (rgb[1] - 127.5) * value + 127.5, (rgb[2] - 127.5) * value + 127.5, color.alpha());
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function brightness(value) {
|
|
64
|
+
if (value < -1e6)
|
|
65
|
+
value = -1e6;
|
|
66
|
+
if (value > 1e6)
|
|
67
|
+
value = 1e6;
|
|
68
|
+
const a = 1 - Math.abs(value);
|
|
69
|
+
const b = (value < 0) ? 0 : 255 * value;
|
|
70
|
+
forEachColor(color => {
|
|
71
|
+
const rgb = color.rgb().array();
|
|
72
|
+
return Color.rgb(rgb[0] * a + b, rgb[1] * a + b, rgb[2] * a + b, color.alpha());
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
function tint(value, tintColor) {
|
|
76
|
+
if (value < 0)
|
|
77
|
+
value = 0;
|
|
78
|
+
if (value > 1)
|
|
79
|
+
value = 1;
|
|
80
|
+
const tintColorHSV = tintColor.hsv().array();
|
|
81
|
+
forEachColor(color => {
|
|
82
|
+
const rgb0 = color.rgb().array();
|
|
83
|
+
const hsv = color.hsv().array();
|
|
84
|
+
// eslint-disable-next-line @typescript-eslint/prefer-destructuring
|
|
85
|
+
hsv[0] = tintColorHSV[0];
|
|
86
|
+
hsv[1] *= tintColorHSV[1];
|
|
87
|
+
hsv[2] *= tintColorHSV[2];
|
|
88
|
+
const rgbNew = Color.hsv(hsv).rgb().array();
|
|
89
|
+
return Color.rgb(rgb0[0] * (1 - value) + value * rgbNew[0], rgb0[1] * (1 - value) + value * rgbNew[1], rgb0[2] * (1 - value) + value * rgbNew[2], color.alpha());
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { getDefaultRecolorFlags, recolor } from './recolor.js';
|
|
2
|
+
import Color from 'color';
|
|
3
|
+
describe('colorTransformer', () => {
|
|
4
|
+
describe('getDefaultRecolorFlags', () => {
|
|
5
|
+
it('should return the default color transformer flags', () => {
|
|
6
|
+
const defaultFlags = getDefaultRecolorFlags();
|
|
7
|
+
expect(defaultFlags).toEqual({
|
|
8
|
+
invert: false,
|
|
9
|
+
rotate: 0,
|
|
10
|
+
saturate: 0,
|
|
11
|
+
gamma: 1,
|
|
12
|
+
contrast: 1,
|
|
13
|
+
brightness: 0,
|
|
14
|
+
tint: 0,
|
|
15
|
+
tintColor: '#FF0000',
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
describe('recolor', () => {
|
|
20
|
+
it('should not alter the colors if no flags are provided', () => {
|
|
21
|
+
const colors = getDefaultColors();
|
|
22
|
+
recolor(colors, {});
|
|
23
|
+
expect(colors).toEqual(getDefaultColors());
|
|
24
|
+
});
|
|
25
|
+
describe('invert', () => {
|
|
26
|
+
it('should invert colors when invert flag is true', () => {
|
|
27
|
+
const colors = getDefaultColors();
|
|
28
|
+
recolor(colors, { invert: true });
|
|
29
|
+
expect(colors2string(colors)).toBe('0055AA00,FF005555,AAFF00AA,55AAFFFF');
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
describe('rotate', () => {
|
|
33
|
+
it('should rotate colors 120°', () => {
|
|
34
|
+
const colors = getDefaultColors();
|
|
35
|
+
recolor(colors, { rotate: 120 });
|
|
36
|
+
expect(colors2string(colors)).toBe('55FFAA00,AA00FF55,FF5500AA,00AA55FF');
|
|
37
|
+
});
|
|
38
|
+
it('should rotate colors 180°', () => {
|
|
39
|
+
const colors = getDefaultColors();
|
|
40
|
+
recolor(colors, { rotate: 180 });
|
|
41
|
+
expect(colors2string(colors)).toBe('55AAFF00,FF005555,AAFF00AA,0055AAFF');
|
|
42
|
+
});
|
|
43
|
+
it('should rotate colors 240°', () => {
|
|
44
|
+
const colors = getDefaultColors();
|
|
45
|
+
recolor(colors, { rotate: 240 });
|
|
46
|
+
expect(colors2string(colors)).toBe('AA55FF00,FFAA0055,00FF55AA,5500AAFF');
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
describe('saturation', () => {
|
|
50
|
+
it('should remove any saturation', () => {
|
|
51
|
+
const colors = getDefaultColors();
|
|
52
|
+
recolor(colors, { saturate: -1.0 });
|
|
53
|
+
expect(colors2string(colors)).toBe('AAAAAA00,80808055,808080AA,555555FF');
|
|
54
|
+
});
|
|
55
|
+
it('should decrease saturation', () => {
|
|
56
|
+
const colors = getDefaultColors();
|
|
57
|
+
recolor(colors, { saturate: -0.5 });
|
|
58
|
+
expect(colors2string(colors)).toBe('D4AA7F00,40BF9555,6A40BFAA,7F552AFF');
|
|
59
|
+
});
|
|
60
|
+
it('should increase saturation', () => {
|
|
61
|
+
const colors = getDefaultColors();
|
|
62
|
+
recolor(colors, { saturate: 0.5 });
|
|
63
|
+
expect(colors2string(colors)).toBe('FFAA2A00,00FFBF55,4000FFAA,D45500FF');
|
|
64
|
+
});
|
|
65
|
+
it('should maximize saturation', () => {
|
|
66
|
+
const colors = getDefaultColors();
|
|
67
|
+
recolor(colors, { saturate: 1.0 });
|
|
68
|
+
expect(colors2string(colors)).toBe('FFAA0000,00FFD455,2B00FFAA,FF5500FF');
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
describe('gamma', () => {
|
|
72
|
+
it('should decrease gamma', () => {
|
|
73
|
+
const colors = getDefaultColors();
|
|
74
|
+
recolor(colors, { gamma: 0.5 });
|
|
75
|
+
expect(colors2string(colors)).toBe('FFD09300,00FFD055,9300FFAA,D09300FF');
|
|
76
|
+
});
|
|
77
|
+
it('should increase gamma', () => {
|
|
78
|
+
const colors = getDefaultColors();
|
|
79
|
+
recolor(colors, { gamma: 2 });
|
|
80
|
+
expect(colors2string(colors)).toBe('FF711C00,00FF7155,1C00FFAA,711C00FF');
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
describe('contrast', () => {
|
|
84
|
+
it('should remove any contrast', () => {
|
|
85
|
+
const colors = getDefaultColors();
|
|
86
|
+
recolor(colors, { contrast: 0 });
|
|
87
|
+
expect(colors2string(colors)).toBe('80808000,80808055,808080AA,808080FF');
|
|
88
|
+
});
|
|
89
|
+
it('should decrease contrast', () => {
|
|
90
|
+
const colors = getDefaultColors();
|
|
91
|
+
recolor(colors, { contrast: 0.5 });
|
|
92
|
+
expect(colors2string(colors)).toBe('BF956A00,40BF9555,6A40BFAA,956A40FF');
|
|
93
|
+
});
|
|
94
|
+
it('should increase contrast', () => {
|
|
95
|
+
const colors = getDefaultColors();
|
|
96
|
+
recolor(colors, { contrast: 2 });
|
|
97
|
+
expect(colors2string(colors)).toBe('FFD52B00,00FFD555,2B00FFAA,D52B00FF');
|
|
98
|
+
});
|
|
99
|
+
it('should maximize contrast', () => {
|
|
100
|
+
const colors = getDefaultColors();
|
|
101
|
+
recolor(colors, { contrast: Infinity });
|
|
102
|
+
expect(colors2string(colors)).toBe('FFFF0000,00FFFF55,0000FFAA,FF0000FF');
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
describe('contrast', () => {
|
|
106
|
+
it('should remove any brightness', () => {
|
|
107
|
+
const colors = getDefaultColors();
|
|
108
|
+
recolor(colors, { brightness: -1 });
|
|
109
|
+
expect(colors2string(colors)).toBe('00000000,00000055,000000AA,000000FF');
|
|
110
|
+
});
|
|
111
|
+
it('should decrease brightness', () => {
|
|
112
|
+
const colors = getDefaultColors();
|
|
113
|
+
recolor(colors, { brightness: -0.5 });
|
|
114
|
+
expect(colors2string(colors)).toBe('80552B00,00805555,2B0080AA,552B00FF');
|
|
115
|
+
});
|
|
116
|
+
it('should increase brightness', () => {
|
|
117
|
+
const colors = getDefaultColors();
|
|
118
|
+
recolor(colors, { brightness: 0.5 });
|
|
119
|
+
expect(colors2string(colors)).toBe('FFD5AA00,80FFD555,AA80FFAA,D5AA80FF');
|
|
120
|
+
});
|
|
121
|
+
it('should maximize brightness', () => {
|
|
122
|
+
const colors = getDefaultColors();
|
|
123
|
+
recolor(colors, { brightness: 1 });
|
|
124
|
+
expect(colors2string(colors)).toBe('FFFFFF00,FFFFFF55,FFFFFFAA,FFFFFFFF');
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
describe('tint', () => {
|
|
128
|
+
it('should not tint at all', () => {
|
|
129
|
+
const colors = getDefaultColors();
|
|
130
|
+
recolor(colors, { tint: 0, tintColor: '#F00' });
|
|
131
|
+
expect(colors2string(colors)).toBe('FFAA5500,00FFAA55,5500FFAA,AA5500FF');
|
|
132
|
+
});
|
|
133
|
+
it('should tint a little bit red', () => {
|
|
134
|
+
const colors = getDefaultColors();
|
|
135
|
+
recolor(colors, { tint: 0.5, tintColor: '#F00' });
|
|
136
|
+
expect(colors2string(colors)).toBe('FF552B00,80805555,AA0080AA,D52B00FF');
|
|
137
|
+
});
|
|
138
|
+
it('should tint a little bit yellow', () => {
|
|
139
|
+
const colors = getDefaultColors();
|
|
140
|
+
recolor(colors, { tint: 0.5, tintColor: '#FF0' });
|
|
141
|
+
expect(colors2string(colors)).toBe('FFD52B00,80FF5555,AA8080AA,D5AA00FF');
|
|
142
|
+
});
|
|
143
|
+
it('should tint a little bit green', () => {
|
|
144
|
+
const colors = getDefaultColors();
|
|
145
|
+
recolor(colors, { tint: 0.5, tintColor: '#0F0' });
|
|
146
|
+
expect(colors2string(colors)).toBe('80D52B00,00FF5555,2B8080AA,55AA00FF');
|
|
147
|
+
});
|
|
148
|
+
it('should tint a little bit blue', () => {
|
|
149
|
+
const colors = getDefaultColors();
|
|
150
|
+
recolor(colors, { tint: 0.5, tintColor: '#00F' });
|
|
151
|
+
expect(colors2string(colors)).toBe('8055AA00,0080D555,2B00FFAA,552B80FF');
|
|
152
|
+
});
|
|
153
|
+
it('should tint a strongly orange', () => {
|
|
154
|
+
const colors = getDefaultColors();
|
|
155
|
+
recolor(colors, { tint: 0.5, tintColor: '#F80' });
|
|
156
|
+
expect(colors2string(colors)).toBe('FF992B00,80C45555,AA4480AA,D56F00FF');
|
|
157
|
+
});
|
|
158
|
+
it('should tint a strongly blue', () => {
|
|
159
|
+
const colors = getDefaultColors();
|
|
160
|
+
recolor(colors, { tint: 0.5, tintColor: '#00F' });
|
|
161
|
+
expect(colors2string(colors)).toBe('8055AA00,0080D555,2B00FFAA,552B80FF');
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
function string2colors(colorList) {
|
|
167
|
+
return Object.fromEntries(colorList.split(',')
|
|
168
|
+
.map((value, index) => ['color' + index, Color('#' + value)]));
|
|
169
|
+
}
|
|
170
|
+
function colors2string(colors) {
|
|
171
|
+
const colorArray = [];
|
|
172
|
+
for (let i = 0; ('color' + i) in colors; i++) {
|
|
173
|
+
colorArray.push(colors['color' + i].hexa().slice(1));
|
|
174
|
+
}
|
|
175
|
+
return colorArray.join(',');
|
|
176
|
+
}
|
|
177
|
+
function getDefaultColors() {
|
|
178
|
+
return string2colors('FFAA5500,00FFAA55,5500FFAA,AA5500FF');
|
|
179
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { BackgroundLayer, FillLayer, LineLayer, SymbolLayer } from 'mapbox-gl';
|
|
2
|
+
import type { LanguageSuffix } from '../style_builder.js';
|
|
3
|
+
export type MaplibreLayer = BackgroundLayer | FillLayer | LineLayer | SymbolLayer;
|
|
4
|
+
export type MaplibreFilter = unknown[];
|
|
5
|
+
export default function getLayers(option: {
|
|
6
|
+
readonly languageSuffix: LanguageSuffix;
|
|
7
|
+
}): MaplibreLayer[];
|