/** * Adjusts the opacity of a hex color. */ export function withOpacity(hex: string, opacity: number): string { const clampedOpacity = Math.max(0, Math.min(1, opacity)); const r = parseInt(hex.slice(1, 3), 16); const g = parseInt(hex.slice(3, 5), 16); const b = parseInt(hex.slice(5, 7), 16); return `rgba(${r}, ${g}, ${b}, ${clampedOpacity})`; } /** * Lightens a hex color by a given percentage (0 to 1). */ export function lighten(hex: string, amount: number): string { const r = parseInt(hex.slice(1, 3), 16); const g = parseInt(hex.slice(3, 5), 16); const b = parseInt(hex.slice(5, 7), 16); const newR = Math.min(255, Math.round(r + (255 - r) * amount)); const newG = Math.min(255, Math.round(g + (255 - g) * amount)); const newB = Math.min(255, Math.round(b + (255 - b) * amount)); return `#${newR.toString(16).padStart(2, '0')}${newG.toString(16).padStart(2, '0')}${newB.toString(16).padStart(2, '0')}`; } /** * Darkens a hex color by a given percentage (0 to 1). */ export function darken(hex: string, amount: number): string { const r = parseInt(hex.slice(1, 3), 16); const g = parseInt(hex.slice(3, 5), 16); const b = parseInt(hex.slice(5, 7), 16); const newR = Math.max(0, Math.round(r * (1 - amount))); const newG = Math.max(0, Math.round(g * (1 - amount))); const newB = Math.max(0, Math.round(b * (1 - amount))); return `#${newR.toString(16).padStart(2, '0')}${newG.toString(16).padStart(2, '0')}${newB.toString(16).padStart(2, '0')}`; } /** * Determines if a color is considered "light" or "dark" based on luminance. */ export function isLightColor(hex: string): boolean { const r = parseInt(hex.slice(1, 3), 16); const g = parseInt(hex.slice(3, 5), 16); const b = parseInt(hex.slice(5, 7), 16); const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255; return luminance > 0.5; }