refactor(es modules): migrate colorUtils

This commit is contained in:
Azgaar 2022-06-26 01:49:41 +03:00
parent 364f33975f
commit b425a9daf6
12 changed files with 63 additions and 49 deletions

View file

@ -7674,7 +7674,6 @@
<script src="libs/delaunator.min.js"></script> <script src="libs/delaunator.min.js"></script>
<script src="utils/commonUtils.js"></script> <script src="utils/commonUtils.js"></script>
<script src="utils/colorUtils.js"></script>
<script src="utils/nodeUtils.js"></script> <script src="utils/nodeUtils.js"></script>
<script src="utils/numberUtils.js"></script> <script src="utils/numberUtils.js"></script>
<script src="utils/polyfills.js"></script> <script src="utils/polyfills.js"></script>

View file

@ -1,6 +1,7 @@
import {TIME} from "/src/config/logging"; import {TIME} from "/src/config/logging";
import {findCell} from "/src/utils/graphUtils"; import {findCell} from "/src/utils/graphUtils";
import {layerIsOn} from "./ui/layers"; import {layerIsOn} from "./ui/layers";
import {getColors, getRandomColor, getMixedColor} from "/src/utils/colorUtils";
window.BurgsAndStates = (function () { window.BurgsAndStates = (function () {
const generate = function () { const generate = function () {

View file

@ -1,4 +1,5 @@
import {TIME} from "/src/config/logging"; import {TIME} from "/src/config/logging";
import {getColors} from "/src/utils/colorUtils";
window.Cultures = (function () { window.Cultures = (function () {
let cells; let cells;

View file

@ -2,6 +2,7 @@ import {restoreDefaultEvents} from "/src/scripts/events";
import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils"; import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils";
import {byId} from "/src/utils/shorthands"; import {byId} from "/src/utils/shorthands";
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips"; import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
import {getRandomColor, getMixedColor} from "/src/utils/colorUtils";
const $body = insertEditorHtml(); const $body = insertEditorHtml();
addListeners(); addListeners();

View file

@ -1,6 +1,7 @@
import {TIME} from "/src/config/logging"; import {TIME} from "/src/config/logging";
import {findAll} from "/src/utils/graphUtils"; import {findAll} from "/src/utils/graphUtils";
import {unique} from "/src/utils/arrayUtils"; import {unique} from "/src/utils/arrayUtils";
import {getRandomColor, getMixedColor} from "/src/utils/colorUtils";
window.Religions = (function () { window.Religions = (function () {
// name generation approach and relative chance to be selected // name generation approach and relative chance to be selected

View file

@ -1,6 +1,7 @@
import {restoreDefaultEvents} from "/src/scripts/events"; import {restoreDefaultEvents} from "/src/scripts/events";
import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils"; import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils";
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips"; import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
import {getRandomColor} from "/src/utils/colorUtils";
export function editBiomes() { export function editBiomes() {
if (customization) return; if (customization) return;

View file

@ -2,6 +2,7 @@ import {restoreDefaultEvents} from "/src/scripts/events";
import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils"; import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils";
import {unique} from "/src/utils/arrayUtils"; import {unique} from "/src/utils/arrayUtils";
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips"; import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
import {getRandomColor} from "/src/utils/colorUtils";
export function editProvinces() { export function editProvinces() {
if (customization) return; if (customization) return;

View file

@ -24,7 +24,7 @@ const customPresetPrefix = "fmgStyle_";
document.getElementById("stylePreset").innerHTML = options; document.getElementById("stylePreset").innerHTML = options;
} }
async function applyStyleOnLoad() { export async function applyStyleOnLoad() {
const desiredPreset = localStorage.getItem("presetStyle") || "default"; const desiredPreset = localStorage.getItem("presetStyle") || "default";
const styleData = await getStylePreset(desiredPreset); const styleData = await getStylePreset(desiredPreset);
const [appliedPreset, style] = styleData; const [appliedPreset, style] = styleData;

View file

@ -1,27 +1,28 @@
// Azgaar (azgaar.fmg@yandex.com). Minsk, 2017-2022. MIT License // Azgaar (azgaar.fmg@yandex.com). Minsk, 2017-2022. MIT License
// https://github.com/Azgaar/Fantasy-Map-Generator // 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 {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 { import {
shouldRegenerateGrid,
generateGrid,
calculateVoronoi, calculateVoronoi,
findCell,
generateGrid,
getPackPolygon, getPackPolygon,
isLand, isLand,
findCell shouldRegenerateGrid
} from "./utils/graphUtils"; } 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 {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"; import "./components";
addGlobalListeners(); addGlobalListeners();

View file

@ -12,6 +12,7 @@ interface Window {
}; };
pack: IPack; pack: IPack;
grig: IGrid; grig: IGrid;
d3: typeof d3;
} }
interface Node { interface Node {

40
src/utils/colorUtils.ts Normal file
View 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();
}

View file

@ -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();
}