updateBackground.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { colorIsDark, lighten, darken } from '/@/utils/color';
  2. import { appStore } from '/@/store/modules/app';
  3. import { ThemeEnum } from '/@/enums/appEnum';
  4. import { setCssVar } from './util';
  5. const HEADER_BG_COLOR_VAR = '--header-bg-color';
  6. const HEADER_BG_HOVER_COLOR_VAR = '--header-bg-hover-color';
  7. const HEADER_MENU_ACTIVE_BG_COLOR_VAR = '--header-active-menu-bg-color';
  8. const SIDER_DARK_BG_COLOR = '--sider-dark-bg-color';
  9. const SIDER_DARK_DARKEN_BG_COLOR = '--sider-dark-darken-bg-color';
  10. const SIDER_LIGHTEN_BG_COLOR = '--sider-dark-lighten-bg-color';
  11. /**
  12. * Change the background color of the top header
  13. * @param color
  14. */
  15. export function updateHeaderBgColor(color?: string) {
  16. const darkMode = appStore.getDarkMode === ThemeEnum.DARK;
  17. if (!color) {
  18. if (darkMode) {
  19. color = '#151515';
  20. } else {
  21. color = appStore.getProjectConfig.headerSetting.bgColor;
  22. }
  23. }
  24. // bg color
  25. setCssVar(HEADER_BG_COLOR_VAR, color);
  26. // hover color
  27. const hoverColor = lighten(color!, 6);
  28. setCssVar(HEADER_BG_HOVER_COLOR_VAR, hoverColor);
  29. setCssVar(HEADER_MENU_ACTIVE_BG_COLOR_VAR, hoverColor);
  30. // Determine the depth of the color value and automatically switch the theme
  31. const isDark = colorIsDark(color!);
  32. appStore.commitProjectConfigState({
  33. headerSetting: {
  34. theme: isDark || darkMode ? ThemeEnum.DARK : ThemeEnum.LIGHT,
  35. },
  36. });
  37. }
  38. /**
  39. * Change the background color of the left menu
  40. * @param color bg color
  41. */
  42. export function updateSidebarBgColor(color?: string) {
  43. // if (!isHexColor(color)) return;
  44. const darkMode = appStore.getDarkMode === ThemeEnum.DARK;
  45. if (!color) {
  46. if (darkMode) {
  47. color = '#212121';
  48. } else {
  49. color = appStore.getProjectConfig.menuSetting.bgColor;
  50. }
  51. }
  52. setCssVar(SIDER_DARK_BG_COLOR, color);
  53. setCssVar(SIDER_DARK_DARKEN_BG_COLOR, darken(color!, 6));
  54. setCssVar(SIDER_LIGHTEN_BG_COLOR, lighten(color!, 5));
  55. // only #ffffff is light
  56. // Only when the background color is #fff, the theme of the menu will be changed to light
  57. const isLight = ['#fff', '#ffffff'].includes(color!.toLowerCase());
  58. appStore.commitProjectConfigState({
  59. menuSetting: {
  60. theme: isLight && !darkMode ? ThemeEnum.LIGHT : ThemeEnum.DARK,
  61. },
  62. });
  63. }