Skip to content

Commit f9d54f0

Browse files
committed
Refactor preserving rgb/hsl when adding alpha channel
1 parent 92bb81e commit f9d54f0

File tree

4 files changed

+25
-29
lines changed

4 files changed

+25
-29
lines changed

jit/pluginUtils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const selectorParser = require('postcss-selector-parser')
22
const postcss = require('postcss')
3-
const { toRgba } = require('../lib/util/withAlphaVariable')
3+
const createColor = require('color')
44
const { nameClass, escapeCommas } = require('./lib/utils')
55

66
function updateAllClasses(selectors, updateClass) {
@@ -166,7 +166,7 @@ module.exports = {
166166
return asValue(modifier, lookup, {
167167
validate: (value) => {
168168
try {
169-
toRgba(value)
169+
createColor(value)
170170
return true
171171
} catch (e) {
172172
return false

src/plugins/gradientColorStops.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,15 @@ import _ from 'lodash'
22
import flattenColorPalette from '../util/flattenColorPalette'
33
import nameClass from '../util/nameClass'
44
import toColorValue from '../util/toColorValue'
5-
import { toRgba, toHsla } from '../util/withAlphaVariable'
5+
import { withAlphaValue } from '../util/withAlphaVariable'
66

77
export default function () {
88
return function ({ addUtilities, theme, variants }) {
99
const colors = flattenColorPalette(theme('gradientColorStops'))
1010

1111
const utilities = _(colors)
1212
.map((value, modifier) => {
13-
const transparentTo = (() => {
14-
if (_.isFunction(value)) {
15-
return value({ opacityValue: 0 })
16-
}
17-
18-
try {
19-
const isHSL = value.startsWith('hsl')
20-
const [i, j, k] = isHSL ? toHsla(value) : toRgba(value)
21-
return `${isHSL ? 'hsla' : 'rgba'}(${i}, ${j}, ${k}, 0)`
22-
} catch (_error) {
23-
return `rgba(255, 255, 255, 0)`
24-
}
25-
})()
13+
const transparentTo = withAlphaValue(value, 0, 'rgba(255, 255, 255, 0)')
2614

2715
return [
2816
[

src/plugins/ringWidth.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
import _ from 'lodash'
22
import nameClass from '../util/nameClass'
3-
import { toHsla, toRgba } from '../util/withAlphaVariable'
3+
import { withAlphaValue } from '../util/withAlphaVariable'
44

55
export default function () {
66
return function ({ addUtilities, theme, variants }) {
7-
const ringColorDefault = (() => {
8-
const isHSL = (theme('ringColor.DEFAULT') || '').startsWith('hsl')
9-
const opacity = theme('ringOpacity.DEFAULT', '0.5')
10-
try {
11-
const [i, j, k] = isHSL
12-
? toHsla(theme('ringColor.DEFAULT'))
13-
: toRgba(theme('ringColor.DEFAULT'))
14-
return `${isHSL ? 'hsla' : 'rgba'}(${i}, ${j}, ${k}, ${opacity})`
15-
} catch (_error) {
16-
return `rgba(147, 197, 253, ${opacity})`
17-
}
18-
})()
7+
const ringOpacityDefault = theme('ringOpacity.DEFAULT', '0.5')
8+
const ringColorDefault = withAlphaValue(
9+
theme('ringColor.DEFAULT'),
10+
ringOpacityDefault,
11+
`rgba(147, 197, 253, ${ringOpacityDefault})`
12+
)
1913

2014
addUtilities(
2115
{

src/util/withAlphaVariable.js

+14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ export function toHsla(color) {
2222
return [h, `${s}%`, `${l}%`, a === undefined && hasAlpha(color) ? 1 : a]
2323
}
2424

25+
export function withAlphaValue(color, alphaValue, defaultValue) {
26+
if (_.isFunction(color)) {
27+
return color({ opacityValue: alphaValue })
28+
}
29+
30+
try {
31+
const isHSL = color.startsWith('hsl')
32+
const [i, j, k] = isHSL ? toHsla(color) : toRgba(color)
33+
return `${isHSL ? 'hsla' : 'rgba'}(${i}, ${j}, ${k}, ${alphaValue})`
34+
} catch {
35+
return defaultValue
36+
}
37+
}
38+
2539
export default function withAlphaVariable({ color, property, variable }) {
2640
if (_.isFunction(color)) {
2741
return {

0 commit comments

Comments
 (0)