UNPKG

@elastic/ems-client

Version:

JavaScript client library for the Elastic Maps Service

93 lines (84 loc) 3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.colorizeColor = colorizeColor; exports.desaturateColor = desaturateColor; exports.toAbsoluteUrl = toAbsoluteUrl; var _chromaJs = _interopRequireDefault(require("chroma-js")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ /** * Resolves a target URL path relative to the host. * This is specifically useed by the Kibana proxy re-routing. * It also handles trailing slashes in tileApiUrl and fileApiUrl parameters. */ function toAbsoluteUrl(host, path) { if (!host) { return path; } const hostEndWithSlash = host[host.length - 1] === '/'; const pathStartsWithSlash = path[0] === '/'; if (hostEndWithSlash === true && pathStartsWithSlash === true) { return host + path.slice(1); } else if (hostEndWithSlash !== pathStartsWithSlash) { return host + path; } else { return host + '/' + path; } } /* Type with the possible ways to define a color in maplibre */ /* A selection of the different maplibre-gl-js properties that refer to a color. Taken from maplibre https://github.com/maplibre/maplibre-gl-js/blob/main/src/style-spec/types.g.ts */ /* Function to transform a maplibre color definition by a given function. */ function transformColor(paintColor, func) { if (typeof paintColor == 'string') { const modifiedColor = func(paintColor); return `rgba(${modifiedColor.rgba().join(',')})`; } else if (typeof paintColor == 'object' && 'stops' in paintColor && Array.isArray(paintColor?.stops)) { const stops = paintColor['stops'].map(stop => { const newColor = transformColor(stop[1], func); return [stop[0], newColor]; }); const newPaintColor = Object.assign({}, paintColor, { stops }); return newPaintColor; } else return paintColor; } /* This is the function used to generate the current EMS desaturated roadmap */ function desaturateColor(paintColor) { return transformColor(paintColor, color => { return (0, _chromaJs.default)(color).desaturate(1.1).brighten(0.33); }); } /* Blends an original maplibre color definition with a destination color. Accepts different blending modes and an additional `mix` option that needs a percentage. More details: https://gka.github.io/chroma.js/#chroma-blend https://gka.github.io/chroma.js/#chroma-mix */ function colorizeColor(sourceColor, destColor, operation = 'screen', percentage = 0.5) { return transformColor(sourceColor, color => { if (operation !== 'mix') { return _chromaJs.default.blend((0, _chromaJs.default)(color), destColor, operation); } else { return _chromaJs.default.mix((0, _chromaJs.default)(color), destColor, percentage); } }); }