1- import { mainTheme , themeColors } from " ./main" ;
1+ import { mainTheme , themeColors } from ' ./main' ;
22
33export namespace Theme {
4- export type ThemeType = "default" | "darkBlue" | "deepBlue" | "chinaRed" ;
4+ export type ThemeType = 'default' | 'darkBlue' | 'deepBlue' | 'chinaRed' ;
5+ }
6+
7+ let themeTypeGetter : ( ( ) => string | null ) | null = null ;
8+
9+ export function setThemeTypeGetter ( fn : ( ) => string | null ) {
10+ themeTypeGetter = fn ;
511}
612
7- // 获取当前主题的主色
813function getCurrentThemeColor ( ) : string {
9- const themeType = localStorage . getItem ( "themeType" ) || " default" ;
14+ const themeType = ( themeTypeGetter && themeTypeGetter ( ) ) || ' default' ;
1015 return themeColors [ themeType ] || themeColors . default ;
1116}
1217
@@ -16,12 +21,12 @@ export function lighten(amount: number, color?: string, alphaValue?: number): st
1621 const actualColor = color || getCurrentThemeColor ( ) ;
1722 const hsl = hexToHSL ( actualColor ) ;
1823 const hexColor = hslToHex ( hsl . h , hsl . s , Math . min ( 100 , hsl . l + amount ) ) ;
19-
24+
2025 // 如果提供了透明度参数,应用透明度
2126 if ( alphaValue !== undefined ) {
2227 return alpha ( alphaValue , hexColor ) ;
2328 }
24-
29+
2530 return hexColor ;
2631}
2732
@@ -31,12 +36,12 @@ export function darken(amount: number, color?: string, alphaValue?: number): str
3136 const actualColor = color || getCurrentThemeColor ( ) ;
3237 const hsl = hexToHSL ( actualColor ) ;
3338 const hexColor = hslToHex ( hsl . h , hsl . s , Math . max ( 0 , hsl . l - amount ) ) ;
34-
39+
3540 // 如果提供了透明度参数,应用透明度
3641 if ( alphaValue !== undefined ) {
3742 return alpha ( alphaValue , hexColor ) ;
3843 }
39-
44+
4045 return hexColor ;
4146}
4247
@@ -64,12 +69,12 @@ export function alpha(alphaValue: number, color?: string): string {
6469 const alpha = Math . max ( 0 , Math . min ( 1 , alphaValue ) ) ;
6570
6671 // 移除#号并处理缩写形式
67- let hex = actualColor . replace ( / ^ # / , "" ) ;
72+ let hex = actualColor . replace ( / ^ # / , '' ) ;
6873 if ( hex . length === 3 ) {
6974 hex = hex
70- . split ( "" )
71- . map ( ( char ) => char + char )
72- . join ( "" ) ;
75+ . split ( '' )
76+ . map ( char => char + char )
77+ . join ( '' ) ;
7378 }
7479
7580 // 解析RGB值
@@ -90,12 +95,12 @@ interface HSL {
9095
9196export function hexToHSL ( hex : string ) : HSL {
9297 // 移除#号并处理缩写形式
93- let hexValue = hex . replace ( / ^ # / , "" ) ;
98+ let hexValue = hex . replace ( / ^ # / , '' ) ;
9499 if ( hexValue . length === 3 ) {
95100 hexValue = hexValue
96- . split ( "" )
97- . map ( ( char ) => char + char )
98- . join ( "" ) ;
101+ . split ( '' )
102+ . map ( char => char + char )
103+ . join ( '' ) ;
99104 }
100105
101106 // 解析RGB值
@@ -133,7 +138,7 @@ export function hexToHSL(hex: string): HSL {
133138 return {
134139 h : Math . round ( h * 360 ) ,
135140 s : Math . round ( s * 100 ) ,
136- l : Math . round ( l * 100 ) ,
141+ l : Math . round ( l * 100 )
137142 } ;
138143}
139144
@@ -170,15 +175,15 @@ export function hslToHex(h: number, s: number, l: number): string {
170175 // 转换为十六进制
171176 const toHex = ( x : number ) : string => {
172177 const hex = Math . round ( x * 255 ) . toString ( 16 ) ;
173- return hex . length === 1 ? "0" + hex : hex ;
178+ return hex . length === 1 ? '0' + hex : hex ;
174179 } ;
175180
176181 return `#${ toHex ( r ) } ${ toHex ( g ) } ${ toHex ( b ) } ` ;
177182}
178183
179184export const useTheme = ( ) => {
180185 // 获取主题类型
181- const getThemeType = ( ) => localStorage . getItem ( " themeType" ) || " default" ;
186+ const getThemeType = ( ) => localStorage . getItem ( ' themeType' ) || ' default' ;
182187 const html = document . documentElement as HTMLElement ;
183188
184189 // 通用设置主题的方法
@@ -190,13 +195,13 @@ export const useTheme = () => {
190195
191196 // 切换主题方法
192197 const switchTheme = ( theme : string ) => {
193- localStorage . setItem ( " themeType" , theme ) ;
194- if ( theme === " darkBlue" ) {
195- html . setAttribute ( " class" , " darkBlue" ) ;
196- } else if ( theme === " deepBlue" ) {
197- html . setAttribute ( " class" , " deepBlue" ) ;
198+ localStorage . setItem ( ' themeType' , theme ) ;
199+ if ( theme === ' darkBlue' ) {
200+ html . setAttribute ( ' class' , ' darkBlue' ) ;
201+ } else if ( theme === ' deepBlue' ) {
202+ html . setAttribute ( ' class' , ' deepBlue' ) ;
198203 } else {
199- html . setAttribute ( " class" , "" ) ;
204+ html . setAttribute ( ' class' , '' ) ;
200205 }
201206
202207 // 应用所有主题
@@ -213,6 +218,6 @@ export const useTheme = () => {
213218 return {
214219 initTheme,
215220 setMainTheme,
216- switchTheme,
221+ switchTheme
217222 } ;
218223} ;
0 commit comments