You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
3.1 KiB

1 month ago
/**
*
*
1 month ago
*
*/
import Colors from '@/constants/Colors';
import { ThemeEnum } from '@/constants/theme';
1 month ago
/**
*
*
* @param theme - ThemeEnum
1 month ago
* @param colorName -
* @returns
*/
export function getThemeColor(
theme: ThemeEnum,
colorName: keyof typeof Colors.light & keyof typeof Colors.dark & keyof typeof Colors.orange
1 month ago
): string {
return Colors[theme][colorName];
}
/**
*
*
* @param theme - ThemeEnum
1 month ago
* @returns
*/
export function getThemeColors(theme: ThemeEnum) {
1 month ago
return Colors[theme];
}
/**
*
*
1 month ago
* @param lightStyle -
* @param darkStyle -
* @param orangeStyle -
1 month ago
* @param theme -
* @returns
*
1 month ago
* @example
* ```tsx
* const style = createThemedStyle(
* { backgroundColor: '#fff' },
* { backgroundColor: '#000' },
* theme
* );
* ```
*/
export function createThemedStyle<T>(
lightStyle: T,
darkStyle: T,
orangeStyle: T,
theme: ThemeEnum
1 month ago
): T {
switch (theme) {
case ThemeEnum.LIGHT:
return lightStyle;
case ThemeEnum.DARK:
return darkStyle;
case ThemeEnum.ORANGE:
return orangeStyle;
default:
return lightStyle;
}
1 month ago
}
/**
*
*
1 month ago
* @param lightValue -
* @param darkValue -
* @param orangeValue -
1 month ago
* @param theme -
* @returns
*
1 month ago
* @example
* ```tsx
* const fontSize = selectByTheme(14, 16, theme);
* ```
*/
export function selectByTheme<T>(
lightValue: T,
darkValue: T,
orangeValue: T,
theme: ThemeEnum
1 month ago
): T {
switch (theme) {
case ThemeEnum.LIGHT:
return lightValue;
case ThemeEnum.DARK:
return darkValue;
case ThemeEnum.ORANGE:
return orangeValue;
default:
return lightValue;
}
1 month ago
}
/**
*
*
1 month ago
* @param color -
* @param opacity - 0-1
* @returns
*
1 month ago
* @example
* ```tsx
* const color = withOpacity('#000000', 0.5); // rgba(0, 0, 0, 0.5)
* ```
*/
export function withOpacity(color: string, opacity: number): string {
// 移除 # 号
const hex = color.replace('#', '');
1 month ago
// 转换为 RGB
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
1 month ago
return `rgba(${r}, ${g}, ${b}, ${opacity})`;
}
/**
*
*
1 month ago
* @param theme -
* @returns
*/
export function isDarkTheme(theme: ThemeEnum): boolean {
return theme === ThemeEnum.DARK;
1 month ago
}
/**
*
*
1 month ago
* @param theme -
* @returns
*/
export function isLightTheme(theme: ThemeEnum): boolean {
return theme === ThemeEnum.LIGHT;
}
/**
*
*
* @param theme -
* @returns
*/
export function isOrangeTheme(theme: ThemeEnum): boolean {
return theme === ThemeEnum.ORANGE;
1 month ago
}