UNPKG

1.21 kBJavaScriptView Raw
1import { parseColor, formatColor } from './color'
2
3export function withAlphaValue(color, alphaValue, defaultValue) {
4 if (typeof color === 'function') {
5 return color({ opacityValue: alphaValue })
6 }
7
8 let parsed = parseColor(color, { loose: true })
9
10 if (parsed === null) {
11 return defaultValue
12 }
13
14 return formatColor({ ...parsed, alpha: alphaValue })
15}
16
17export default function withAlphaVariable({ color, property, variable }) {
18 let properties = [].concat(property)
19 if (typeof color === 'function') {
20 return {
21 [variable]: '1',
22 ...Object.fromEntries(
23 properties.map((p) => {
24 return [p, color({ opacityVariable: variable, opacityValue: `var(${variable})` })]
25 })
26 ),
27 }
28 }
29
30 const parsed = parseColor(color)
31
32 if (parsed === null) {
33 return Object.fromEntries(properties.map((p) => [p, color]))
34 }
35
36 if (parsed.alpha !== undefined) {
37 // Has an alpha value, return color as-is
38 return Object.fromEntries(properties.map((p) => [p, color]))
39 }
40
41 return {
42 [variable]: '1',
43 ...Object.fromEntries(
44 properties.map((p) => {
45 return [p, formatColor({ ...parsed, alpha: `var(${variable})` })]
46 })
47 ),
48 }
49}