Skip to content

Commit

Permalink
Refactor preserving rgb/hsl when adding alpha channel
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Apr 30, 2021
1 parent 92bb81e commit f9d54f0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
4 changes: 2 additions & 2 deletions jit/pluginUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const selectorParser = require('postcss-selector-parser')
const postcss = require('postcss')
const { toRgba } = require('../lib/util/withAlphaVariable')
const createColor = require('color')
const { nameClass, escapeCommas } = require('./lib/utils')

function updateAllClasses(selectors, updateClass) {
Expand Down Expand Up @@ -166,7 +166,7 @@ module.exports = {
return asValue(modifier, lookup, {
validate: (value) => {
try {
toRgba(value)
createColor(value)
return true
} catch (e) {
return false
Expand Down
16 changes: 2 additions & 14 deletions src/plugins/gradientColorStops.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,15 @@ import _ from 'lodash'
import flattenColorPalette from '../util/flattenColorPalette'
import nameClass from '../util/nameClass'
import toColorValue from '../util/toColorValue'
import { toRgba, toHsla } from '../util/withAlphaVariable'
import { withAlphaValue } from '../util/withAlphaVariable'

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

const utilities = _(colors)
.map((value, modifier) => {
const transparentTo = (() => {
if (_.isFunction(value)) {
return value({ opacityValue: 0 })
}

try {
const isHSL = value.startsWith('hsl')
const [i, j, k] = isHSL ? toHsla(value) : toRgba(value)
return `${isHSL ? 'hsla' : 'rgba'}(${i}, ${j}, ${k}, 0)`
} catch (_error) {
return `rgba(255, 255, 255, 0)`
}
})()
const transparentTo = withAlphaValue(value, 0, 'rgba(255, 255, 255, 0)')

return [
[
Expand Down
20 changes: 7 additions & 13 deletions src/plugins/ringWidth.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import _ from 'lodash'
import nameClass from '../util/nameClass'
import { toHsla, toRgba } from '../util/withAlphaVariable'
import { withAlphaValue } from '../util/withAlphaVariable'

export default function () {
return function ({ addUtilities, theme, variants }) {
const ringColorDefault = (() => {
const isHSL = (theme('ringColor.DEFAULT') || '').startsWith('hsl')
const opacity = theme('ringOpacity.DEFAULT', '0.5')
try {
const [i, j, k] = isHSL
? toHsla(theme('ringColor.DEFAULT'))
: toRgba(theme('ringColor.DEFAULT'))
return `${isHSL ? 'hsla' : 'rgba'}(${i}, ${j}, ${k}, ${opacity})`
} catch (_error) {
return `rgba(147, 197, 253, ${opacity})`
}
})()
const ringOpacityDefault = theme('ringOpacity.DEFAULT', '0.5')
const ringColorDefault = withAlphaValue(
theme('ringColor.DEFAULT'),
ringOpacityDefault,
`rgba(147, 197, 253, ${ringOpacityDefault})`
)

addUtilities(
{
Expand Down
14 changes: 14 additions & 0 deletions src/util/withAlphaVariable.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ export function toHsla(color) {
return [h, `${s}%`, `${l}%`, a === undefined && hasAlpha(color) ? 1 : a]
}

export function withAlphaValue(color, alphaValue, defaultValue) {
if (_.isFunction(color)) {
return color({ opacityValue: alphaValue })
}

try {
const isHSL = color.startsWith('hsl')
const [i, j, k] = isHSL ? toHsla(color) : toRgba(color)
return `${isHSL ? 'hsla' : 'rgba'}(${i}, ${j}, ${k}, ${alphaValue})`
} catch {
return defaultValue
}
}

export default function withAlphaVariable({ color, property, variable }) {
if (_.isFunction(color)) {
return {
Expand Down

0 comments on commit f9d54f0

Please sign in to comment.