Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
htunnicliff committed Aug 5, 2021
1 parent a582beb commit 6df465a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/util/withAlphaVariable.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,20 @@ export default function withAlphaVariable({ color, property, variable }) {
}

if (isValidColor(color)) {
const { alpha = 1, mode } = culori.parse(color)
const parsed = culori.parse(color)

if (alpha !== 1) {
if ('alpha' in parsed) {
// Has an alpha value, return color as-is
return {
[property]: color,
}
}

let value
if (mode === 'hsl') {
const { h, s, l } = culori.hsl(color)
value = `hsla(${h}, ${s}, ${l}, var(${variable}))`
} else {
const { r, g, b } = culori.rgb(color)
value = `rgba(${r}, ${g}, ${b}, var(${variable}))`
}
const formatFn = parsed.mode === 'hsl' ? 'formatHsl' : 'formatRgb'
const value = culori[formatFn]({
...parsed,
alpha: 0.3,
}).replace('0.3)', `var(${variable}))`)

return {
[variable]: '1',
Expand Down
10 changes: 10 additions & 0 deletions tests/withAlphaVariable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ test('it adds the right custom property', () => {
'--tw-text-opacity': '1',
color: 'rgba(255, 0, 0, var(--tw-text-opacity))',
})
expect(
withAlphaVariable({
color: 'hsl(240 100% 50%)',
property: 'color',
variable: '--tw-text-opacity',
})
).toEqual({
'--tw-text-opacity': '1',
color: 'hsla(240, 100%, 50%, var(--tw-text-opacity))',
})
})

test('it ignores colors that cannot be parsed', () => {
Expand Down

0 comments on commit 6df465a

Please sign in to comment.