mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 17:51:24 +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
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();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue