Skip to content

Commit

Permalink
Fix Color normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalabint committed Oct 7, 2024
1 parent 10b8a91 commit a421cfa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/common/util/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ const interpolateTurbo = (value) => {
];
};

const getSpeedColor = (speed, maxSpeed) => {
const normalizedSpeed = Math.max(0, Math.min(1, speed / maxSpeed));
const getSpeedColor = (speed, minSpeed, maxSpeed) => {
if (maxSpeed <= minSpeed) {
throw new Error('maxSpeed must be greater than minSpeed');
}

const normalizedSpeed = (speed - minSpeed) / (maxSpeed - minSpeed);

const [r, g, b] = interpolateTurbo(normalizedSpeed);

return `rgb(${r}, ${g}, ${b})`;
};

Expand Down
6 changes: 5 additions & 1 deletion src/map/MapRoutePoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ const MapRoutePoints = ({ positions, onClick }) => {
}, [onMarkerClick]);

useEffect(() => {
if (positions.length === 0) return;

const maxSpeed = positions.map((p) => p.speed).reduce((a, b) => Math.max(a, b), -Infinity);
const minSpeed = positions.map((p) => p.speed).reduce((a, b) => Math.min(a, b), Infinity);

map.getSource(id)?.setData({
type: 'FeatureCollection',
features: positions.map((position, index) => ({
Expand All @@ -70,7 +74,7 @@ const MapRoutePoints = ({ positions, onClick }) => {
index,
id: position.id,
rotation: position.course,
color: getSpeedColor(position.speed, maxSpeed),
color: getSpeedColor(position.speed, minSpeed, maxSpeed),
},
})),
});
Expand Down

0 comments on commit a421cfa

Please sign in to comment.