Skip to content

Commit 8b03e8b

Browse files
committed
Update color extraction
1 parent 5b7f050 commit 8b03e8b

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/lsp/util/color.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,43 @@ export function getColor(
4848
)
4949

5050
// check that all of the values are valid colors
51-
if (colors.some((color) => !color.isValid)) {
51+
if (colors.some((color) => color !== 'transparent' && !color.isValid)) {
5252
return null
5353
}
5454

55-
// check that all of the values are the same color
56-
const colorStrings = colors.map((color) => color.toRgbString())
57-
if (dedupe(colorStrings).length !== 1) {
55+
// check that all of the values are the same color, ignoring alpha
56+
const colorStrings = dedupe(
57+
colors.map((color) =>
58+
color === 'transparent'
59+
? 'transparent'
60+
: `${color.r}-${color.g}-${color.b}`
61+
)
62+
)
63+
if (colorStrings.length !== 1) {
5864
return null
5965
}
6066

61-
return { documentation: colorStrings[0] }
67+
if (colorStrings[0] === 'transparent') {
68+
return {
69+
documentation: 'rgba(0, 0, 0, 0.01)',
70+
}
71+
}
72+
73+
const nonTransparentColors = colors.filter(
74+
(color): color is TinyColor => color !== 'transparent'
75+
)
76+
77+
const alphas = dedupe(nonTransparentColors.map((color) => color.a))
78+
79+
if (alphas.length === 1 || (alphas.length === 2 && alphas.includes(0))) {
80+
return {
81+
documentation: nonTransparentColors
82+
.find((color) => color.a !== 0)
83+
.toRgbString(),
84+
}
85+
}
86+
87+
return null
6288
}
6389

6490
export function getColorFromValue(value: unknown): string {
@@ -73,9 +99,9 @@ export function getColorFromValue(value: unknown): string {
7399
return null
74100
}
75101

76-
function createColor(str: string): TinyColor {
102+
function createColor(str: string): TinyColor | 'transparent' {
77103
if (str === 'transparent') {
78-
return new TinyColor({ r: 0, g: 0, b: 0, a: 0.01 })
104+
return 'transparent'
79105
}
80106

81107
// matches: rgba(<r>, <g>, <b>, var(--bg-opacity))

0 commit comments

Comments
 (0)