2021-05-11 02:39:51 +02:00
|
|
|
import * as colors from "./assets/colors.json"
|
2021-07-10 21:03:41 +02:00
|
|
|
import {TileRange} from "./Models/TileRange";
|
2021-05-18 19:48:20 +02:00
|
|
|
|
2020-07-24 01:12:57 +02:00
|
|
|
export class Utils {
|
|
|
|
|
2021-01-06 02:21:50 +01:00
|
|
|
/**
|
|
|
|
* In the 'deploy'-step, some code needs to be run by ts-node.
|
|
|
|
* However, ts-node crashes when it sees 'document'. When running from console, we flag this and disable all code where document is needed.
|
|
|
|
* This is a workaround and yet another hack
|
|
|
|
*/
|
|
|
|
public static runningFromConsole = false;
|
2020-11-17 02:22:48 +01:00
|
|
|
public static readonly assets_path = "./assets/svg/";
|
2021-07-03 22:24:12 +02:00
|
|
|
public static externalDownloadFunction: (url: string) => Promise<any>;
|
2021-04-04 03:22:56 +02:00
|
|
|
private static knownKeys = ["addExtraTags", "and", "calculatedTags", "changesetmessage", "clustering", "color", "condition", "customCss", "dashArray", "defaultBackgroundId", "description", "descriptionTail", "doNotDownload", "enableAddNewPoints", "enableBackgroundLayerSelection", "enableGeolocation", "enableLayers", "enableMoreQuests", "enableSearch", "enableShareScreen", "enableUserBadge", "freeform", "hideFromOverview", "hideInAnswer", "icon", "iconOverlays", "iconSize", "id", "if", "ifnot", "isShown", "key", "language", "layers", "lockLocation", "maintainer", "mappings", "maxzoom", "maxZoom", "minNeededElements", "minzoom", "multiAnswer", "name", "or", "osmTags", "passAllFeatures", "presets", "question", "render", "roaming", "roamingRenderings", "rotation", "shortDescription", "socialImage", "source", "startLat", "startLon", "startZoom", "tagRenderings", "tags", "then", "title", "titleIcons", "type", "version", "wayHandling", "widenFactor", "width"]
|
|
|
|
private static extraKeys = ["nl", "en", "fr", "de", "pt", "es", "name", "phone", "email", "amenity", "leisure", "highway", "building", "yes", "no", "true", "false"]
|
2020-09-30 22:22:58 +02:00
|
|
|
|
|
|
|
static EncodeXmlValue(str) {
|
2021-05-18 19:48:20 +02:00
|
|
|
if (typeof str !== "string") {
|
|
|
|
str = "" + str
|
2021-05-09 18:56:51 +02:00
|
|
|
}
|
2021-05-18 19:48:20 +02:00
|
|
|
|
2020-09-30 22:22:58 +02:00
|
|
|
return str.replace(/&/g, '&')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''')
|
|
|
|
}
|
|
|
|
|
2020-07-24 01:12:57 +02:00
|
|
|
/**
|
|
|
|
* Gives a clean float, or undefined if parsing fails
|
|
|
|
* @param str
|
|
|
|
*/
|
|
|
|
static asFloat(str): number {
|
|
|
|
if (str) {
|
|
|
|
const i = parseFloat(str);
|
|
|
|
if (isNaN(i)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-09-30 22:22:58 +02:00
|
|
|
|
|
|
|
public static Upper(str: string) {
|
|
|
|
return str.substr(0, 1).toUpperCase() + str.substr(1);
|
|
|
|
}
|
|
|
|
|
2020-10-04 01:04:46 +02:00
|
|
|
public static TwoDigits(i: number) {
|
|
|
|
if (i < 10) {
|
|
|
|
return "0" + i;
|
|
|
|
}
|
|
|
|
return "" + i;
|
|
|
|
}
|
|
|
|
|
2020-10-30 00:56:46 +01:00
|
|
|
public static Round(i: number) {
|
2021-03-09 13:10:48 +01:00
|
|
|
if (i < 0) {
|
2020-10-30 00:56:46 +01:00
|
|
|
return "-" + Utils.Round(-i);
|
|
|
|
}
|
|
|
|
const j = "" + Math.floor(i * 10);
|
|
|
|
if (j.length == 1) {
|
|
|
|
return "0." + j;
|
|
|
|
}
|
|
|
|
return j.substr(0, j.length - 1) + "." + j.substr(j.length - 1, j.length);
|
|
|
|
}
|
|
|
|
|
2020-10-04 01:04:46 +02:00
|
|
|
public static Times(f: ((i: number) => string), count: number): string {
|
2020-09-30 22:22:58 +02:00
|
|
|
let res = "";
|
|
|
|
for (let i = 0; i < count; i++) {
|
2020-10-04 01:04:46 +02:00
|
|
|
res += f(i);
|
2020-09-30 22:22:58 +02:00
|
|
|
}
|
|
|
|
return res;
|
2020-07-24 14:46:25 +02:00
|
|
|
}
|
2020-07-31 04:58:58 +02:00
|
|
|
|
2021-07-03 22:24:12 +02:00
|
|
|
public static TimesT<T>(count: number, f: ((i: number) => T)): T[] {
|
|
|
|
let res: T[] = [];
|
2021-06-16 14:23:53 +02:00
|
|
|
for (let i = 0; i < count; i++) {
|
2021-07-03 22:24:12 +02:00
|
|
|
res.push(f(i));
|
2021-06-16 14:23:53 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-07-31 04:58:58 +02:00
|
|
|
static DoEvery(millis: number, f: (() => void)) {
|
2021-01-06 02:21:50 +01:00
|
|
|
if (Utils.runningFromConsole) {
|
2020-07-31 17:11:44 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-07-31 04:58:58 +02:00
|
|
|
window.setTimeout(
|
|
|
|
function () {
|
|
|
|
f();
|
|
|
|
Utils.DoEvery(millis, f);
|
|
|
|
}
|
|
|
|
, millis)
|
|
|
|
}
|
|
|
|
|
2020-08-08 02:16:42 +02:00
|
|
|
public static NoNull<T>(array: T[]): T[] {
|
|
|
|
const ls: T[] = [];
|
|
|
|
for (const t of array) {
|
|
|
|
if (t === undefined || t === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ls.push(t);
|
|
|
|
}
|
|
|
|
return ls;
|
|
|
|
}
|
2021-03-09 13:10:48 +01:00
|
|
|
|
|
|
|
public static NoEmpty(array: string[]): string[] {
|
2020-08-22 13:02:31 +02:00
|
|
|
const ls: string[] = [];
|
|
|
|
for (const t of array) {
|
|
|
|
if (t === "") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ls.push(t);
|
|
|
|
}
|
|
|
|
return ls;
|
|
|
|
}
|
2020-08-08 02:16:42 +02:00
|
|
|
|
2021-03-09 13:10:48 +01:00
|
|
|
public static EllipsesAfter(str: string, l: number = 100) {
|
2021-06-15 16:18:58 +02:00
|
|
|
if (str === undefined || str === null) {
|
2020-09-03 03:16:43 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
2021-03-09 13:10:48 +01:00
|
|
|
if (str.length <= l) {
|
2020-08-26 00:21:34 +02:00
|
|
|
return str;
|
|
|
|
}
|
2021-03-09 13:10:48 +01:00
|
|
|
return str.substr(0, l - 3) + "...";
|
2020-08-26 00:21:34 +02:00
|
|
|
}
|
2021-03-09 13:10:48 +01:00
|
|
|
|
|
|
|
public static Dedup(arr: string[]): string[] {
|
|
|
|
if (arr === undefined) {
|
2020-08-26 15:36:04 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const newArr = [];
|
|
|
|
for (const string of arr) {
|
2020-10-02 19:00:24 +02:00
|
|
|
if (newArr.indexOf(string) < 0) {
|
2020-08-26 15:36:04 +02:00
|
|
|
newArr.push(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newArr;
|
|
|
|
}
|
2021-07-27 19:35:43 +02:00
|
|
|
|
2020-10-02 19:00:24 +02:00
|
|
|
public static MergeTags(a: any, b: any) {
|
2020-08-27 00:08:00 +02:00
|
|
|
const t = {};
|
|
|
|
for (const k in a) {
|
|
|
|
t[k] = a[k];
|
|
|
|
}
|
|
|
|
for (const k in b) {
|
|
|
|
t[k] = b[k];
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
2020-10-02 19:00:24 +02:00
|
|
|
|
|
|
|
public static SplitFirst(a: string, sep: string): string[] {
|
2020-08-31 02:59:47 +02:00
|
|
|
const index = a.indexOf(sep);
|
2020-10-02 19:00:24 +02:00
|
|
|
if (index < 0) {
|
2020-08-31 02:59:47 +02:00
|
|
|
return [a];
|
|
|
|
}
|
2020-10-02 19:00:24 +02:00
|
|
|
return [a.substr(0, index), a.substr(index + sep.length)];
|
2020-08-31 02:59:47 +02:00
|
|
|
}
|
2020-07-31 04:58:58 +02:00
|
|
|
|
2021-06-14 02:39:23 +02:00
|
|
|
public static SubstituteKeys(txt: string, tags: any) {
|
|
|
|
for (const key in tags) {
|
2021-07-03 22:24:12 +02:00
|
|
|
if (!tags.hasOwnProperty(key)) {
|
2021-06-14 02:39:23 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
txt = txt.replace(new RegExp("{" + key + "}", "g"), tags[key])
|
|
|
|
}
|
|
|
|
return txt;
|
|
|
|
}
|
|
|
|
|
2021-03-09 13:10:48 +01:00
|
|
|
public static LoadCustomCss(location: string) {
|
2021-02-20 16:48:42 +01:00
|
|
|
const head = document.getElementsByTagName('head')[0];
|
|
|
|
const link = document.createElement('link');
|
2020-11-14 03:26:09 +01:00
|
|
|
link.id = "customCss";
|
|
|
|
link.rel = 'stylesheet';
|
|
|
|
link.type = 'text/css';
|
|
|
|
link.href = location;
|
|
|
|
link.media = 'all';
|
|
|
|
head.appendChild(link);
|
2021-03-09 13:10:48 +01:00
|
|
|
console.log("Added custom layout ", location)
|
2020-11-14 03:26:09 +01:00
|
|
|
}
|
2021-03-09 13:10:48 +01:00
|
|
|
|
2021-06-20 03:06:00 +02:00
|
|
|
/**
|
|
|
|
* Copies all key-value pairs of the source into the target.
|
|
|
|
* If the key starts with a '+', the values of the list will be appended to the target instead of overwritten
|
|
|
|
* @param source
|
|
|
|
* @param target
|
|
|
|
* @constructor
|
|
|
|
*/
|
2021-03-09 13:10:48 +01:00
|
|
|
static Merge(source: any, target: any) {
|
2021-01-06 02:52:38 +01:00
|
|
|
for (const key in source) {
|
2021-05-27 18:55:12 +02:00
|
|
|
if (!source.hasOwnProperty(key)) {
|
2021-05-19 22:38:05 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-06-20 03:06:00 +02:00
|
|
|
if (key.startsWith("+") || key.endsWith("+")) {
|
|
|
|
const trimmedKey = key.replace("+", "");
|
|
|
|
const sourceV = source[key];
|
|
|
|
const targetV = (target[trimmedKey] ?? [])
|
|
|
|
|
|
|
|
let newList: any[];
|
|
|
|
if (key.startsWith("+")) {
|
|
|
|
newList = sourceV.concat(targetV)
|
|
|
|
} else {
|
|
|
|
newList = targetV.concat(sourceV)
|
|
|
|
}
|
|
|
|
|
|
|
|
target[trimmedKey] = newList;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-06 02:52:38 +01:00
|
|
|
const sourceV = source[key];
|
|
|
|
const targetV = target[key]
|
2021-06-20 03:06:00 +02:00
|
|
|
if (sourceV?.length !== undefined && targetV?.length !== undefined && key.startsWith("+")) {
|
|
|
|
target[key] = targetV.concat(sourceV)
|
|
|
|
} else if (typeof sourceV === "object") {
|
2021-07-27 19:35:43 +02:00
|
|
|
if (sourceV === null) {
|
|
|
|
target[key] = null
|
|
|
|
} else if (targetV === undefined) {
|
2021-01-06 02:52:38 +01:00
|
|
|
target[key] = sourceV;
|
2021-03-09 13:10:48 +01:00
|
|
|
} else {
|
2021-01-06 02:52:38 +01:00
|
|
|
Utils.Merge(sourceV, targetV);
|
|
|
|
}
|
2021-03-09 13:10:48 +01:00
|
|
|
|
|
|
|
} else {
|
2021-01-06 02:52:38 +01:00
|
|
|
target[key] = sourceV;
|
|
|
|
}
|
2021-03-09 13:10:48 +01:00
|
|
|
|
2021-01-06 02:52:38 +01:00
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
2021-03-09 13:10:48 +01:00
|
|
|
|
|
|
|
static getOrSetDefault<K, V>(dict: Map<K, V>, k: K, v: () => V) {
|
|
|
|
let found = dict.get(k);
|
|
|
|
if (found !== undefined) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
dict.set(k, v());
|
|
|
|
return dict.get(k);
|
|
|
|
|
|
|
|
}
|
2021-03-14 20:15:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tile bounds of the
|
|
|
|
* @param z
|
|
|
|
* @param x
|
|
|
|
* @param y
|
2021-05-06 03:03:54 +02:00
|
|
|
* @returns [[maxlat, minlon], [minlat, maxlon]]
|
2021-03-14 20:15:11 +01:00
|
|
|
*/
|
|
|
|
static tile_bounds(z: number, x: number, y: number): [[number, number], [number, number]] {
|
|
|
|
return [[Utils.tile2lat(y, z), Utils.tile2long(x, z)], [Utils.tile2lat(y + 1, z), Utils.tile2long(x + 1, z)]]
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return x, y of the tile containing (lat, lon) on the given zoom level
|
|
|
|
*/
|
|
|
|
static embedded_tile(lat: number, lon: number, z: number): { x: number, y: number, z: number } {
|
|
|
|
return {x: Utils.lon2tile(lon, z), y: Utils.lat2tile(lat, z), z: z}
|
|
|
|
}
|
2021-05-06 03:03:54 +02:00
|
|
|
|
|
|
|
static TileRangeBetween(zoomlevel: number, lat0: number, lon0: number, lat1: number, lon1: number): TileRange {
|
2021-04-22 03:30:46 +02:00
|
|
|
const t0 = Utils.embedded_tile(lat0, lon0, zoomlevel)
|
|
|
|
const t1 = Utils.embedded_tile(lat1, lon1, zoomlevel)
|
|
|
|
|
|
|
|
const xstart = Math.min(t0.x, t1.x)
|
|
|
|
const xend = Math.max(t0.x, t1.x)
|
|
|
|
const ystart = Math.min(t0.y, t1.y)
|
|
|
|
const yend = Math.max(t0.y, t1.y)
|
|
|
|
const total = (1 + xend - xstart) * (1 + yend - ystart)
|
2021-05-06 03:03:54 +02:00
|
|
|
|
2021-04-22 03:30:46 +02:00
|
|
|
return {
|
|
|
|
xstart: xstart,
|
|
|
|
xend: xend,
|
|
|
|
ystart: ystart,
|
2021-05-06 03:03:54 +02:00
|
|
|
yend: yend,
|
2021-04-22 03:30:46 +02:00
|
|
|
total: total,
|
|
|
|
zoomlevel: zoomlevel
|
|
|
|
}
|
|
|
|
}
|
2021-03-14 20:15:11 +01:00
|
|
|
|
2021-04-04 03:22:56 +02:00
|
|
|
public static MinifyJSON(stringified: string): string {
|
|
|
|
stringified = stringified.replace(/\|/g, "||");
|
|
|
|
|
|
|
|
const keys = Utils.knownKeys.concat(Utils.extraKeys);
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const knownKey = keys[i];
|
|
|
|
let code = i;
|
|
|
|
if (i >= 124) {
|
|
|
|
code += 1; // Character 127 is our 'escape' character |
|
|
|
|
}
|
|
|
|
let replacement = "|" + String.fromCharCode(code)
|
|
|
|
stringified = stringified.replace(new RegExp(`\"${knownKey}\":`, "g"), replacement);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringified;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static UnMinify(minified: string): string {
|
|
|
|
|
2021-07-03 22:24:12 +02:00
|
|
|
if (minified === undefined || minified === null) {
|
2021-06-14 02:39:23 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
2021-07-03 22:24:12 +02:00
|
|
|
|
2021-04-04 03:22:56 +02:00
|
|
|
const parts = minified.split("|");
|
|
|
|
let result = parts.shift();
|
|
|
|
const keys = Utils.knownKeys.concat(Utils.extraKeys);
|
|
|
|
|
|
|
|
for (const part of parts) {
|
|
|
|
if (part == "") {
|
|
|
|
// Empty string => this was a || originally
|
|
|
|
result += "|"
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
const i = part.charCodeAt(0);
|
|
|
|
result += "\"" + keys[i] + "\":" + part.substring(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-18 19:48:20 +02:00
|
|
|
public static MapRange<T>(tileRange: TileRange, f: (x: number, y: number) => T): T[] {
|
|
|
|
const result: T[] = []
|
2021-05-06 03:03:54 +02:00
|
|
|
for (let x = tileRange.xstart; x <= tileRange.xend; x++) {
|
|
|
|
for (let y = tileRange.ystart; y <= tileRange.yend; y++) {
|
2021-05-18 19:48:20 +02:00
|
|
|
const t = f(x, y);
|
|
|
|
result.push(t)
|
2021-05-06 03:03:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2021-07-03 22:24:12 +02:00
|
|
|
|
|
|
|
public static downloadJson(url: string): Promise<any> {
|
|
|
|
if (this.externalDownloadFunction !== undefined) {
|
2021-06-22 14:21:32 +02:00
|
|
|
return this.externalDownloadFunction(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(
|
|
|
|
(resolve, reject) => {
|
2021-07-03 22:24:12 +02:00
|
|
|
try {
|
2021-06-22 14:21:32 +02:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.onload = () => {
|
|
|
|
if (xhr.status == 200) {
|
2021-07-03 22:24:12 +02:00
|
|
|
try {
|
|
|
|
resolve(JSON.parse(xhr.response))
|
|
|
|
} catch (e) {
|
|
|
|
reject("Not a valid json: " + xhr.response)
|
|
|
|
}
|
2021-06-22 14:21:32 +02:00
|
|
|
} else {
|
|
|
|
reject(xhr.statusText)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.open('GET', url);
|
2021-07-03 22:24:12 +02:00
|
|
|
xhr.setRequestHeader("accept", "application/json")
|
2021-06-22 14:21:32 +02:00
|
|
|
xhr.send();
|
2021-07-03 22:24:12 +02:00
|
|
|
} catch (e) {
|
2021-06-22 14:21:32 +02:00
|
|
|
reject(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2021-07-03 22:24:12 +02:00
|
|
|
|
2021-06-22 14:21:32 +02:00
|
|
|
}
|
2021-05-07 13:17:42 +02:00
|
|
|
|
2021-05-10 23:46:19 +02:00
|
|
|
/**
|
|
|
|
* Triggers a 'download file' popup which will download the contents
|
|
|
|
*/
|
2021-07-27 19:59:41 +02:00
|
|
|
public static offerContentsAsDownloadableFile(contents: string | Blob, fileName: string = "download.txt",
|
|
|
|
options?: { mimetype: string }) {
|
2021-05-07 13:17:42 +02:00
|
|
|
const element = document.createElement("a");
|
2021-07-22 14:33:49 +02:00
|
|
|
let file;
|
2021-07-27 19:35:43 +02:00
|
|
|
if (typeof (contents) === "string") {
|
2021-07-27 19:59:41 +02:00
|
|
|
file = new Blob([contents], {type: options?.mimetype ?? 'text/plain'});
|
2021-07-27 19:35:43 +02:00
|
|
|
} else {
|
|
|
|
file = contents;
|
|
|
|
}
|
2021-05-07 13:17:42 +02:00
|
|
|
element.href = URL.createObjectURL(file);
|
|
|
|
element.download = fileName;
|
|
|
|
document.body.appendChild(element); // Required for this to work in FireFox
|
|
|
|
element.click();
|
|
|
|
}
|
2021-05-18 19:48:20 +02:00
|
|
|
|
|
|
|
public static ColourNameToHex(color: string): string {
|
2021-05-11 02:39:51 +02:00
|
|
|
return colors[color.toLowerCase()] ?? color;
|
|
|
|
}
|
2021-05-18 19:48:20 +02:00
|
|
|
|
|
|
|
public static HexToColourName(hex: string): string {
|
2021-05-11 02:39:51 +02:00
|
|
|
hex = hex.toLowerCase()
|
2021-05-18 19:48:20 +02:00
|
|
|
if (!hex.startsWith("#")) {
|
2021-05-11 02:39:51 +02:00
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
const c = Utils.color(hex);
|
2021-05-18 19:48:20 +02:00
|
|
|
|
2021-05-11 02:39:51 +02:00
|
|
|
let smallestDiff = Number.MAX_VALUE;
|
|
|
|
let bestColor = undefined;
|
|
|
|
for (const color in colors) {
|
2021-05-18 19:48:20 +02:00
|
|
|
if (!colors.hasOwnProperty(color)) {
|
2021-05-11 02:39:51 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const foundhex = colors[color];
|
2021-05-18 19:48:20 +02:00
|
|
|
if (typeof foundhex !== "string") {
|
2021-05-11 02:39:51 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-05-18 19:48:20 +02:00
|
|
|
if (foundhex === hex) {
|
2021-05-11 02:39:51 +02:00
|
|
|
return color
|
|
|
|
}
|
|
|
|
const diff = this.colorDiff(Utils.color(foundhex), c)
|
2021-05-18 19:48:20 +02:00
|
|
|
if (diff > 50) {
|
2021-05-11 02:39:51 +02:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-18 19:48:20 +02:00
|
|
|
if (diff < smallestDiff) {
|
2021-05-11 02:39:51 +02:00
|
|
|
smallestDiff = diff;
|
|
|
|
bestColor = color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bestColor ?? hex;
|
|
|
|
}
|
2021-05-19 22:38:05 +02:00
|
|
|
|
2021-05-27 18:55:12 +02:00
|
|
|
|
2021-05-18 19:48:20 +02:00
|
|
|
private static tile2long(x, z) {
|
|
|
|
return (x / Math.pow(2, z) * 360 - 180);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static tile2lat(y, z) {
|
|
|
|
const n = Math.PI - 2 * Math.PI * y / Math.pow(2, z);
|
|
|
|
return (180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static lon2tile(lon, zoom) {
|
|
|
|
return (Math.floor((lon + 180) / 360 * Math.pow(2, zoom)));
|
2021-05-11 02:39:51 +02:00
|
|
|
}
|
2021-05-18 19:48:20 +02:00
|
|
|
|
|
|
|
private static lat2tile(lat, zoom) {
|
|
|
|
return (Math.floor((1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * Math.pow(2, zoom)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static colorDiff(c0: { r: number, g: number, b: number }, c1: { r: number, g: number, b: number }) {
|
|
|
|
return Math.abs(c0.r - c1.r) + Math.abs(c0.g - c1.g) + Math.abs(c0.b - c1.b);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static color(hex: string): { r: number, g: number, b: number } {
|
|
|
|
if (hex.startsWith == undefined) {
|
2021-05-11 02:39:51 +02:00
|
|
|
console.trace("WUT?", hex)
|
|
|
|
throw "wut?"
|
|
|
|
}
|
2021-05-18 19:48:20 +02:00
|
|
|
if (!hex.startsWith("#")) {
|
|
|
|
return undefined;
|
2021-05-11 02:39:51 +02:00
|
|
|
}
|
2021-05-18 19:48:20 +02:00
|
|
|
if (hex.length === 4) {
|
|
|
|
return {
|
|
|
|
r: parseInt(hex.substr(1, 1), 16),
|
|
|
|
g: parseInt(hex.substr(2, 1), 16),
|
|
|
|
b: parseInt(hex.substr(3, 1), 16),
|
|
|
|
}
|
2021-05-11 02:39:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-05-18 19:48:20 +02:00
|
|
|
r: parseInt(hex.substr(1, 2), 16),
|
|
|
|
g: parseInt(hex.substr(3, 2), 16),
|
|
|
|
b: parseInt(hex.substr(5, 2), 16),
|
2021-05-11 02:39:51 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-22 03:30:46 +02:00
|
|
|
|
2021-07-27 19:35:43 +02:00
|
|
|
public static setDefaults(options, defaults) {
|
|
|
|
for (let key in defaults) {
|
2021-07-15 10:56:30 +02:00
|
|
|
if (!(key in options)) options[key] = defaults[key];
|
2021-07-15 09:34:00 +02:00
|
|
|
}
|
2021-07-15 10:56:30 +02:00
|
|
|
return options;
|
2021-07-15 09:34:00 +02:00
|
|
|
}
|
2021-05-06 03:03:54 +02:00
|
|
|
}
|
2021-05-18 19:48:20 +02:00
|
|
|
|