-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathcolormap.js
134 lines (122 loc) · 3.79 KB
/
colormap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* From http://www.redblobgames.com/maps/mapgen2/
* Copyright 2017 Red Blob Games <redblobgames@gmail.com>
* License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
*/
export const discreteColors = {
OCEAN: "#44447a",
COAST: "#33335a",
LAKESHORE: "#225588",
LAKE: "#336699",
RIVER: "#225588",
MARSH: "#2f6666",
ICE: "#99ffff",
BEACH: "#a09077",
SNOW: "#ffffff",
TUNDRA: "#bbbbaa",
BARE: "#888888",
SCORCHED: "#555555",
TAIGA: "#99aa77",
SHRUBLAND: "#889977",
TEMPERATE_DESERT: "#c9d29b",
TEMPERATE_RAIN_FOREST: "#448855",
TEMPERATE_DECIDUOUS_FOREST: "#679459",
GRASSLAND: "#88aa55",
SUBTROPICAL_DESERT: "#d2b98b",
TROPICAL_RAIN_FOREST: "#337755",
TROPICAL_SEASONAL_FOREST: "#559944",
};
function smoothColoring(e, t, m) {
// adapted from <https://www.redblobgames.com/maps/terrain-from-noise/>
if (e < 0.0) {
return `rgb(${(48 + 48*e) | 0}, ${(64 + 64*e) | 0}, ${(127 + 128*e) | 0})`;
}
// Green or brown at low elevation, and make it more white-ish
// as you get colder
let white = (1-t) * (1-t);
m = 1.0 - ((1-m)*(1-m));
var red = 210 - 100*m, grn = 185 - 45*m, blu = 139 - 45*m;
return `rgb(${(255 * white + red * (1-white)) | 0},
${(255 * white + grn * (1-white)) | 0},
${(255 * white + blu * (1-white)) | 0})`;
}
class Coloring {
constructor() {
}
draw_coast_s(map, s) {
return map.ocean_r[map.mesh.r_begin_s(s)] !== map.ocean_r[map.mesh.r_end_s(s)];
}
draw_lakeside_s(map, s) {
let r0 = map.mesh.r_begin_s(s),
r1 = map.mesh.r_end_s(s);
return (map.water_r[r0] !== map.water_r[r1]
&& !map.ocean_r[r0]
&& map.biome_r[r0] !== 'ICE'
&& map.biome_r[r1] !== 'ICE');
}
draw_river_s(map, s) {
let r0 = map.mesh.r_begin_s(s),
r1 = map.mesh.r_end_s(s);
return ((map.flow_s[s] > 0 || map.flow_s[map.mesh.s_opposite_s(s)] > 0)
&& !map.water_r[r0] && !map.water_r[r1]);
}
biome(map, r) {
return "red";
}
side(map, s) {
let r0 = map.mesh.r_begin_s(s),
r1 = map.mesh.r_end_s(s);
if (this.draw_coast_s(map, s)) {
// Coastlines are thick
return {
noisy: true,
lineWidth: 3,
strokeStyle: discreteColors.COAST,
};
} else if (this.draw_lakeside_s(map, s)) {
// Lake boundary
return {
noisy: true,
lineWidth: 1.5,
strokeStyle: discreteColors.LAKESHORE,
};
} else if (this.draw_river_s(map, s)) {
// River
return {
noisy: true,
lineWidth: 2.0 * Math.sqrt(map.flow_s[s]),
strokeStyle: discreteColors.RIVER,
};
} else if (map.biome_r[r0] === map.biome_r[r1]) {
return {
noisy: false,
lineWidth: 1.0,
strokeStyle: this.biome(map, r0),
};
} else {
return {
noisy: true,
lineWidth: 1.0,
strokeStyle: this.biome(map, r0),
};
}
}
}
export class Discrete extends Coloring {
biome(map, r) {
return discreteColors[map.biome_r[r]];
}
}
export class Smooth extends Coloring {
biome(map, r) {
if (map.water_r[r] && !map.ocean_r[r]) {
return discreteColors[map.biome_r[r]];
} else {
return smoothColoring(
map.elevation_r[r],
Math.min(1, Math.max(0, map.temperature_r[r])),
Math.min(1, Math.max(0, map.moisture_r[r]))
);
}
}
}