|
1 | 1 | // Vue Data UI does not support CSS vars nor OKLCH for now |
| 2 | + |
| 3 | +/** |
| 4 | + * Converts a hex color to RGB components |
| 5 | + */ |
| 6 | +function hexToRgb(hex: string): [number, number, number] | null { |
| 7 | + const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex) |
| 8 | + return result |
| 9 | + ? [parseInt(result[1]!, 16), parseInt(result[2]!, 16), parseInt(result[3]!, 16)] |
| 10 | + : null |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Converts RGB components to hex color |
| 15 | + */ |
| 16 | +function rgbToHex(r: number, g: number, b: number): string { |
| 17 | + const toHex = (value: number): string => |
| 18 | + Math.round(Math.min(Math.max(0, value), 255)) |
| 19 | + .toString(16) |
| 20 | + .padStart(2, '0') |
| 21 | + return `#${toHex(r)}${toHex(g)}${toHex(b)}` |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Lightens a hex color by mixing it with white. |
| 26 | + * Used to create light tints of accent colors for better visibility in light mode. |
| 27 | + * @param hex - The hex color to lighten (e.g., "#ff0000") |
| 28 | + * @param factor - Lighten factor from 0 to 1 (0.5 = 50% lighter, mixed with white) |
| 29 | + */ |
| 30 | +export function lightenHex(hex: string, factor: number = 0.5): string { |
| 31 | + const rgb = hexToRgb(hex) |
| 32 | + if (!rgb) return hex |
| 33 | + |
| 34 | + // Lighten by mixing with white (255, 255, 255) |
| 35 | + const lightened = rgb.map(c => Math.round(c + (255 - c) * factor)) as [number, number, number] |
| 36 | + return rgbToHex(...lightened) |
| 37 | +} |
| 38 | + |
2 | 39 | export function oklchToHex(color: string | undefined | null): string | undefined | null { |
3 | 40 | if (color == null) return color |
4 | 41 |
|
|
0 commit comments