| |
| |
| |
| |
| |
| |
|
|
| const STOPS = [ |
| [0.0, [0.706, 0.016, 0.150]], |
| [0.25, [0.929, 0.567, 0.459]], |
| [0.5, [0.865, 0.865, 0.865]], |
| [0.75, [0.557, 0.647, 0.925]], |
| [1.0, [0.227, 0.298, 0.753]], |
| ]; |
|
|
| function lerp(a, b, t) { |
| return a + (b - a) * t; |
| } |
|
|
| export function colormapCoolwarmR(t) { |
| t = Math.max(0, Math.min(1, t)); |
| for (let i = 0; i < STOPS.length - 1; i++) { |
| const [t0, c0] = STOPS[i]; |
| const [t1, c1] = STOPS[i + 1]; |
| if (t >= t0 && t <= t1) { |
| const f = (t - t0) / (t1 - t0); |
| return [ |
| lerp(c0[0], c1[0], f), |
| lerp(c0[1], c1[1], f), |
| lerp(c0[2], c1[2], f), |
| ]; |
| } |
| } |
| return STOPS[STOPS.length - 1][1]; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function mapScalarsToColors(values, vmin, vmax) { |
| const n = values.length; |
| const colors = new Float32Array(n * 3); |
| const absMax = Math.max(Math.abs(vmin), Math.abs(vmax)) || 1e-6; |
|
|
| for (let i = 0; i < n; i++) { |
| |
| const t = (values[i] / absMax + 1) * 0.5; |
| const [r, g, b] = colormapCoolwarmR(t); |
| colors[i * 3] = r; |
| colors[i * 3 + 1] = g; |
| colors[i * 3 + 2] = b; |
| } |
| return colors; |
| } |
|
|
| |
| |
| |
| |
| export function colormapGradientCSS() { |
| const stops = []; |
| for (let i = 0; i <= 20; i++) { |
| const t = 1 - i / 20; |
| const [r, g, b] = colormapCoolwarmR(t); |
| stops.push(`rgb(${r * 255 | 0},${g * 255 | 0},${b * 255 | 0})`); |
| } |
| return `linear-gradient(to bottom, ${stops.join(', ')})`; |
| } |
|
|