@zl-ui-kit/theme 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,8 +8,15 @@ export function parseColor(str: string): [number, number, number] | null {
8
8
  if (s.startsWith('#')) {
9
9
  let hex = s.slice(1)
10
10
  if (hex.length === 3)
11
- hex = hex.split('').map(c => c + c).join('')
12
- return [Number.parseInt(hex.slice(0, 2), 16), Number.parseInt(hex.slice(2, 4), 16), Number.parseInt(hex.slice(4, 6), 16)]
11
+ hex = hex
12
+ .split('')
13
+ .map((c) => c + c)
14
+ .join('')
15
+ return [
16
+ Number.parseInt(hex.slice(0, 2), 16),
17
+ Number.parseInt(hex.slice(2, 4), 16),
18
+ Number.parseInt(hex.slice(4, 6), 16),
19
+ ]
13
20
  }
14
21
  const m = s.match(/rgba?\(([^)]+)\)/)
15
22
  if (m) {
@@ -31,8 +38,7 @@ export function toHex([r, g, b]: [number, number, number]): string {
31
38
  export function mix(color1: string, color2: string, weight: number): string {
32
39
  const c1 = parseColor(color1)
33
40
  const c2 = parseColor(color2)
34
- if (!c1 || !c2)
35
- return color1
41
+ if (!c1 || !c2) return color1
36
42
  const w = weight / 100
37
43
  return toHex([c1[0] * (1 - w) + c2[0] * w, c1[1] * (1 - w) + c2[1] * w, c1[2] * (1 - w) + c2[2] * w])
38
44
  }
@@ -50,12 +56,21 @@ export interface ElColorShades {
50
56
  'dark-2': string
51
57
  }
52
58
 
53
- /** EP 主色色阶:light-n = mix(白, 主色, n*10%);dark-2 = mix(黑, 主色, 20%) */
54
- export function generateElShades(hex: string): ElColorShades {
59
+ /**
60
+ * EP 主色色阶:**light-n 中 n 越大越"靠背景"** ——
61
+ * 浅色主题往白走(light-9 ≈ 混 90% 白),深色主题往黑走(light-9 ≈ 混 90% 黑);
62
+ * dark-2 则相反:浅色下更深(混黑),深色下更浅(混白)。
63
+ *
64
+ * ⚠️ mix(c1, c2, w) 的 w 是"混入 c2 的比例",与 light-n 的 n 方向相反,
65
+ * 故必须用 (10 - i) * 10。写成 i * 10 会颠倒色阶,导致 el-tag / el-message /
66
+ * el-alert 的 light-9 背景变深、与同色系文字几乎同色("灰蒙蒙看不清")。
67
+ */
68
+ export function generateElShades(hex: string, isDark = false): ElColorShades {
55
69
  const shades = {} as ElColorShades
70
+ const toBg = isDark ? '#000000' : '#ffffff'
56
71
  for (let i = 1; i <= 9; i++) {
57
- shades[`light-${i}` as keyof ElColorShades] = mix('#ffffff', hex, i * 10)
72
+ shades[`light-${i}` as keyof ElColorShades] = mix(toBg, hex, (10 - i) * 10)
58
73
  }
59
- shades['dark-2'] = mix('#000000', hex, 20)
74
+ shades['dark-2'] = mix(isDark ? '#ffffff' : '#000000', hex, 20)
60
75
  return shades
61
76
  }