Skip to content

Commit 68c2ef2

Browse files
authored
Merge pull request #51 from open-meteo/fallback-colorscale-fix
2 parents a2c3508 + f8cb195 commit 68c2ef2

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/lib/utils/color-scales.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const colorScales = {
3131
cape: {
3232
min: 0,
3333
max: 4000,
34+
scalefactor: 1,
3435
colors: [
3536
...interpolateColorScaleHSL(
3637
['#009392', '#39b185', '#9ccb86', '#e9e29c', '#eeb479', '#e88471', '#cf597e'],
@@ -41,6 +42,7 @@ export const colorScales = {
4142
cloud: {
4243
min: 0,
4344
max: 100,
45+
scalefactor: 1,
4446
colors: [
4547
...interpolateColorScaleHSL(['#FFF', '#c3c2c2'], 100) // 0 to 100%
4648
]
@@ -55,8 +57,9 @@ export const colorScales = {
5557
]
5658
},
5759
pressure: {
58-
min: 0,
59-
max: 50,
60+
min: 950,
61+
max: 1050,
62+
scalefactor: 2,
6063
colors: [
6164
...interpolateColorScaleHSL(['#4444FF', '#FFFFFF'], 25), // 950 to 1000hPa
6265
...interpolateColorScaleHSL(['#FFFFFF', '#FF4444'], 25) // 1000hPa to 1050hPa
@@ -65,6 +68,7 @@ export const colorScales = {
6568
relative: {
6669
min: 0,
6770
max: 100,
71+
scalefactor: 1,
6872
colors: [
6973
...interpolateColorScaleHSL(
7074
['#009392', '#39b185', '#9ccb86', '#e9e29c', '#eeb479', '#e88471', '#cf597e'].reverse(),
@@ -75,6 +79,7 @@ export const colorScales = {
7579
shortwave: {
7680
min: 0,
7781
max: 1000,
82+
scalefactor: 1,
7883
colors: [
7984
...interpolateColorScaleHSL(
8085
['#009392', '#39b185', '#9ccb86', '#e9e29c', '#eeb479', '#e88471', '#cf597e'],
@@ -85,6 +90,7 @@ export const colorScales = {
8590
temperature: {
8691
min: -40,
8792
max: 60,
93+
scalefactor: 1,
8894
colors: [
8995
...interpolateColorScaleHSL(['purple', 'blue'], 40), // -40° to 0°
9096
...interpolateColorScaleHSL(['blue', 'green'], 16), // 0° to 16°
@@ -96,6 +102,7 @@ export const colorScales = {
96102
thunderstorm: {
97103
min: 0,
98104
max: 100,
105+
scalefactor: 1,
99106
colors: [
100107
...interpolateColorScaleHSL(['blue', 'green'], 33), //
101108
...interpolateColorScaleHSL(['green', 'orange'], 33), //
@@ -105,6 +112,7 @@ export const colorScales = {
105112
uv: {
106113
min: 0,
107114
max: 12,
115+
scalefactor: 1,
108116
colors: [
109117
...interpolateColorScaleHSL(
110118
['#009392', '#39b185', '#9ccb86', '#e9e29c', '#eeb479', '#e88471', '#cf597e'],
@@ -115,6 +123,7 @@ export const colorScales = {
115123
wind: {
116124
min: 0,
117125
max: 40,
126+
scalefactor: 1,
118127
colors: [
119128
...interpolateColorScaleHSL(['blue', 'green'], 10), // 0 to 10kn
120129
...interpolateColorScaleHSL(['green', 'orange'], 10), // 10 to 20kn

src/routes/+page.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@
513513
return value;
514514
}
515515
}
516+
return colorScales['temperature'];
516517
});
517518
518519
let colors = $derived(colorScale.colors.reverse());

src/worker.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,12 @@ const OPACITY = Number(import.meta.env.VITE_TILE_OPACITY);
8484
// };
8585

8686
const getColor = (v: string, px: number): number[] => {
87-
if (v.startsWith('temperature')) {
88-
// increase minimum temperature by 40, since scale starts at -40
89-
return colors[Math.max(0, Math.floor(px + 40))];
90-
} else if (v.startsWith('pressure')) {
91-
// increase minimum index by 950
92-
return colors[Math.min(colors.length - 1, Math.max(0, Math.floor((px - 950) / 2)))];
93-
} else {
94-
return colors[Math.min(colors.length - 1, Math.max(0, Math.floor(px)))];
95-
}
87+
return colorsObj.colors[
88+
Math.min(
89+
colorsObj.colors.length - 1,
90+
Math.max(0, Math.floor((px - colorsObj.min) / colorsObj.scalefactor))
91+
)
92+
];
9693
};
9794

9895
const getOpacity = (v: string, px: number, dark: boolean): number => {
@@ -137,7 +134,7 @@ const getIndexAndFractions = (
137134
);
138135
};
139136

140-
let colors;
137+
let colorsObj;
141138
self.onmessage = async (message) => {
142139
if (message.data.type == 'GT') {
143140
const key = message.data.key;
@@ -154,8 +151,7 @@ self.onmessage = async (message) => {
154151
const rgba = new Uint8ClampedArray(pixels * 4);
155152
const dark = message.data.dark;
156153

157-
const colorsObject = colorScales[variable.value.split('_')[0]] ?? colors['temperature'];
158-
colors = colorsObject.colors;
154+
colorsObj = colorScales[variable.value.split('_')[0]] ?? colorScales['temperature'];
159155

160156
let projectionGrid = null;
161157
if (domain.grid.projection) {

0 commit comments

Comments
 (0)