| 255 | } |
| 256 | |
| 257 | export function contrastRatio(foreground: HexColor, background: HexColor) { |
| 258 | const linear = (c: number) => (c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4) |
| 259 | const luminance = (hex: HexColor) => { |
| 260 | const { r, g, b } = hexToRgb(hex) |
| 261 | return 0.2126 * linear(r) + 0.587 * linear(g) + 0.0722 * linear(b) |
| 262 | } |
| 263 | const fg = luminance(foreground) |
| 264 | const bg = luminance(background) |
| 265 | const lighter = Math.max(fg, bg) |
| 266 | const darker = Math.min(fg, bg) |
| 267 | return (lighter + 0.05) / (darker + 0.05) |
| 268 | } |
| 269 | |
| 270 | export function blend(color: HexColor, background: HexColor, alpha: number): HexColor { |
| 271 | const fg = hexToRgb(color) |