mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 01:41:22 +01:00
refactor(es modules): migrate colorUtils
This commit is contained in:
parent
364f33975f
commit
b425a9daf6
12 changed files with 63 additions and 49 deletions
|
|
@ -7674,7 +7674,6 @@
|
|||
<script src="libs/delaunator.min.js"></script>
|
||||
|
||||
<script src="utils/commonUtils.js"></script>
|
||||
<script src="utils/colorUtils.js"></script>
|
||||
<script src="utils/nodeUtils.js"></script>
|
||||
<script src="utils/numberUtils.js"></script>
|
||||
<script src="utils/polyfills.js"></script>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import {TIME} from "/src/config/logging";
|
||||
import {findCell} from "/src/utils/graphUtils";
|
||||
import {layerIsOn} from "./ui/layers";
|
||||
import {getColors, getRandomColor, getMixedColor} from "/src/utils/colorUtils";
|
||||
|
||||
window.BurgsAndStates = (function () {
|
||||
const generate = function () {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import {TIME} from "/src/config/logging";
|
||||
import {getColors} from "/src/utils/colorUtils";
|
||||
|
||||
window.Cultures = (function () {
|
||||
let cells;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import {restoreDefaultEvents} from "/src/scripts/events";
|
|||
import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils";
|
||||
import {byId} from "/src/utils/shorthands";
|
||||
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
|
||||
import {getRandomColor, getMixedColor} from "/src/utils/colorUtils";
|
||||
|
||||
const $body = insertEditorHtml();
|
||||
addListeners();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import {TIME} from "/src/config/logging";
|
||||
import {findAll} from "/src/utils/graphUtils";
|
||||
import {unique} from "/src/utils/arrayUtils";
|
||||
import {getRandomColor, getMixedColor} from "/src/utils/colorUtils";
|
||||
|
||||
window.Religions = (function () {
|
||||
// name generation approach and relative chance to be selected
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import {restoreDefaultEvents} from "/src/scripts/events";
|
||||
import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils";
|
||||
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
|
||||
import {getRandomColor} from "/src/utils/colorUtils";
|
||||
|
||||
export function editBiomes() {
|
||||
if (customization) return;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import {restoreDefaultEvents} from "/src/scripts/events";
|
|||
import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils";
|
||||
import {unique} from "/src/utils/arrayUtils";
|
||||
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
|
||||
import {getRandomColor} from "/src/utils/colorUtils";
|
||||
|
||||
export function editProvinces() {
|
||||
if (customization) return;
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ const customPresetPrefix = "fmgStyle_";
|
|||
document.getElementById("stylePreset").innerHTML = options;
|
||||
}
|
||||
|
||||
async function applyStyleOnLoad() {
|
||||
export async function applyStyleOnLoad() {
|
||||
const desiredPreset = localStorage.getItem("presetStyle") || "default";
|
||||
const styleData = await getStylePreset(desiredPreset);
|
||||
const [appliedPreset, style] = styleData;
|
||||
|
|
|
|||
29
src/main.ts
29
src/main.ts
|
|
@ -1,27 +1,28 @@
|
|||
// Azgaar (azgaar.fmg@yandex.com). Minsk, 2017-2022. MIT License
|
||||
// https://github.com/Azgaar/Fantasy-Map-Generator
|
||||
|
||||
import {invokeActiveZooming} from "../modules/activeZooming";
|
||||
import {applyPreset, drawBorders, drawRivers, drawStates} from "../modules/ui/layers";
|
||||
import {applyMapSize, applyStoredOptions, randomizeOptions} from "../modules/ui/options";
|
||||
import {ERROR, INFO, TIME, WARN} from "./config/logging";
|
||||
import {UINT16_MAX} from "./constants";
|
||||
import {INFO, TIME, WARN, ERROR} from "./config/logging";
|
||||
import {clearLegend} from "./modules/legend";
|
||||
import {drawScaleBar, Ruler, Rulers} from "./modules/measurers";
|
||||
import {applyStyleOnLoad} from "../modules/ui/stylePresets";
|
||||
import {restoreDefaultEvents} from "./scripts/events";
|
||||
import {addGlobalListeners} from "./scripts/listeners";
|
||||
import {locked} from "./scripts/options/lock";
|
||||
import {clearMainTip, tip} from "./scripts/tooltips";
|
||||
import {createTypedArray} from "./utils/arrayUtils";
|
||||
import {
|
||||
shouldRegenerateGrid,
|
||||
generateGrid,
|
||||
calculateVoronoi,
|
||||
findCell,
|
||||
generateGrid,
|
||||
getPackPolygon,
|
||||
isLand,
|
||||
findCell
|
||||
shouldRegenerateGrid
|
||||
} from "./utils/graphUtils";
|
||||
import {createTypedArray} from "./utils/arrayUtils";
|
||||
import {applyPreset, drawRivers, drawStates, drawBorders} from "../modules/ui/layers";
|
||||
import {invokeActiveZooming} from "../modules/activeZooming";
|
||||
import {applyStoredOptions, applyMapSize, randomizeOptions} from "../modules/ui/options";
|
||||
import {locked} from "./scripts/options/lock";
|
||||
import {Rulers, Ruler, drawScaleBar} from "./modules/measurers";
|
||||
import {byId} from "./utils/shorthands";
|
||||
import {addGlobalListeners} from "./scripts/listeners";
|
||||
import {restoreDefaultEvents} from "./scripts/events";
|
||||
import {clearMainTip, tip} from "./scripts/tooltips";
|
||||
import {clearLegend} from "./modules/legend";
|
||||
import "./components";
|
||||
|
||||
addGlobalListeners();
|
||||
|
|
|
|||
1
src/types/global.d.ts
vendored
1
src/types/global.d.ts
vendored
|
|
@ -12,6 +12,7 @@ interface Window {
|
|||
};
|
||||
pack: IPack;
|
||||
grig: IGrid;
|
||||
d3: typeof d3;
|
||||
}
|
||||
|
||||
interface Node {
|
||||
|
|
|
|||
40
src/utils/colorUtils.ts
Normal file
40
src/utils/colorUtils.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
const d3 = window.d3;
|
||||
|
||||
const c12 = [
|
||||
"#dababf",
|
||||
"#fb8072",
|
||||
"#80b1d3",
|
||||
"#fdb462",
|
||||
"#b3de69",
|
||||
"#fccde5",
|
||||
"#c6b9c1",
|
||||
"#bc80bd",
|
||||
"#ccebc5",
|
||||
"#ffed6f",
|
||||
"#8dd3c7",
|
||||
"#eb8de7"
|
||||
];
|
||||
|
||||
export function getColors(number: number) {
|
||||
const cRB = d3.scaleSequential(d3.interpolateRainbow);
|
||||
const colors = d3.shuffle(
|
||||
d3
|
||||
.range(number)
|
||||
.map((index: number) => (index < 12 ? c12[index] : d3.color(cRB((index - 12) / (number - 12))).hex()))
|
||||
);
|
||||
return colors;
|
||||
}
|
||||
|
||||
export function getRandomColor() {
|
||||
return d3.color(d3.scaleSequential(d3.interpolateRainbow)(Math.random())).hex();
|
||||
}
|
||||
|
||||
// mix a color with a random color
|
||||
export function getMixedColor(hexColor: string, mixation = 0.2, bright = 0.3) {
|
||||
// if provided color is not hex (e.g. harching), generate random one
|
||||
const color1 = hexColor && hexColor[0] === "#" ? hexColor : getRandomColor();
|
||||
const color2 = getRandomColor();
|
||||
const mixedColor = d3.interpolate(color1, color2)(mixation);
|
||||
|
||||
return d3.color(mixedColor).brighter(bright).hex();
|
||||
}
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
"use strict";
|
||||
// FMG utils related to colors
|
||||
|
||||
// convert RGB color string to HEX without #
|
||||
function toHEX(rgb) {
|
||||
if (rgb.charAt(0) === "#") return rgb;
|
||||
|
||||
rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
|
||||
return rgb && rgb.length === 4
|
||||
? "#" +
|
||||
("0" + parseInt(rgb[1], 10).toString(16)).slice(-2) +
|
||||
("0" + parseInt(rgb[2], 10).toString(16)).slice(-2) +
|
||||
("0" + parseInt(rgb[3], 10).toString(16)).slice(-2)
|
||||
: "";
|
||||
}
|
||||
|
||||
// return array of standard shuffled colors
|
||||
function getColors(number) {
|
||||
const c12 = ["#dababf", "#fb8072", "#80b1d3", "#fdb462", "#b3de69", "#fccde5", "#c6b9c1", "#bc80bd", "#ccebc5", "#ffed6f", "#8dd3c7", "#eb8de7"];
|
||||
const cRB = d3.scaleSequential(d3.interpolateRainbow);
|
||||
const colors = d3.shuffle(d3.range(number).map(i => (i < 12 ? c12[i] : d3.color(cRB((i - 12) / (number - 12))).hex())));
|
||||
return colors;
|
||||
}
|
||||
|
||||
function getRandomColor() {
|
||||
return d3.color(d3.scaleSequential(d3.interpolateRainbow)(Math.random())).hex();
|
||||
}
|
||||
|
||||
// mix a color with a random color
|
||||
function getMixedColor(color, mix = 0.2, bright = 0.3) {
|
||||
const c = color && color[0] === "#" ? color : getRandomColor(); // if provided color is not hex (e.g. harching), generate random one
|
||||
return d3.color(d3.interpolate(c, getRandomColor())(mix)).brighter(bright).hex();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue