From a421cfafd0ae7a565dc41062862266d5abeed8ac Mon Sep 17 00:00:00 2001 From: Kalabint Date: Mon, 7 Oct 2024 15:11:37 +0200 Subject: [PATCH 1/2] Fix Color normalization --- src/common/util/colors.js | 10 ++++++++-- src/map/MapRoutePoints.js | 6 +++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/common/util/colors.js b/src/common/util/colors.js index 535540e309..1e7e74d52b 100644 --- a/src/common/util/colors.js +++ b/src/common/util/colors.js @@ -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})`; }; diff --git a/src/map/MapRoutePoints.js b/src/map/MapRoutePoints.js index 7ee4dc6363..4200528bc7 100644 --- a/src/map/MapRoutePoints.js +++ b/src/map/MapRoutePoints.js @@ -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) => ({ @@ -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), }, })), }); From 0561dc5d6cafc4ca65b26cef4c1c3a2e54dc3d6b Mon Sep 17 00:00:00 2001 From: Kalabint Date: Mon, 7 Oct 2024 19:11:09 +0200 Subject: [PATCH 2/2] Removed unnecessary sanity checks --- src/common/util/colors.js | 4 ---- src/map/MapRoutePoints.js | 2 -- 2 files changed, 6 deletions(-) diff --git a/src/common/util/colors.js b/src/common/util/colors.js index 1e7e74d52b..87963a4b8b 100644 --- a/src/common/util/colors.js +++ b/src/common/util/colors.js @@ -23,10 +23,6 @@ const interpolateTurbo = (value) => { }; 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); diff --git a/src/map/MapRoutePoints.js b/src/map/MapRoutePoints.js index 4200528bc7..e561c32830 100644 --- a/src/map/MapRoutePoints.js +++ b/src/map/MapRoutePoints.js @@ -57,8 +57,6 @@ 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);