@versatiles/style 4.2.8 → 4.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/README.MD CHANGED
@@ -191,5 +191,5 @@ Iconsets can be defined in [`scripts/config-sprites.ts`](./scripts/config-sprite
191
191
 
192
192
  # Licenses
193
193
 
194
- * Sourcecode: [Unlicense](./UNLICENSE.md)
194
+ * Sourcecode: [Unlicense](./LICENSE.md)
195
195
  * Iconsets and rendered Spritemaps: [CC0 1.0 Universal](./icons/LICENSE.md)
@@ -1,3 +1,4 @@
1
1
  import type { MaplibreLayer } from '../types/index.js';
2
2
  import type { StyleRules } from './types.js';
3
- export declare function decorate(layers: MaplibreLayer[], rules: StyleRules): MaplibreLayer[];
3
+ import type { CachedRecolor } from './recolor.ts';
4
+ export declare function decorate(layers: MaplibreLayer[], rules: StyleRules, recolor: CachedRecolor): MaplibreLayer[];
@@ -2,7 +2,7 @@ import Color from 'color';
2
2
  import expandBraces from 'brace-expansion';
3
3
  import maplibreProperties from '../shortbread/properties.js';
4
4
  import { deepMerge } from '../lib/utils.js';
5
- export function decorate(layers, rules) {
5
+ export function decorate(layers, rules, recolor) {
6
6
  const layerIds = layers.map(l => l.id);
7
7
  const layerIdSet = new Set(layerIds);
8
8
  // Initialize a new map to hold final styles for layers
@@ -39,89 +39,90 @@ export function decorate(layers, rules) {
39
39
  processStyling(layer, layerStyle);
40
40
  return [layer];
41
41
  });
42
- }
43
- // Function to process each style attribute for the layer
44
- function processStyling(layer, styleRule) {
45
- for (const [ruleKeyCamelCase, ruleValue] of Object.entries(styleRule)) {
46
- if (ruleValue == null)
47
- continue;
48
- // CamelCase to not-camel-case
49
- const ruleKey = ruleKeyCamelCase.replace(/[A-Z]/g, c => '-' + c.toLowerCase());
50
- const propertyDefs = maplibreProperties.get(layer.type + '/' + ruleKey);
51
- if (!propertyDefs)
52
- continue;
53
- propertyDefs.forEach(propertyDef => {
54
- const { key } = propertyDef;
55
- let value = ruleValue;
56
- switch (propertyDef.valueType) {
57
- case 'color':
58
- value = processExpression(value, processColor);
59
- break;
60
- case 'fonts':
61
- value = processExpression(value, processFont);
62
- break;
63
- case 'resolvedImage':
64
- case 'formatted':
65
- case 'array':
66
- case 'boolean':
67
- case 'enum':
68
- case 'number':
69
- value = processExpression(value);
70
- break;
71
- default: throw new Error(`unknown propertyDef.valueType "${propertyDef.valueType}" for key "${key}"`);
42
+ // Function to process each style attribute for the layer
43
+ function processStyling(layer, styleRule) {
44
+ for (const [ruleKeyCamelCase, ruleValue] of Object.entries(styleRule)) {
45
+ if (ruleValue == null)
46
+ continue;
47
+ // CamelCase to not-camel-case
48
+ const ruleKey = ruleKeyCamelCase.replace(/[A-Z]/g, c => '-' + c.toLowerCase());
49
+ const propertyDefs = maplibreProperties.get(layer.type + '/' + ruleKey);
50
+ if (!propertyDefs)
51
+ continue;
52
+ propertyDefs.forEach(propertyDef => {
53
+ const { key } = propertyDef;
54
+ let value = ruleValue;
55
+ switch (propertyDef.valueType) {
56
+ case 'color':
57
+ value = processExpression(value, processColor);
58
+ break;
59
+ case 'fonts':
60
+ value = processExpression(value, processFont);
61
+ break;
62
+ case 'resolvedImage':
63
+ case 'formatted':
64
+ case 'array':
65
+ case 'boolean':
66
+ case 'enum':
67
+ case 'number':
68
+ value = processExpression(value);
69
+ break;
70
+ default: throw new Error(`unknown propertyDef.valueType "${propertyDef.valueType}" for key "${key}"`);
71
+ }
72
+ switch (propertyDef.parent) {
73
+ case 'layer':
74
+ // @ts-expect-error: too complex to handle
75
+ layer[key] = value;
76
+ break;
77
+ case 'layout':
78
+ if (!layer.layout)
79
+ layer.layout = {};
80
+ // @ts-expect-error: too complex to handle
81
+ layer.layout[key] = value;
82
+ break;
83
+ case 'paint':
84
+ if (!layer.paint)
85
+ layer.paint = {};
86
+ // @ts-expect-error: too complex to handle
87
+ layer.paint[key] = value;
88
+ break;
89
+ default:
90
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
91
+ throw new Error(`unknown parent "${propertyDef.parent}" for key "${key}"`);
92
+ }
93
+ });
94
+ }
95
+ function processColor(value) {
96
+ if (typeof value === 'string')
97
+ value = Color(value);
98
+ if (value instanceof Color) {
99
+ const color = recolor.do(value);
100
+ const text = (color.alpha() === 1) ? color.hex() : color.string();
101
+ return text.toLowerCase();
72
102
  }
73
- switch (propertyDef.parent) {
74
- case 'layer':
75
- // @ts-expect-error: too complex to handle
76
- layer[key] = value;
77
- break;
78
- case 'layout':
79
- if (!layer.layout)
80
- layer.layout = {};
81
- // @ts-expect-error: too complex to handle
82
- layer.layout[key] = value;
83
- break;
84
- case 'paint':
85
- if (!layer.paint)
86
- layer.paint = {};
87
- // @ts-expect-error: too complex to handle
88
- layer.paint[key] = value;
89
- break;
90
- default:
91
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
92
- throw new Error(`unknown parent "${propertyDef.parent}" for key "${key}"`);
103
+ throw new Error(`unknown color type "${typeof value}"`);
104
+ }
105
+ function processFont(value) {
106
+ if (typeof value === 'string')
107
+ return [value];
108
+ throw new Error(`unknown font type "${typeof value}"`);
109
+ }
110
+ function processExpression(value, cbValue) {
111
+ if (typeof value === 'object') {
112
+ if (value instanceof Color)
113
+ return processColor(value);
114
+ if (!Array.isArray(value)) {
115
+ return processZoomStops(value, cbValue);
116
+ }
93
117
  }
94
- });
95
- }
96
- }
97
- function processColor(value) {
98
- if (typeof value === 'string')
99
- value = Color(value);
100
- if (value instanceof Color) {
101
- value = (value.alpha() === 1) ? value.hex() : value.string();
102
- return value.toLowerCase();
103
- }
104
- throw new Error(`unknown color type "${typeof value}"`);
105
- }
106
- function processFont(value) {
107
- if (typeof value === 'string')
108
- return [value];
109
- throw new Error(`unknown font type "${typeof value}"`);
110
- }
111
- function processExpression(value, cbValue) {
112
- if (typeof value === 'object') {
113
- if (value instanceof Color)
114
- return processColor(value);
115
- if (!Array.isArray(value)) {
116
- return processZoomStops(value, cbValue);
118
+ return cbValue ? cbValue(value) : value;
119
+ }
120
+ function processZoomStops(obj, cbValue) {
121
+ return {
122
+ stops: Object.entries(obj)
123
+ .map(([z, v]) => [parseInt(z, 10), cbValue ? cbValue(v) : v])
124
+ .sort((a, b) => a[0] - b[0]),
125
+ };
117
126
  }
118
127
  }
119
- return cbValue ? cbValue(value) : value;
120
- }
121
- function processZoomStops(obj, cbValue) {
122
- return {
123
- stops: Object.entries(obj)
124
- .map(([z, v]) => [parseInt(z, 10), cbValue ? cbValue(v) : v])
125
- .sort((a, b) => a[0] - b[0]),
126
- };
127
128
  }
@@ -1,8 +1,7 @@
1
- import type StyleBuilder from './style_builder.js';
2
- import type { StyleBuilderColors } from './types.js';
1
+ import Color from 'color';
3
2
  export interface RecolorOptions {
4
3
  /** If true, inverts the colors. */
5
- invert?: boolean;
4
+ invertBrightness?: boolean;
6
5
  /** The degree to rotate the hue of the color (in degrees). */
7
6
  rotate?: number;
8
7
  /** Adjusts the saturation level of the color. Positive values increase saturation, negative values decrease it. */
@@ -19,4 +18,12 @@ export interface RecolorOptions {
19
18
  tintColor?: string;
20
19
  }
21
20
  export declare function getDefaultRecolorFlags(): RecolorOptions;
22
- export declare function recolor<Subclass extends StyleBuilder<Subclass>>(colors: StyleBuilderColors<Subclass>, opt?: RecolorOptions): void;
21
+ export declare function recolorObject(colors: Record<string, Color>, opt?: RecolorOptions): void;
22
+ export declare class CachedRecolor {
23
+ private readonly skip;
24
+ private readonly opt?;
25
+ private readonly cache;
26
+ constructor(opt?: RecolorOptions);
27
+ do(color: Color): Color;
28
+ }
29
+ export declare function recolor(color: Color, opt?: RecolorOptions): Color;
@@ -1,7 +1,7 @@
1
1
  import Color from 'color';
2
2
  export function getDefaultRecolorFlags() {
3
3
  return {
4
- invert: false,
4
+ invertBrightness: false,
5
5
  rotate: 0,
6
6
  saturate: 0,
7
7
  gamma: 1,
@@ -11,81 +11,112 @@ export function getDefaultRecolorFlags() {
11
11
  tintColor: '#FF0000',
12
12
  };
13
13
  }
14
- export function recolor(colors, opt) {
14
+ function isValidRecolorOptions(opt) {
15
15
  if (!opt)
16
+ return false;
17
+ if ((opt.invertBrightness != null) && opt.invertBrightness)
18
+ return true;
19
+ if ((opt.rotate != null) && (opt.rotate !== 0))
20
+ return true;
21
+ if ((opt.saturate != null) && (opt.saturate !== 0))
22
+ return true;
23
+ if ((opt.gamma != null) && (opt.gamma !== 1))
24
+ return true;
25
+ if ((opt.contrast != null) && (opt.contrast !== 1))
26
+ return true;
27
+ if ((opt.brightness != null) && (opt.brightness !== 0))
28
+ return true;
29
+ if ((opt.tint != null) && (opt.tint !== 0))
30
+ return true;
31
+ if ((opt.tintColor != null) && (opt.tintColor !== '#FF0000'))
32
+ return true;
33
+ return false;
34
+ }
35
+ export function recolorObject(colors, opt) {
36
+ if (!isValidRecolorOptions(opt))
16
37
  return;
17
- if (opt.invert ?? false)
18
- invert();
38
+ for (const [k, c] of Object.entries(colors)) {
39
+ colors[k] = recolor(c, opt);
40
+ }
41
+ }
42
+ export class CachedRecolor {
43
+ skip;
44
+ opt;
45
+ cache;
46
+ constructor(opt) {
47
+ this.skip = !isValidRecolorOptions(opt);
48
+ this.cache = new Map();
49
+ this.opt = opt;
50
+ }
51
+ do(color) {
52
+ if (this.skip)
53
+ return color;
54
+ const key = color.string();
55
+ const result = this.cache.get(key);
56
+ if (result)
57
+ return result;
58
+ color = recolor(color, this.opt);
59
+ this.cache.set(key, color);
60
+ return color;
61
+ }
62
+ }
63
+ export function recolor(color, opt) {
64
+ if (!isValidRecolorOptions(opt))
65
+ return color;
66
+ if (opt.invertBrightness ?? false)
67
+ color = invert(color);
19
68
  if ((opt.rotate !== undefined) && (opt.rotate !== 0))
20
- rotate(opt.rotate);
69
+ color = color.rotate(opt.rotate);
21
70
  if ((opt.saturate !== undefined) && (opt.saturate !== 0))
22
- saturate(opt.saturate);
71
+ color = color.saturate(opt.saturate);
23
72
  if ((opt.gamma !== undefined) && (opt.gamma !== 1))
24
- gamma(opt.gamma);
73
+ color = gamma(color, opt.gamma);
25
74
  if ((opt.contrast !== undefined) && (opt.contrast !== 1))
26
- contrast(opt.contrast);
75
+ color = contrast(color, opt.contrast);
27
76
  if ((opt.brightness !== undefined) && (opt.brightness !== 0))
28
- brightness(opt.brightness);
77
+ color = brightness(color, opt.brightness);
29
78
  if ((opt.tint !== undefined) && (opt.tintColor !== undefined) && (opt.tint !== 0))
30
- tint(opt.tint, Color(opt.tintColor));
31
- function forEachColor(callback) {
32
- for (const k in colors)
33
- colors[k] = callback(colors[k]);
34
- }
35
- function invert() {
36
- forEachColor(c => c.negate());
37
- }
38
- function rotate(value) {
39
- forEachColor(c => c.rotate(value));
40
- }
41
- function saturate(value) {
42
- forEachColor(c => c.saturate(value));
79
+ color = tint(color, opt.tint, Color(opt.tintColor));
80
+ return color;
81
+ function invert(c) {
82
+ c = c.hsl();
83
+ return c.lightness(100 - c.lightness()).rgb();
43
84
  }
44
- function gamma(value) {
85
+ function gamma(c, value) {
45
86
  if (value < 1e-3)
46
87
  value = 1e-3;
47
88
  if (value > 1e3)
48
89
  value = 1e3;
49
- forEachColor(color => {
50
- const rgb = color.rgb().array();
51
- 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());
52
- });
90
+ const rgb = c.rgb().array();
91
+ return Color.rgb(Math.pow(rgb[0] / 255, value) * 255, Math.pow(rgb[1] / 255, value) * 255, Math.pow(rgb[2] / 255, value) * 255, c.alpha());
53
92
  }
54
- function contrast(value) {
93
+ function contrast(c, value) {
55
94
  if (value < 0)
56
95
  value = 0;
57
96
  if (value > 1e6)
58
97
  value = 1e6;
59
- forEachColor(color => {
60
- const rgb = color.rgb().array();
61
- 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());
62
- });
98
+ const rgb = c.rgb().array();
99
+ return Color.rgb((rgb[0] - 127.5) * value + 127.5, (rgb[1] - 127.5) * value + 127.5, (rgb[2] - 127.5) * value + 127.5, c.alpha());
63
100
  }
64
- function brightness(value) {
101
+ function brightness(c, value) {
65
102
  if (value < -1e6)
66
103
  value = -1e6;
67
104
  if (value > 1e6)
68
105
  value = 1e6;
69
106
  const a = 1 - Math.abs(value);
70
107
  const b = (value < 0) ? 0 : 255 * value;
71
- forEachColor(color => {
72
- const rgb = color.rgb().array();
73
- return Color.rgb(rgb[0] * a + b, rgb[1] * a + b, rgb[2] * a + b, color.alpha());
74
- });
108
+ const rgb = c.rgb().array();
109
+ return Color.rgb(rgb[0] * a + b, rgb[1] * a + b, rgb[2] * a + b, c.alpha());
75
110
  }
76
- function tint(value, tintColor) {
111
+ function tint(c, value, tintColor) {
77
112
  if (value < 0)
78
113
  value = 0;
79
114
  if (value > 1)
80
115
  value = 1;
81
- const tintColorHSV = tintColor.hsv().array();
82
- forEachColor(color => {
83
- const rgb0 = color.rgb().array();
84
- const hsv = color.hsv().array();
85
- // eslint-disable-next-line @typescript-eslint/prefer-destructuring
86
- hsv[0] = tintColorHSV[0];
87
- const rgbNew = Color.hsv(hsv).rgb().array();
88
- 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());
89
- });
116
+ const rgb0 = c.rgb().array();
117
+ const hsv = c.hsv().array();
118
+ hsv[0] = tintColor.hue();
119
+ const rgbNew = Color.hsv(hsv).rgb().array();
120
+ return Color.rgb(rgb0[0] * (1 - value) + value * rgbNew[0], rgb0[1] * (1 - value) + value * rgbNew[1], rgb0[2] * (1 - value) + value * rgbNew[2], c.alpha());
90
121
  }
91
122
  }
@@ -1,7 +1,7 @@
1
1
  import Color from 'color';
2
2
  import { getShortbreadTemplate, getShortbreadLayers } from '../shortbread/index.js';
3
3
  import { decorate } from './decorator.js';
4
- import { getDefaultRecolorFlags, recolor } from './recolor.js';
4
+ import { CachedRecolor, getDefaultRecolorFlags } from './recolor.js';
5
5
  import { deepClone, resolveUrl } from '../lib/utils.js';
6
6
  // StyleBuilder class definition
7
7
  export default class StyleBuilder {
@@ -21,8 +21,6 @@ export default class StyleBuilder {
21
21
  for (const key in options.colors)
22
22
  colors[key] = Color(options.colors[key]);
23
23
  }
24
- // transform colors
25
- recolor(colors, recolorOptions);
26
24
  const fonts = deepClone(this.defaultFonts);
27
25
  if (options.fonts) {
28
26
  for (const key in options.fonts) {
@@ -57,7 +55,7 @@ export default class StyleBuilder {
57
55
  throw Error('unknown layer type');
58
56
  });
59
57
  // apply layer rules
60
- layers = decorate(layers, layerStyleRules);
58
+ layers = decorate(layers, layerStyleRules, new CachedRecolor(recolorOptions));
61
59
  // hide labels, if wanted
62
60
  if (hideLabels)
63
61
  layers = layers.filter(l => l.type !== 'symbol');
@@ -0,0 +1,5 @@
1
+ import Colorful from './colorful.js';
2
+ export default class Eclipse extends Colorful {
3
+ readonly name: string;
4
+ constructor();
5
+ }
@@ -0,0 +1,11 @@
1
+ import Colorful from './colorful.js';
2
+ export default class Eclipse extends Colorful {
3
+ name = 'Eclipse';
4
+ constructor() {
5
+ super();
6
+ this.transformDefaultColors(color => {
7
+ color = color.hsl();
8
+ return color.lightness(100 - color.lightness()).rgb();
9
+ });
10
+ }
11
+ }
@@ -4,19 +4,23 @@ import type { MaplibreStyle } from '../types/maplibre.js';
4
4
  import type { RecolorOptions } from '../style_builder/recolor.js';
5
5
  export type { MaplibreStyle, RecolorOptions };
6
6
  import Colorful from './colorful.js';
7
+ import Eclipse from './eclipse.js';
7
8
  import Graybeard from './graybeard.js';
8
9
  import Neutrino from './neutrino.js';
9
10
  export type ColorfulOptions = StyleBuilderOptions<Colorful>;
11
+ export type EclipseOptions = StyleBuilderOptions<Eclipse>;
10
12
  export type GraybeardOptions = StyleBuilderOptions<Graybeard>;
11
13
  export type NeutrinoOptions = StyleBuilderOptions<Neutrino>;
12
- export type SomeOptions = ColorfulOptions | GraybeardOptions | NeutrinoOptions;
14
+ export type SomeOptions = ColorfulOptions | EclipseOptions | GraybeardOptions | NeutrinoOptions;
13
15
  type MakeStyle<T extends StyleBuilder<T>, O extends StyleBuilderOptions<T>> = ((options?: O) => MaplibreStyle) & {
14
16
  getOptions: () => O;
15
17
  };
16
18
  export type ColorfulBuilder = MakeStyle<Colorful, ColorfulOptions>;
19
+ export type EclipseBuilder = MakeStyle<Eclipse, EclipseOptions>;
17
20
  export type GraybeardBuilder = MakeStyle<Graybeard, GraybeardOptions>;
18
21
  export type NeutrinoBuilder = MakeStyle<Neutrino, NeutrinoOptions>;
19
22
  export type SomeBuilder = ColorfulBuilder | GraybeardBuilder | NeutrinoBuilder;
20
23
  export declare const colorful: ColorfulBuilder;
24
+ export declare const eclipse: EclipseBuilder;
21
25
  export declare const graybeard: GraybeardBuilder;
22
26
  export declare const neutrino: NeutrinoBuilder;
@@ -1,13 +1,17 @@
1
+ // import styles
1
2
  import Colorful from './colorful.js';
3
+ import Eclipse from './eclipse.js';
2
4
  import Graybeard from './graybeard.js';
3
5
  import Neutrino from './neutrino.js';
4
- function makeStyle(styleBuilder) {
6
+ function getStyleBuilder(styleBuilder) {
5
7
  const fn = function (options) {
6
8
  return new styleBuilder().build(options);
7
9
  };
8
10
  fn.getOptions = () => new styleBuilder().getDefaultOptions();
9
11
  return fn;
10
12
  }
11
- export const colorful = makeStyle(Colorful);
12
- export const graybeard = makeStyle(Graybeard);
13
- export const neutrino = makeStyle(Neutrino);
13
+ // generate style builders
14
+ export const colorful = getStyleBuilder(Colorful);
15
+ export const eclipse = getStyleBuilder(Eclipse);
16
+ export const graybeard = getStyleBuilder(Graybeard);
17
+ export const neutrino = getStyleBuilder(Neutrino);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versatiles/style",
3
- "version": "4.2.8",
3
+ "version": "4.4.0",
4
4
  "description": "Generate StyleJSON for MapLibre",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -46,26 +46,26 @@
46
46
  "@types/brace-expansion": "^1.1.2",
47
47
  "@types/inquirer": "^9.0.7",
48
48
  "@types/jest": "^29.5.12",
49
- "@types/node": "^20.11.24",
49
+ "@types/node": "^20.12.4",
50
50
  "@types/tar-stream": "^3.1.3",
51
- "@typescript-eslint/eslint-plugin": "^7.1.0",
52
- "@typescript-eslint/parser": "^7.1.0",
51
+ "@typescript-eslint/eslint-plugin": "^7.5.0",
52
+ "@typescript-eslint/parser": "^7.5.0",
53
53
  "@versatiles/container": "^1.2.1",
54
54
  "@versatiles/release-tool": "^1.2.2",
55
55
  "bin-pack": "^1.0.2",
56
56
  "eslint": "^8.57.0",
57
- "inquirer": "^9.2.15",
57
+ "inquirer": "^9.2.17",
58
58
  "jest": "^29.7.0",
59
59
  "jest-environment-jsdom": "^29.7.0",
60
60
  "jest-ts-webcompat-resolver": "^1.0.0",
61
- "npm-check-updates": "^16.14.15",
62
- "rollup": "^4.12.0",
61
+ "npm-check-updates": "^16.14.18",
62
+ "rollup": "^4.14.0",
63
63
  "rollup-plugin-dts": "^6.1.0",
64
- "sharp": "^0.33.2",
64
+ "sharp": "^0.33.3",
65
65
  "tar-stream": "^3.1.7",
66
66
  "ts-jest": "^29.1.2",
67
67
  "ts-node": "^10.9.2",
68
- "tsx": "^4.7.1",
69
- "typescript": "^5.3.3"
68
+ "tsx": "^4.7.2",
69
+ "typescript": "^5.4.4"
70
70
  }
71
71
  }