MCPcopy Create free account
hub / github.com/ZToolsCenter/ZTools / adjustColorForTheme

Function adjustColorForTheme

internal-plugins/setting/src/utils/themeUtils.ts:33–43  ·  view source on GitHub ↗

* 按当前明暗环境对颜色进行轻度矫正,避免极亮/极暗自定义色不可读。

(color: string)

Source from the content-addressed store, hash-verified

31 * 按当前明暗环境对颜色进行轻度矫正,避免极亮/极暗自定义色不可读。
32 */
33function 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 变量。

Callers 1

applyCustomColorFunction · 0.70

Calls 2

hexToRgbFunction · 0.70
adjustBrightnessFunction · 0.70

Tested by

no test coverage detected