style.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import Color from 'color'
  2. import { useDesignStore } from '@/store/modules/designStore/designStore'
  3. import { PickCreateComponentType } from '@/packages/index.d'
  4. import { EditCanvasConfigType } from '@/store/modules/chartEditStore/chartEditStore.d'
  5. type AttrType = PickCreateComponentType<'attr'>
  6. type StylesType = PickCreateComponentType<'styles'>
  7. // 动画
  8. export const animationsClass = (animations: string[]) => {
  9. if (animations.length) {
  10. return `animate__animated animate__${animations[0]}`
  11. }
  12. return ''
  13. }
  14. // 滤镜
  15. export const getFilterStyle = (styles: StylesType | EditCanvasConfigType) => {
  16. const { opacity, saturate, contrast, hueRotate, brightness } = styles
  17. return {
  18. // 透明度
  19. opacity: opacity,
  20. filter: `saturate(${saturate}) contrast(${contrast}) hue-rotate(${hueRotate}deg) brightness(${brightness})`,
  21. }
  22. }
  23. // 变换
  24. export const getTranstormStyle = (styles: StylesType) => {
  25. const { rotateZ, rotateX, rotateY, skewX, skewY } = styles
  26. return {
  27. transform: `rotateZ(${rotateZ || 0}deg) rotateX(${rotateX || 0}deg) rotateY(${rotateY || 0}deg) skewX(${skewX || 0}deg) skewY(${skewY || 0}deg)`,
  28. }
  29. }
  30. /**
  31. * * hsla 转换
  32. * @param color 颜色
  33. * @param alpha 默认1
  34. * @returns
  35. */
  36. export function alpha(color: string, alpha = 1 ) {
  37. return Color(color).alpha(alpha).toString()
  38. }
  39. /**
  40. * * 颜色透明
  41. * rgba(10, 10, 10, 0.8) -> rgba(10, 10, 10, 0.4)
  42. * @param color 颜色
  43. * @param concentration 0~1 浓度
  44. * @returns
  45. */
  46. export function fade(color: string, fade: number) {
  47. return Color(color).fade(fade).toString()
  48. }
  49. /**
  50. * * 颜色变亮
  51. * hsl(100, 50%, 10%) -> hsl(100, 50%, 50%)
  52. * @param color 颜色
  53. * @param concentration 0~1 浓度
  54. * @returns
  55. */
  56. export function lighten(color: string, concentration: number) {
  57. return Color(color).lighten(concentration).toString()
  58. }
  59. /**
  60. * * 颜色变暗
  61. * hsl(100, 50%, 50%) -> hsl(100, 50%, 25%)
  62. * @param color 颜色
  63. * @param concentration 0~1 浓度
  64. * @returns
  65. */
  66. export function darken(color: string, concentration: number) {
  67. return Color(color).darken(concentration).toString()
  68. }
  69. /**
  70. * * 修改主题色
  71. * @param themeName 主题名称
  72. * @returns
  73. */
  74. export const setHtmlTheme = (themeName?: string) => {
  75. const e = window.document.documentElement
  76. if (themeName) {
  77. e.setAttribute('data-theme', themeName)
  78. return
  79. }
  80. const designStore = useDesignStore()
  81. e.setAttribute('data-theme', designStore.themeName)
  82. }