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.

123 lines
2.5 KiB

1 month ago
/**
*
*
*
*/
import Colors from '@/constants/Colors';
/**
*
*
* @param theme - 'light' | 'dark'
* @param colorName -
* @returns
*/
export function getThemeColor(
theme: 'light' | 'dark',
colorName: keyof typeof Colors.light & keyof typeof Colors.dark
): string {
return Colors[theme][colorName];
}
/**
*
*
* @param theme - 'light' | 'dark'
* @returns
*/
export function getThemeColors(theme: 'light' | 'dark') {
return Colors[theme];
}
/**
*
*
* @param lightStyle -
* @param darkStyle -
* @param theme -
* @returns
*
* @example
* ```tsx
* const style = createThemedStyle(
* { backgroundColor: '#fff' },
* { backgroundColor: '#000' },
* theme
* );
* ```
*/
export function createThemedStyle<T>(
lightStyle: T,
darkStyle: T,
theme: 'light' | 'dark'
): T {
return theme === 'dark' ? darkStyle : lightStyle;
}
/**
*
*
* @param lightValue -
* @param darkValue -
* @param theme -
* @returns
*
* @example
* ```tsx
* const fontSize = selectByTheme(14, 16, theme);
* ```
*/
export function selectByTheme<T>(
lightValue: T,
darkValue: T,
theme: 'light' | 'dark'
): T {
return theme === 'dark' ? darkValue : lightValue;
}
/**
*
*
* @param color -
* @param opacity - 0-1
* @returns
*
* @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('#', '');
// 转换为 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);
return `rgba(${r}, ${g}, ${b}, ${opacity})`;
}
/**
*
*
* @param theme -
* @returns
*/
export function isDarkTheme(theme: 'light' | 'dark'): boolean {
return theme === 'dark';
}
/**
*
*
* @param theme -
* @returns
*/
export function isLightTheme(theme: 'light' | 'dark'): boolean {
return theme === 'light';
}