Newer
Older
KaiFengPC / src / utils / theme.js
@zhangdeliang zhangdeliang on 23 May 1 KB 初始化项目
  1. // 处理主题样式
  2. export function handleThemeStyle(theme) {
  3. document.documentElement.style.setProperty('--el-color-primary', theme);
  4. for (let i = 1; i <= 9; i++) {
  5. document.documentElement.style.setProperty(`--el-color-primary-light-${i}`, `${getLightColor(theme, i / 10)}`);
  6. }
  7. for (let i = 1; i <= 9; i++) {
  8. document.documentElement.style.setProperty(`--el-color-primary-dark-${i}`, `${getDarkColor(theme, i / 10)}`);
  9. }
  10. }
  11.  
  12. // hex颜色转rgb颜色
  13. export function hexToRgb(str) {
  14. str = str.replace('#', '');
  15. let hexs = str.match(/../g);
  16. for (let i = 0; i < 3; i++) {
  17. hexs[i] = parseInt(hexs[i], 16);
  18. }
  19. return hexs;
  20. }
  21.  
  22. // rgb颜色转Hex颜色
  23. export function rgbToHex(r, g, b) {
  24. let hexs = [r.toString(16), g.toString(16), b.toString(16)];
  25. for (let i = 0; i < 3; i++) {
  26. if (hexs[i].length == 1) {
  27. hexs[i] = `0${hexs[i]}`;
  28. }
  29. }
  30. return `#${hexs.join('')}`;
  31. }
  32.  
  33. // 变浅颜色值
  34. export function getLightColor(color, level) {
  35. let rgb = hexToRgb(color);
  36. for (let i = 0; i < 3; i++) {
  37. rgb[i] = Math.floor((255 - rgb[i]) * level + rgb[i]);
  38. }
  39. return rgbToHex(rgb[0], rgb[1], rgb[2]);
  40. }
  41.  
  42. // 变深颜色值
  43. export function getDarkColor(color, level) {
  44. let rgb = hexToRgb(color);
  45. for (let i = 0; i < 3; i++) {
  46. rgb[i] = Math.floor(rgb[i] * (1 - level));
  47. }
  48. return rgbToHex(rgb[0], rgb[1], rgb[2]);
  49. }