42 lines
1.8 KiB
TypeScript
42 lines
1.8 KiB
TypeScript
export const getSecurityColor = (security: number): string => {
|
|
// Clamp security between -1 and 1
|
|
const clampedSecurity = Math.max(-1, Math.min(1, security));
|
|
|
|
// Define color points for specific security values
|
|
const colorPoints = [
|
|
{ sec: -1.0, color: [75, 0, 130] }, // Dark purple (same as 0.1)
|
|
{ sec: 0.1, color: [75, 0, 130] }, // Dark purple
|
|
{ sec: 0.2, color: [255, 0, 0] }, // Red
|
|
{ sec: 0.3, color: [255, 165, 0] }, // Orange
|
|
{ sec: 0.4, color: [255, 200, 0] }, // Light orange
|
|
{ sec: 0.5, color: [0, 255, 0] }, // Yellow
|
|
{ sec: 0.6, color: [0, 255, 127] }, // Green
|
|
{ sec: 0.7, color: [0, 255, 255] }, // Cyan
|
|
{ sec: 0.8, color: [135, 206, 235] }, // Sky blue (more appropriate light blue)
|
|
{ sec: 0.9, color: [0, 0, 255] }, // Blue
|
|
{ sec: 1.0, color: [0, 0, 139] } // Deep blue
|
|
];
|
|
|
|
// Find the two color points to interpolate between
|
|
let lowerPoint = colorPoints[0];
|
|
let upperPoint = colorPoints[colorPoints.length - 1];
|
|
|
|
for (let i = 0; i < colorPoints.length - 1; i++) {
|
|
if (clampedSecurity >= colorPoints[i].sec && clampedSecurity <= colorPoints[i + 1].sec) {
|
|
lowerPoint = colorPoints[i];
|
|
upperPoint = colorPoints[i + 1];
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Calculate the ratio between the two points
|
|
const ratio = (clampedSecurity - lowerPoint.sec) / (upperPoint.sec - lowerPoint.sec);
|
|
|
|
// Interpolate between the colors
|
|
const red = Math.round(lowerPoint.color[0] + (upperPoint.color[0] - lowerPoint.color[0]) * ratio);
|
|
const green = Math.round(lowerPoint.color[1] + (upperPoint.color[1] - lowerPoint.color[1]) * ratio);
|
|
const blue = Math.round(lowerPoint.color[2] + (upperPoint.color[2] - lowerPoint.color[2]) * ratio);
|
|
|
|
return `rgb(${red}, ${green}, ${blue})`;
|
|
};
|