-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathuseColors.ts
210 lines (177 loc) · 6.39 KB
/
useColors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import type { ColorVariables, CssColor } from '../services/color'
import { capitalize, computed } from 'vue'
import { useGlobalConfig } from '../services/global-config/global-config'
import { warn } from '../utils/console'
import { useCache } from './useCache'
import { useReactiveComputed } from './useReactiveComputed'
import {
getBoxShadowColor,
getBoxShadowColorFromBg,
getHoverColor,
getFocusColor,
getGradientBackground,
isColor,
shiftHSLAColor,
setHSLAColor,
isCSSVariable,
colorToRgba,
getStateMaskGradientBackground,
getColorLightness,
cssVariableName,
normalizeColorName,
type ColorInput,
} from '../services/color/utils'
import kebabCase from 'lodash/kebabCase'
/**
* You can add these props to any component by destructuring them inside props option.
* @example
* props: { ...useColorProps, componentsOwnProp, etc. }
* It's better to add props at the beginning to make sure that component own props will be used instead in case of collision.
*/
export const useColorProps = {
color: {
type: String,
default: '',
},
}
export const useColors = () => {
const gc = useGlobalConfig()
if (!gc) {
throw new Error('useColors must be used in setup function or Vuestic GlobalConfigPlugin is not registered!')
}
const { globalConfig } = gc
const colors = useReactiveComputed<ColorVariables>({
get: () => globalConfig.value.colors!.presets[globalConfig.value.colors!.currentPresetName],
set: (v: ColorVariables) => { setColors(v) },
})
const setColors = (colors: Partial<ColorVariables>): void => {
globalConfig.value.colors!.presets[globalConfig.value.colors!.currentPresetName] = {
...globalConfig.value.colors.variables,
...colors,
} as ColorVariables
}
const getColors = (): ColorVariables => {
return colors
}
/**
* Returns color from config variables by name or return prop if color is a valid hex, hsl, hsla, rgb or rgba color.
* @param prop - should be color name or color in hex, hsl, hsla, rgb or rgba format.
* @param preferVariables - function should return (if possible) CSS variable instead of hex (hex is needed to set opacity).
*/
const getColorStrict = (prop: string, preferVariables?: boolean): CssColor | null => {
if (prop === 'transparent') {
return '#ffffff00'
}
if (prop === 'currentColor') {
return prop
}
if (prop?.startsWith('on')) {
const colorName = prop.slice(2)
if (colors[normalizeColorName(colorName)]) {
return getColor(getTextColor(getColor(colorName)), undefined, preferVariables)
}
}
const colorValue = colors[prop] || colors[normalizeColorName(prop)]
if (colorValue) {
return preferVariables ? `var(${cssVariableName(prop)})` : colorValue
}
if (isColor(prop)) {
return prop
}
if (preferVariables && isCSSVariable(prop)) {
return prop
}
warn(`'${prop}' is not a proper color! Use HEX or default color themes
names (https://vuestic.dev/en/styles/colors#default-color-themes)`)
return null
}
/**
* Returns color from config variables by name or return prop if color is a valid hex, hsl, hsla, rgb or rgba color.
* @param prop - should be color name or color in hex, hsl, hsla, rgb or rgba format.
* @param preferVariables - function should return (if possible) CSS variable instead of hex (hex is needed to set opacity).
* @param defaultColor - this color will be used if prop is invalid. By default it is primary color.
*/
const getColor = (prop?: string, defaultColor?: string, preferVariables?: boolean): CssColor => {
if (!defaultColor) {
defaultColor = colors.primary
}
if (!prop) { return defaultColor }
return getColorStrict(prop, preferVariables) ?? defaultColor
}
const getComputedColor = (color: string) => {
return computed({
get () { return getColor(color) },
set (v: string) { setColors({ [color]: v }) },
})
}
const colorsToCSSVariable = (colors: { [colorName: string]: string | undefined }, prefix = 'va') => {
return Object
.keys(colors)
.filter((key) => colors[key] !== undefined)
.reduce((acc: Record<string, any>, colorName: string) => {
acc[`--${prefix}-${kebabCase(colorName)}`] = getColor(colors[colorName]!, undefined, true)
acc[`--${prefix}-on-${kebabCase(colorName)}`] = getColor(getTextColor(getColor(colors[colorName]!)), undefined, true)
return acc
}, {})
}
const cache = useCache()
const getColorLightnessFromCache = (color: ColorInput) => {
if (typeof color !== 'string') {
return getColorLightness(color)
}
if (!cache.colorContrast[color]) {
cache.colorContrast[color] = getColorLightness(color)
}
return cache.colorContrast[color]
}
const computedDarkColor = computed(() => {
return getColorLightnessFromCache(getColor('textPrimary')) > (255 / 2) ? 'textInverted' : 'textPrimary'
})
const computedLightColor = computed(() => {
return getColorLightnessFromCache(getColor('textPrimary')) > (255 / 2) ? 'textPrimary' : 'textInverted'
})
const getTextColor = (color: ColorInput, darkColor?: string, lightColor?: string) => {
const onColorName = `on${capitalize(String(color))}`
if (colors[onColorName]) {
return colors[onColorName]
}
darkColor = darkColor || computedDarkColor.value
lightColor = lightColor || computedLightColor.value
return getColorLightnessFromCache(color) > globalConfig.value.colors.threshold ? darkColor : lightColor
}
const currentPresetName = computed<string>({
get: () => globalConfig.value.colors!.currentPresetName,
set: (v: string) => { applyPreset(v) },
})
const presets = computed(() => globalConfig.value.colors!.presets)
const applyPreset = (presetName: string) => {
globalConfig.value.colors!.currentPresetName = presetName
if (!globalConfig.value.colors!.presets[presetName]) {
return warn(`Preset ${presetName} does not exist`)
}
}
return {
colors,
currentPresetName,
presets,
applyPreset,
setColors,
getColors,
getColorStrict,
getColor,
getComputedColor,
getBoxShadowColor,
getBoxShadowColorFromBg,
getHoverColor,
getFocusColor,
getGradientBackground,
getTextColor,
shiftHSLAColor,
setHSLAColor,
colorsToCSSVariable,
colorToRgba,
getStateMaskGradientBackground,
}
}
export * from '../services/color/utils'
export * from '../services/color'