All files / hooks useThemeColors.ts

100% Statements 7/7
50% Branches 1/2
100% Functions 3/3
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340                                                                                                                                                                                                                                              28x                                                                                                                                                                                         28x                                                                                                                                                                                                                       169x   169x 125x               134x 134x              
/**
 * 主题感知颜色 Hook
 *
 * 根据当前主题模式返回正确的颜色值
 * 解决深色模式和浅色模式颜色不一致的问题
 */
 
import { useMemo } from 'react';
 
import { withAlpha } from '@/src/constants/colors';
import {
  primaryScale,
  neutralScale,
  successScale,
  warningScale,
  errorScale,
  infoScale,
  lightSemanticColors,
  darkSemanticColors,
} from '@/src/design-system/tokens/colors';
 
import { useThemeAwareColorScheme } from './useThemeAwareColorScheme';
 
/**
 * 主题颜色接口
 */
export interface ThemeColors {
  // 基础颜色
  background: string;
  backgroundSubtle: string;
  backgroundMuted: string;
  backgroundElevated: string;
  cardBackground: string;
 
  // 文本颜色
  text: string;
  textSecondary: string;
  textTertiary: string;
  textMuted: string;
 
  // 边框颜色
  border: string;
  borderMuted: string;
  borderFocus: string;
 
  // 品牌色
  primary: string;
  primaryLight: string;
  primaryDark: string;
 
  // 图标颜色
  icon: string;
  iconSecondary: string;
 
  // 语义色
  success: string;
  successMuted: string;
  warning: string;
  warningMuted: string;
  error: string;
  errorMuted: string;
  info: string;
  infoMuted: string;
 
  // 交互状态
  hover: string;
  active: string;
  selected: string;
 
  // 遮罩
  overlay: string;
  overlaySubtle: string;
 
  // 输入框
  inputBackground: string;
  inputBorder: string;
  inputPlaceholder: string;
 
  // 标签栏
  tabBackground: string;
  tabIconDefault: string;
  tabIconSelected: string;
 
  // 扫描按钮
  scanButtonBackground: string;
  scanButtonBorder: string;
  scanButtonIcon: string;
 
  // 原始 scale(用于需要特定阶梯的场景)
  neutralScale: typeof neutralScale;
  primaryScale: typeof primaryScale;
 
  // 品牌色带透明度的变体
  tint: string;
  tintAlpha10: string;
  tintAlpha20: string;
  tintAlpha30: string;
  tintAlpha40: string;
  tintAlpha50: string;
 
  // 图标色带透明度的变体
  iconAlpha05: string;
  iconAlpha10: string;
  iconAlpha15: string;
  iconAlpha20: string;
  iconAlpha30: string;
  iconAlpha40: string;
  iconAlpha60: string;
 
  // 背景变体
  bg: string;
 
  // 工具函数
  withAlpha: (color: string, alpha: number) => string;
}
 
/**
 * 浅色主题颜色
 */
const lightColors: ThemeColors = {
  // 基础颜色
  background: '#FFFFFF',
  backgroundSubtle: neutralScale.neutral1,
  backgroundMuted: neutralScale.neutral2,
  backgroundElevated: '#FFFFFF',
  cardBackground: '#FFFFFF',
 
  // 文本颜色
  text: neutralScale.neutral12,
  textSecondary: neutralScale.neutral9,
  textTertiary: neutralScale.neutral7,
  textMuted: neutralScale.neutral6,
 
  // 边框颜色
  border: neutralScale.neutral4,
  borderMuted: neutralScale.neutral3,
  borderFocus: primaryScale.primary7,
 
  // 品牌色
  primary: primaryScale.primary7,
  primaryLight: primaryScale.primary3,
  primaryDark: primaryScale.primary9,
 
  // 图标颜色
  icon: neutralScale.neutral9,
  iconSecondary: neutralScale.neutral6,
 
  // 语义色
  success: successScale.success7,
  successMuted: successScale.success2,
  warning: warningScale.warning7,
  warningMuted: warningScale.warning2,
  error: errorScale.error7,
  errorMuted: errorScale.error2,
  info: infoScale.info7,
  infoMuted: infoScale.info2,
 
  // 交互状态
  hover: neutralScale.neutral2,
  active: neutralScale.neutral3,
  selected: primaryScale.primary2,
 
  // 遮罩
  overlay: 'rgba(0, 0, 0, 0.5)',
  overlaySubtle: 'rgba(0, 0, 0, 0.3)',
 
  // 输入框
  inputBackground: '#FFFFFF',
  inputBorder: neutralScale.neutral4,
  inputPlaceholder: neutralScale.neutral6,
 
  // 标签栏
  tabBackground: '#FFFFFF',
  tabIconDefault: neutralScale.neutral7,
  tabIconSelected: primaryScale.primary7,
 
  // 扫描按钮
  scanButtonBackground: primaryScale.primary7,
  scanButtonBorder: '#FFFFFF',
  scanButtonIcon: '#FFFFFF',
 
  // 原始 scale
  neutralScale,
  primaryScale,
 
  // 品牌色及透明度变体
  tint: primaryScale.primary7,
  tintAlpha10: withAlpha(primaryScale.primary7, 0.1),
  tintAlpha20: withAlpha(primaryScale.primary7, 0.2),
  tintAlpha30: withAlpha(primaryScale.primary7, 0.3),
  tintAlpha40: withAlpha(primaryScale.primary7, 0.4),
  tintAlpha50: withAlpha(primaryScale.primary7, 0.5),
 
  // 图标色及透明度变体
  iconAlpha05: withAlpha(neutralScale.neutral9, 0.05),
  iconAlpha10: withAlpha(neutralScale.neutral9, 0.1),
  iconAlpha15: withAlpha(neutralScale.neutral9, 0.15),
  iconAlpha20: withAlpha(neutralScale.neutral9, 0.2),
  iconAlpha30: withAlpha(neutralScale.neutral9, 0.3),
  iconAlpha40: withAlpha(neutralScale.neutral9, 0.4),
  iconAlpha60: withAlpha(neutralScale.neutral9, 0.6),
 
  // 背景变体
  bg: '#FFFFFF',
 
  // 工具函数
  withAlpha,
};
 
