mirror of
https://github.com/hanzoai/dataroom.git
synced 2026-10-30 18:41:07 +00:00
29 lines
731 B
TypeScript
29 lines
731 B
TypeScript
function hexToRgb(hex: string) {
|
|
let r = 0,
|
|
g = 0,
|
|
b = 0;
|
|
// 3 digits
|
|
if (hex.length === 4) {
|
|
r = parseInt(hex[1] + hex[1], 16);
|
|
g = parseInt(hex[2] + hex[2], 16);
|
|
b = parseInt(hex[3] + hex[3], 16);
|
|
}
|
|
// 6 digits
|
|
else if (hex.length === 7) {
|
|
r = parseInt(hex[1] + hex[2], 16);
|
|
g = parseInt(hex[3] + hex[4], 16);
|
|
b = parseInt(hex[5] + hex[6], 16);
|
|
}
|
|
return [r, g, b];
|
|
}
|
|
|
|
function luminance(r: number, g: number, b: number) {
|
|
return (0.299 * r + 0.587 * g + 0.114 * b) / 255;
|
|
}
|
|
|
|
export function determineTextColor(hexColor: string | null | undefined) {
|
|
if (!hexColor) return "white";
|
|
const [r, g, b] = hexToRgb(hexColor);
|
|
return luminance(r, g, b) > 0.5 ? "black" : "white";
|
|
}
|