Skip to content

Commit 33f093e

Browse files
Use hsl to calculate fingerprint colors (#407)
To make the colours more perceptually different
1 parent 10171b1 commit 33f093e

File tree

2 files changed

+10
-26
lines changed

2 files changed

+10
-26
lines changed

src/components/RecordingFingerprint.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { BoxProps, Grid, GridItem, Text, VStack } from "@chakra-ui/react";
2+
import { FormattedMessage } from "react-intl";
23
import { applyFilters } from "../ml";
34
import { XYZData } from "../model";
4-
import { calculateColor } from "../utils/gradient-calculator";
5+
import { calculateGradientColor } from "../utils/gradient-calculator";
56
import ClickableTooltip from "./ClickableTooltip";
6-
import { FormattedMessage } from "react-intl";
77

88
interface RecordingFingerprintProps extends BoxProps {
99
data: XYZData;
@@ -49,11 +49,7 @@ const RecordingFingerprint = ({
4949
>
5050
<GridItem
5151
w="100%"
52-
backgroundColor={calculateColor(
53-
dataFeatures[k],
54-
{ r: 225, g: 255, b: 255 },
55-
{ r: 25, g: 125, b: 188 }
56-
)}
52+
backgroundColor={calculateGradientColor("#007DBC", dataFeatures[k])}
5753
/>
5854
</ClickableTooltip>
5955
))}

src/utils/gradient-calculator.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
interface RGB {
2-
r: number;
3-
g: number;
4-
b: number;
5-
}
6-
7-
export const calculateColor = (
8-
value: number,
9-
startColor: RGB,
10-
endColor: RGB
11-
) => {
12-
const diff = {
13-
r: endColor.r - startColor.r,
14-
g: endColor.g - startColor.g,
15-
b: endColor.b - startColor.b,
16-
};
17-
return `rgba(${startColor.r + diff.r * value}, ${
18-
startColor.g + diff.g * value
19-
}, ${startColor.b + diff.b * value}, 1)`;
1+
export const calculateGradientColor = (hexColor: string, value: number) => {
2+
const minLightness = 10;
3+
const maxLightness = 90;
4+
const diffLightness = maxLightness - minLightness;
5+
return `hsl(from ${hexColor} h s ${
6+
minLightness + (1 - value) * diffLightness
7+
}%)`;
208
};

0 commit comments

Comments
 (0)