* 按当前明暗环境对颜色进行轻度矫正,避免极亮/极暗自定义色不可读。
(color: string)
| 31 | * 按当前明暗环境对颜色进行轻度矫正,避免极亮/极暗自定义色不可读。 |
| 32 | */ |
| 33 | function adjustColorForTheme(color: string): string { |
| 34 | const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches |
| 35 | const rgb = hexToRgb(color) |
| 36 | if (!rgb) return color |
| 37 | |
| 38 | const luminance = (0.299 * rgb.r + 0.587 * rgb.g + 0.114 * rgb.b) / 255 |
| 39 | if (!isDarkMode && luminance > 0.9) return adjustBrightness(color, 0.4) |
| 40 | if (isDarkMode && luminance < 0.15) return adjustBrightness(color, 0.6) |
| 41 | |
| 42 | return color |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * 应用自定义主题色到 CSS 变量。 |
no test coverage detected