/**
 * 深色主题颜色
 */
const darkColors: ThemeColors = {
  // 基础颜色
  background: '#0A0A0A',
  backgroundSubtle: '#141414',
  backgroundMuted: '#1F1F1F',
  backgroundElevated: '#252525',
  cardBackground: '#1A1A1A',
 
  // 文本颜色
  text: '#FAFAFA',
  textSecondary: '#A1A1A1',
  textTertiary: '#737373',
  textMuted: '#525252',
 
  // 边框颜色
  border: '#2E2E2E',
  borderMuted: '#262626',
  borderFocus: primaryScale.primary6,
 
  // 品牌色
  primary: primaryScale.primary6,
  primaryLight: primaryScale.primary4,
  primaryDark: primaryScale.primary8,
 
  // 图标颜色
  icon: '#A1A1A1',
  iconSecondary: '#737373',
 
  // 语义色
  success: successScale.success5,
  successMuted: '#0D2818',
  warning: warningScale.warning5,
  warningMuted: '#2D1F0A',
  error: errorScale.error5,
  errorMuted: '#2D0F0F',
  info: infoScale.info5,
  infoMuted: '#0F1A2D',
 
  // 交互状态
  hover: '#1F1F1F',
  active: '#2A2A2A',
  selected: '#2D1F1A',
 
  // 遮罩
  overlay: 'rgba(0, 0, 0, 0.7)',
  overlaySubtle: 'rgba(0, 0, 0, 0.5)',
 
  // 输入框
  inputBackground: '#1A1A1A',
  inputBorder: '#2E2E2E',
  inputPlaceholder: '#525252',
 
  // 标签栏
  tabBackground: '#0A0A0A',
  tabIconDefault: '#737373',
  tabIconSelected: primaryScale.primary6,
 
  // 扫描按钮
  scanButtonBackground: primaryScale.primary6,
  scanButtonBorder: '#0A0A0A',
  scanButtonIcon: '#0A0A0A',
 
  // 原始 scale
  neutralScale,
  primaryScale,
 
  // 品牌色及透明度变体
  tint: primaryScale.primary6,
  tintAlpha10: withAlpha(primaryScale.primary6, 0.1),
  tintAlpha20: withAlpha(primaryScale.primary6, 0.2),
  tintAlpha30: withAlpha(primaryScale.primary6, 0.3),
  tintAlpha40: withAlpha(primaryScale.primary6, 0.4),
  tintAlpha50: withAlpha(primaryScale.primary6, 0.5),
 
  // 图标色及透明度变体
  iconAlpha05: withAlpha('#A1A1A1', 0.05),
  iconAlpha10: withAlpha('#A1A1A1', 0.1),
  iconAlpha15: withAlpha('#A1A1A1', 0.15),
  iconAlpha20: withAlpha('#A1A1A1', 0.2),
  iconAlpha30: withAlpha('#A1A1A1', 0.3),
  iconAlpha40: withAlpha('#A1A1A1', 0.4),
  iconAlpha60: withAlpha('#A1A1A1', 0.6),
 
  // 背景变体
  bg: '#1A1A1A',
 
  // 工具函数
  withAlpha,
};
 
/**
 * 获取主题感知颜色
 *
 * @returns 当前主题的颜色对象
 *
 * @example
 * ```tsx
 * function MyComponent() {
 *   const colors = useThemeColors();
 *   return (
 *     <View style={{ backgroundColor: colors.background }}>
 *       <Text style={{ color: colors.text }}>Hello</Text>
 *     </View>
 *   );
 * }
 * ```
 */
export function useThemeColors(): ThemeColors {
  const colorScheme = useThemeAwareColorScheme();
 
  return useMemo(() => {
    return colorScheme === 'dark' ? darkColors : lightColors;
  }, [colorScheme]);
}
 
/**
 * 获取当前主题模式
 */
export function useIsDarkMode(): boolean {
  const colorScheme = useThemeAwareColorScheme();
  return colorScheme === 'dark';
}
 
/**
 * 导出静态颜色(用于不需要响应式的场景)
 */
export { lightColors, darkColors };