goview_vue/src/utils/style.ts

90 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-03-26 20:59:20 +08:00
import Color from 'color'
import { useDesignStore } from '@/store/modules/designStore/designStore'
import { PickCreateComponentType } from '@/packages/index.d'
import { EditCanvasConfigType } from '@/store/modules/chartEditStore/chartEditStore.d'
type AttrType = PickCreateComponentType<'attr'>
type StylesType = PickCreateComponentType<'styles'>
// 动画
export const animationsClass = (animations: string[]) => {
if (animations.length) {
return `animate__animated animate__${animations[0]}`
}
return ''
}
// 滤镜
export const getFilterStyle = (styles: StylesType | EditCanvasConfigType) => {
const { opacity, saturate, contrast, hueRotate, brightness } = styles
return {
opacity: opacity,
filter: `saturate(${saturate}) contrast(${contrast}) hue-rotate(${hueRotate}deg) brightness(${brightness})`,
}
}
// 变换
2022-06-29 09:55:44 +08:00
export const getTransformStyle = (styles: StylesType) => {
const { rotateZ, rotateX, rotateY, skewX, skewY } = styles
return {
transform: `rotateZ(${rotateZ || 0}deg) rotateX(${rotateX || 0}deg) rotateY(${rotateY || 0}deg) skewX(${skewX || 0}deg) skewY(${skewY || 0}deg)`,
}
}
2022-03-26 20:59:20 +08:00
/**
* * hsla
* @param color
* @param alpha 1
* @returns
*/
export function alpha(color: string, alpha = 1 ) {
return Color(color).alpha(alpha).toString()
}
/**
* *
* rgba(10, 10, 10, 0.8) -> rgba(10, 10, 10, 0.4)
* @param color
* @param concentration 0~1
* @returns
*/
export function fade(color: string, fade: number) {
return Color(color).fade(fade).toString()
}
/**
* *
* hsl(100, 50%, 10%) -> hsl(100, 50%, 50%)
* @param color
* @param concentration 0~1
* @returns
*/
export function lighten(color: string, concentration: number) {
return Color(color).lighten(concentration).toString()
}
/**
* *
* hsl(100, 50%, 50%) -> hsl(100, 50%, 25%)
* @param color
* @param concentration 0~1
* @returns
*/
export function darken(color: string, concentration: number) {
return Color(color).darken(concentration).toString()
}
2021-12-15 22:16:16 +08:00
2021-12-18 16:36:43 +08:00
/**
* *
* @param themeName
2022-03-12 15:25:26 +08:00
* @returns
2021-12-18 16:36:43 +08:00
*/
2021-12-15 22:16:16 +08:00
export const setHtmlTheme = (themeName?: string) => {
const e = window.document.documentElement
if (themeName) {
e.setAttribute('data-theme', themeName)
2021-12-15 22:16:16 +08:00
return
}
const designStore = useDesignStore()
e.setAttribute('data-theme', designStore.themeName)
2022-03-26 20:59:20 +08:00
}