style.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. opacity: opacity,
  19. filter: `saturate(${saturate}) contrast(${contrast}) hue-rotate(${hueRotate}deg) brightness(${brightness})`,
  20. }
  21. }
  22. // 变换
  23. export const getTranstormStyle = (styles: StylesType) => {
  24. const { rotateZ, rotateX, rotateY, skewX, skewY } = styles
  25. return {
  26. transform: `rotateZ(${rotateZ || 0}deg) rotateX(${rotateX || 0}deg) rotateY(${rotateY || 0}deg) skewX(${skewX || 0}deg) skewY(${skewY || 0}deg)`,
  27. }
  28. }
  29. /**
  30. * * hsla 转换
  31. * @param color 颜色
  32. * @param alpha 默认1
  33. * @returns
  34. */
  35. export function alpha(color: string, alpha = 1 ) {
  36. return Color(color).alpha(alpha).toString()
  37. }
  38. /**
  39. * * 颜色透明
  40. * rgba(10, 10, 10, 0.8) -> rgba(10, 10, 10, 0.4)
  41. * @param color 颜色
  42. * @param concentration 0~1 浓度
  43. * @returns
  44. */
  45. export function fade(color: string, fade: number) {
  46. return Color(color).fade(fade).toString()
  47. }
  48. /**
  49. * * 颜色变亮
  50. * hsl(100, 50%, 10%) -> hsl(100, 50%, 50%)
  51. * @param color 颜色
  52. * @param concentration 0~1 浓度
  53. * @returns
  54. */
  55. export function lighten(color: string, concentration: number) {
  56. return Color(color).lighten(concentration).toString()
  57. }
  58. /**
  59. * * 颜色变暗
  60. * hsl(100, 50%, 50%) -> hsl(100, 50%, 25%)
  61. * @param color 颜色
  62. * @param concentration 0~1 浓度
  63. * @returns
  64. */
  65. export function darken(color: string, concentration: number) {
  66. return Color(color).darken(concentration).toString()
  67. }
  68. /**
  69. * * 修改主题色
  70. * @param themeName 主题名称
  71. * @returns
  72. */
  73. export const setHtmlTheme = (themeName?: string) => {
  74. const e = window.document.documentElement
  75. if (themeName) {
  76. e.setAttribute('data-theme', themeName)
  77. return
  78. }
  79. const designStore = useDesignStore()
  80. e.setAttribute('data-theme', designStore.themeName)
  81. }