Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed Speed based Color Mapping from Theme Color to TurboColorRamp #1258

Merged
merged 9 commits into from
Oct 1, 2024
42 changes: 25 additions & 17 deletions src/common/util/colors.js
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) => {
Kalabint marked this conversation as resolved.
Show resolved Hide resolved
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;
5 changes: 1 addition & 4 deletions src/map/MapRoutePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useTheme } from '@mui/styles';
import { useId, useEffect } from 'react';
import { useSelector } from 'react-redux';
import { map } from './core/MapView';
import { getSpeedColor } from '../common/util/colors';
import getSpeedColor from '../common/util/colors';

const MapRoutePath = ({ positions }) => {
const id = useId();
Expand Down Expand Up @@ -73,9 +73,6 @@ const MapRoutePath = ({ positions }) => {
},
properties: {
color: reportColor || getSpeedColor(
theme.palette.success.main,
theme.palette.warning.main,
theme.palette.error.main,
positions[i + 1].speed,
maxSpeed,
),
Expand Down
6 changes: 2 additions & 4 deletions src/map/MapRoutePoints.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { useId, useCallback, useEffect } from 'react';
import { useTheme } from '@mui/styles';
import { map } from './core/MapView';
import { getSpeedColor } from '../common/util/colors';
import getSpeedColor from '../common/util/colors';

const MapRoutePoints = ({ positions, onClick }) => {
const id = useId();
const theme = useTheme();

const onMouseEnter = () => map.getCanvas().style.cursor = 'pointer';
const onMouseLeave = () => map.getCanvas().style.cursor = '';
Expand Down Expand Up @@ -72,7 +70,7 @@ const MapRoutePoints = ({ positions, onClick }) => {
index,
id: position.id,
rotation: position.course,
color: getSpeedColor(theme.palette.success.main, theme.palette.warning.main, theme.palette.error.main, position.speed, maxSpeed),
color: getSpeedColor(position.speed, maxSpeed),
},
})),
});
Expand Down