Skip to content

Commit def3142

Browse files
committed
color scale fix and picker value consistency
1 parent c8b44c1 commit def3142

File tree

6 files changed

+39
-27
lines changed

6 files changed

+39
-27
lines changed

src/lib/utils/color-scales.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { interpolateHsl, color } from 'd3';
2-
import type { ColorScale, Variable } from '../../types';
2+
import type { ColorScale, Interpolator, Variable } from '../../types';
3+
import { interpolate2DHermite, noInterpolation, quinticHermite2D } from './math';
34

45
function interpolateColorScaleHSL(colors: Array<string>, steps: number) {
56
const segments = colors.length - 1;
@@ -51,7 +52,7 @@ const convectiveCloudScale: ColorScale = {
5152
colors: [
5253
...interpolateColorScaleHSL(['#c0392b', '#d35400', '#f1c40f', '#16a085', '#2980b9'], 6000)
5354
],
54-
interpolationMethod: 'quintic2d'
55+
interpolationMethod: 'none'
5556
};
5657

5758
export const colorScales: ColorScales = {
@@ -170,3 +171,16 @@ export function getColorScale(variable: Variable) {
170171
colorScales['temperature']
171172
);
172173
}
174+
175+
export function getInterpolator(colorScale: ColorScale): Interpolator {
176+
if (colorScale.interpolationMethod === 'hermite2d') {
177+
return interpolate2DHermite;
178+
} else if (colorScale.interpolationMethod === 'quintic2d') {
179+
return quinticHermite2D;
180+
} else if (colorScale.interpolationMethod === 'none') {
181+
return noInterpolation;
182+
} else {
183+
// default is hermite2d
184+
return interpolate2DHermite;
185+
}
186+
}

src/lib/utils/math.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ export const interpolateLinear = (
3838
);
3939
};
4040

41+
export const noInterpolation = (
42+
values: TypedArray,
43+
nx: number,
44+
index: number,
45+
xFraction: number,
46+
yFraction: number
47+
): number => {
48+
return values[index];
49+
};
50+
4151
export const hermite = (t: number, p0: number, p1: number, m0: number, m1: number) => {
4252
const t2 = t * t;
4353
const t3 = t2 * t;

src/om-protocol.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import { variables } from '$lib/utils/variables';
1616

1717
import TileWorker from './worker?worker';
1818

19-
import type { TileJSON, TileIndex, Domain, Variable, Bounds, Range } from './types';
19+
import type { TileJSON, TileIndex, Domain, Variable, Bounds, Range, ColorScale } from './types';
2020
import { DynamicProjection, ProjectionGrid, type Projection } from '$lib/utils/projection';
2121
import { OMapsFileReader } from './omaps-reader';
22-
import { getColorScale } from '$lib/utils/color-scales';
22+
import { getInterpolator } from '$lib/utils/color-scales';
2323

2424
let dark = false;
2525
let partial = false;
@@ -53,7 +53,8 @@ const TILE_SIZE = Number(import.meta.env.VITE_TILE_SIZE) * 2;
5353

5454
export const getValueFromLatLong = (
5555
lat: number,
56-
lon: number
56+
lon: number,
57+
colorScale: ColorScale
5758
): { index: number; value: number; direction?: number } => {
5859
if (data) {
5960
const values = data.values;
@@ -72,14 +73,9 @@ export const getValueFromLatLong = (
7273
};
7374

7475
if (values && index) {
76+
const interpolator = getInterpolator(colorScale);
7577
//const px = interpolateLinear(data, index, xFraction, yFraction);
76-
const px = interpolate2DHermite(
77-
values as TypedArray,
78-
domain.grid.nx,
79-
index,
80-
xFraction,
81-
yFraction
82-
);
78+
const px = interpolator(values as Float32Array, domain.grid.nx, index, xFraction, yFraction);
8379

8480
return { index: index, value: px };
8581
} else {
@@ -92,7 +88,6 @@ export const getValueFromLatLong = (
9288

9389
const getTile = async ({ z, x, y }: TileIndex, omUrl: string): Promise<ImageBitmap> => {
9490
const key = `${omUrl}/${TILE_SIZE}/${z}/${x}/${y}`;
95-
const colorScale = getColorScale(variable);
9691

9792
const worker = new TileWorker();
9893

@@ -105,7 +100,6 @@ const getTile = async ({ z, x, y }: TileIndex, omUrl: string): Promise<ImageBitm
105100
data,
106101
domain,
107102
variable,
108-
colorScale,
109103
ranges,
110104
dark: dark,
111105
mapBounds: mapBounds

src/routes/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@
410410
} else {
411411
popup.addTo(map);
412412
}
413-
let { index, value } = getValueFromLatLong(coordinates.lat, coordinates.lng);
413+
let { index, value } = getValueFromLatLong(coordinates.lat, coordinates.lng, colorScale);
414414
if (index) {
415415
if ((hideZero.includes(variable.value) && value <= 0.25) || !value) {
416416
popup.remove();

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export type ColorScale = {
5454
interpolationMethod: InterpolationMethod;
5555
};
5656

57-
export type InterpolationMethod = 'hermite2d' | 'quintic2d';
57+
export type InterpolationMethod = 'hermite2d' | 'quintic2d' | 'none';
5858

5959
export type Interpolator = (
6060
values: Float32Array<ArrayBufferLike>,

src/worker.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import {
99
interpolateLinear,
1010
interpolate2DHermite,
1111
quinticHermite2D,
12-
degreesToRadians
12+
degreesToRadians,
13+
noInterpolation
1314
} from './lib/utils/math';
1415

1516
import type { IconListPixels } from './lib/utils/icons';
1617
import type { ColorScale, Domain, IndexAndFractions, Interpolator } from './types';
18+
import { getColorScale, getInterpolator } from '$lib/utils/color-scales';
1719

1820
const TILE_SIZE = Number(import.meta.env.VITE_TILE_SIZE) * 2;
1921
const OPACITY = Number(import.meta.env.VITE_TILE_OPACITY);
@@ -143,7 +145,7 @@ self.onmessage = async (message) => {
143145

144146
const domain = message.data.domain;
145147
const variable = message.data.variable;
146-
const colorScale: ColorScale = message.data.colorScale;
148+
const colorScale = getColorScale(message.data.variable);
147149

148150
const pixels = TILE_SIZE * TILE_SIZE;
149151
const rgba = new Uint8ClampedArray(pixels * 4);
@@ -159,15 +161,7 @@ self.onmessage = async (message) => {
159161
projectionGrid = new ProjectionGrid(projection, domain.grid, ranges);
160162
}
161163

162-
let interpolator: Interpolator;
163-
if (colorScale.interpolationMethod === 'hermite2d') {
164-
interpolator = interpolate2DHermite;
165-
} else if (colorScale.interpolationMethod === 'quintic2d') {
166-
interpolator = quinticHermite2D;
167-
} else {
168-
// default is hermite2d
169-
interpolator = interpolate2DHermite;
170-
}
164+
const interpolator = getInterpolator(colorScale);
171165

172166
for (let i = 0; i < TILE_SIZE; i++) {
173167
const lat = tile2lat(y + i / TILE_SIZE, z);

0 commit comments

Comments
 (0)