-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1258 from Kalabint/route_turbo_color_ramp
Changed Speed based Color Mapping from Theme Color to TurboColorRamp
- Loading branch information
Showing
3 changed files
with
28 additions
and
25 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,23 +1,31 @@ | ||
import { decomposeColor } from '@mui/material'; | ||
|
||
export const interpolateColor = (color1, color2, factor) => { | ||
if (factor > 1) factor = 1; | ||
if (factor < 0) factor = 0; | ||
// 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], | ||
}; | ||
|
||
const c1 = decomposeColor(color1).values; | ||
const c2 = decomposeColor(color2).values; | ||
const interpolateChannel = (normalizedValue, coeffs) => { | ||
let result = 0; | ||
for (let i = 0; i < coeffs.length; i += 1) { | ||
result += coeffs[i] * (normalizedValue ** i); | ||
} | ||
return Math.max(0, Math.min(1, result)); | ||
}; | ||
|
||
const r = Math.round(c1[0] + factor * (c2[0] - c1[0])); | ||
const g = Math.round(c1[1] + factor * (c2[1] - c1[1])); | ||
const b = Math.round(c1[2] + factor * (c2[2] - c1[2])); | ||
const interpolateTurbo = (value) => { | ||
const normalizedValue = Math.max(0, Math.min(1, value)); | ||
return [ | ||
Math.round(255 * interpolateChannel(normalizedValue, turboPolynomials.r)), | ||
Math.round(255 * interpolateChannel(normalizedValue, turboPolynomials.g)), | ||
Math.round(255 * interpolateChannel(normalizedValue, turboPolynomials.b)), | ||
]; | ||
}; | ||
|
||
const getSpeedColor = (speed, maxSpeed) => { | ||
const normalizedSpeed = Math.max(0, Math.min(1, speed / maxSpeed)); | ||
const [r, g, b] = interpolateTurbo(normalizedSpeed); | ||
return `rgb(${r}, ${g}, ${b})`; | ||
}; | ||
|
||
export const getSpeedColor = (color1, color2, color3, speed, max) => { | ||
const factor = speed / max; | ||
if (factor <= 0.5) { | ||
return interpolateColor(color1, color2, factor * 2); | ||
} | ||
return interpolateColor(color2, color3, (factor - 0.5) * 2); | ||
}; | ||
export default getSpeedColor; |
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
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