-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
24 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,31 @@ | ||
/* | ||
* Polynomial approximation for the Turbo colormap | ||
* Original GLSL implementation: | ||
* https://gist.github.com/mikhailov-work/0d177465a8151eb6ede1768d51d476c7 | ||
* Original LUT: | ||
* https://gist.github.com/mikhailov-work/ee72ba4191942acecc03fe6da94fc73f | ||
* | ||
* Adapted to JavaScript by Kalabint | ||
* | ||
* Authors: | ||
* Colormap Design: Anton Mikhailov (mikhailov@google.com) | ||
* GLSL Approximation: Ruofei Du (ruofei@google.com) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
function interpolateTurbo(t) { | ||
t = Math.max(0, Math.min(1, t)); // Clamp t to [0, 1] | ||
const r = Math.round(255 * interpolateTurbo_srgb_fn_r(t)); | ||
const g = Math.round(255 * interpolateTurbo_srgb_fn_g(t)); | ||
const b = Math.round(255 * interpolateTurbo_srgb_fn_b(t)); | ||
return [r, g, b]; | ||
} | ||
|
||
function interpolateTurbo_srgb_fn_r(t) { | ||
const t2 = t * t; | ||
const t3 = t2 * t; | ||
const t4 = t2 * t2; | ||
const t5 = t3 * t2; | ||
return ( | ||
0.13572138 + | ||
4.61539260 * t - | ||
42.66032258 * t2 + | ||
132.13108234 * t3 - | ||
152.94239396 * t4 + | ||
59.28637943 * t5 | ||
); | ||
} | ||
// Turbo Colormap | ||
const turboPolynomials = { | ||
r: [0.13572138, 4.61539260, -42.66032258, 132.13108234, -152.94239396, 59.28637943], | ||
g: [0.09140261, 2.19418839, 4.84296658, -14.18503333, 4.27729857, 2.82956604], | ||
b: [0.10667330, 12.64194608, -60.58204836, 110.36276771, -89.90310912, 27.34824973] | ||
}; | ||
|
||
function interpolateTurbo_srgb_fn_g(t) { | ||
const t2 = t * t; | ||
const t3 = t2 * t; | ||
const t4 = t2 * t2; | ||
const t5 = t3 * t2; | ||
return ( | ||
0.09140261 + | ||
2.19418839 * t + | ||
4.84296658 * t2 - | ||
14.18503333 * t3 + | ||
4.27729857 * t4 + | ||
2.82956604 * t5 | ||
); | ||
function interpolateChannel(t, coeffs) { | ||
let result = 0; | ||
let tPower = 1; | ||
for (let i = 0; i < coeffs.length; i++) { | ||
result += coeffs[i] * tPower; | ||
tPower *= t; | ||
} | ||
return Math.max(0, Math.min(1, result)); | ||
} | ||
|
||
function interpolateTurbo_srgb_fn_b(t) { | ||
const t2 = t * t; | ||
const t3 = t2 * t; | ||
const t4 = t2 * t2; | ||
const t5 = t3 * t2; | ||
return ( | ||
0.10667330 + | ||
12.64194608 * t - | ||
60.58204836 * t2 + | ||
110.36276771 * t3 - | ||
89.90310912 * t4 + | ||
27.34824973 * t5 | ||
); | ||
function interpolateTurbo(t) { | ||
t = Math.max(0, Math.min(1, t)); | ||
return [ | ||
Math.round(255 * interpolateChannel(t, turboPolynomials.r)), | ||
Math.round(255 * interpolateChannel(t, turboPolynomials.g)), | ||
Math.round(255 * interpolateChannel(t, turboPolynomials.b)) | ||
]; | ||
} | ||
|
||
export const getSpeedColor = (speed, max) => { | ||
let factor = speed / max; | ||
if (factor > 1) factor = 1; | ||
if (factor < 0) factor = 0; | ||
|
||
export function getSpeedColor(speed, max) { | ||
const factor = Math.max(0, Math.min(1, speed / max)); | ||
const [r, g, b] = interpolateTurbo(factor); | ||
return `rgb(${r}, ${g}, ${b})`; | ||
}; | ||
} |