Skip to content

Commit c52cd71

Browse files
htunnicliffRobinMalfait
authored andcommitted
Replace color with culori
1 parent 05e4cee commit c52cd71

File tree

4 files changed

+50
-109
lines changed

4 files changed

+50
-109
lines changed

package-lock.json

Lines changed: 11 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@
7070
"arg": "^5.0.1",
7171
"chalk": "^4.1.2",
7272
"chokidar": "^3.5.2",
73-
"color": "^4.0.1",
7473
"cosmiconfig": "^7.0.1",
74+
"culori": "^0.19.1",
7575
"detective": "^5.2.0",
7676
"didyoumean": "^1.2.2",
7777
"dlv": "^1.1.3",

src/util/pluginUtils.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import selectorParser from 'postcss-selector-parser'
22
import postcss from 'postcss'
3-
import createColor from 'color'
3+
import culori from 'culori'
44
import escapeCommas from './escapeCommas'
55
import { withAlphaValue } from './withAlphaVariable'
66
import isKeyframeRule from './isKeyframeRule'
@@ -222,12 +222,7 @@ function splitAlpha(modifier) {
222222
}
223223

224224
function isColor(value) {
225-
try {
226-
createColor(value)
227-
return true
228-
} catch (e) {
229-
return false
230-
}
225+
return culori.parse(value) !== undefined
231226
}
232227

233228
export function asColor(modifier, lookup = {}, tailwindConfig = {}) {

src/util/withAlphaVariable.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
1-
import createColor from 'color'
1+
import culori from 'culori'
22
import _ from 'lodash'
33

4-
function hasAlpha(color) {
5-
return (
6-
color.startsWith('rgba(') ||
7-
color.startsWith('hsla(') ||
8-
(color.startsWith('#') && color.length === 9) ||
9-
(color.startsWith('#') && color.length === 5)
10-
)
11-
}
12-
13-
export function toRgba(color) {
14-
const [r, g, b, a] = createColor(color).rgb().array()
15-
16-
return [r, g, b, a === undefined && hasAlpha(color) ? 1 : a]
17-
}
18-
19-
export function toHsla(color) {
20-
const [h, s, l, a] = createColor(color).hsl().array()
21-
22-
return [h, `${s}%`, `${l}%`, a === undefined && hasAlpha(color) ? 1 : a]
4+
function isValidColor(color) {
5+
return culori.parse(color) !== undefined
236
}
247

258
export function withAlphaValue(color, alphaValue, defaultValue) {
269
if (_.isFunction(color)) {
2710
return color({ opacityValue: alphaValue })
2811
}
2912

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
13+
if (isValidColor(color)) {
14+
// Parse color
15+
const parsed = culori.parse(color)
16+
17+
// Apply alpha value
18+
parsed.alpha = alphaValue
19+
20+
// Return formatted string
21+
if (parsed.mode === 'hsl') {
22+
return culori.formatHsl(parsed)
23+
} else {
24+
return culori.formatRgb(parsed)
25+
}
3626
}
27+
28+
return defaultValue
3729
}
3830

3931
export default function withAlphaVariable({ color, property, variable }) {
@@ -44,24 +36,32 @@ export default function withAlphaVariable({ color, property, variable }) {
4436
}
4537
}
4638

47-
try {
48-
const isHSL = color.startsWith('hsl')
49-
50-
const [i, j, k, a] = isHSL ? toHsla(color) : toRgba(color)
39+
if (isValidColor(color)) {
40+
const { alpha = 1, mode } = culori.parse(color)
5141

52-
if (a !== undefined) {
42+
if (alpha !== 1) {
43+
// Has an alpha value, return color as-is
5344
return {
5445
[property]: color,
5546
}
5647
}
5748

58-
return {
59-
[variable]: '1',
60-
[property]: `${isHSL ? 'hsla' : 'rgba'}(${i}, ${j}, ${k}, var(${variable}))`,
49+
let value
50+
if (mode === 'hsl') {
51+
const { h, s, l } = culori.hsl(color)
52+
value = `hsla(${h}, ${s}, ${l}, var(${variable}))`
53+
} else {
54+
const { r, g, b } = culori.rgb(color)
55+
value = `rgba(${r}, ${g}, ${b}, var(${variable}))`
6156
}
62-
} catch (error) {
57+
6358
return {
64-
[property]: color,
59+
[variable]: '1',
60+
[property]: value,
6561
}
6662
}
63+
64+
return {
65+
[property]: color,
66+
}
6767
}

0 commit comments

Comments
 (0)