Add colors to the website and calculate text color for background colors
This commit is contained in:
23
astro/src/lib/colors.ts
Normal file
23
astro/src/lib/colors.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
export function getTextColor(bgColor: string) {
|
||||
// Remove # if present
|
||||
const hex = bgColor.replace('#', '');
|
||||
|
||||
// Convert hex to RGB
|
||||
const r = parseInt(hex.substring(0, 2), 16) / 255;
|
||||
const g = parseInt(hex.substring(2, 4), 16) / 255;
|
||||
const b = parseInt(hex.substring(4, 6), 16) / 255;
|
||||
|
||||
// Convert to linear RGB
|
||||
const toLinear = (c: any) =>
|
||||
c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
|
||||
|
||||
const R = toLinear(r);
|
||||
const G = toLinear(g);
|
||||
const B = toLinear(b);
|
||||
|
||||
// Calculate luminance
|
||||
const luminance = 0.2126 * R + 0.7152 * G + 0.0722 * B;
|
||||
|
||||
// Return white for dark backgrounds, black for light backgrounds
|
||||
return luminance > 0.179 ? '#000000' : '#fcfcfc';
|
||||
}
|
||||
Reference in New Issue
Block a user