From de1a2c4d31b26fc4dfe7c176816aa43090a34285 Mon Sep 17 00:00:00 2001 From: dsz Date: Tue, 24 Nov 2026 02:50:58 +0545 Subject: [PATCH] Read the alpha of modern CSS color syntax in colorAlpha colorAlpha() only understood rgb()/rgba() and treated any other color as fully opaque. Computed colors are not always rgb(): browsers keep the space a color was written or mixed in, so oklab(), oklch(), lab(), hsl() and color(srgb ...) all occur. Tailwind v4 compiles bg-black/50 to color-mix(in oklab, ...), which Chrome reports as oklab(0 0 0 / 0.5). The backdrop finder skips a full-screen layer whose background alpha is >= 0.99, so a half-transparent dimming backdrop written in any of those forms was read as solid and never recognized as a backdrop. colorAlpha() now parses any color function: the value after "/" in the modern syntax, or the fourth argument of the legacy comma syntax, as a number or percentage, clamped to 0..1, with "none" read as 0. Co-authored-by: Claude --- src/detection/layout.ts | 30 ++++++++++++++++++++++++------ tests/unit/layout.test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 tests/unit/layout.test.ts diff --git a/src/detection/layout.ts b/src/detection/layout.ts index cdd7052..432d500 100644 --- a/src/detection/layout.ts +++ b/src/detection/layout.ts @@ -77,13 +77,31 @@ export function zIndexOf(el: Element, probe: LayoutProbe): number { return Number.isFinite(z) ? z : 0; } +/** + * Alpha of a computed color, 0 to 1. Computed colors are not always rgb(): + * browsers keep the space a color was written or mixed in, so oklab(), oklch(), + * lab(), hsl() and color(srgb ...) all occur (Tailwind v4's bg-black/50 is a + * color-mix that Chrome reports as oklab(0 0 0 / 0.5)). + */ export function colorAlpha(color: string): number { - const m = color.match(/rgba?\(([^)]+)\)/); - if (!m?.[1]) return color === 'transparent' ? 0 : 1; - const parts = m[1].split(/[\s,/]+/).filter(Boolean); - if (parts.length < 4) return 1; - const a = parts[3] ?? '1'; - return a.endsWith('%') ? Number.parseFloat(a) / 100 : Number.parseFloat(a); + const value = color.trim().toLowerCase(); + if (value === 'transparent') return 0; + const args = value.match(/^[a-z-]+\((.*)\)$/)?.[1]; + if (args === undefined) return 1; + let alpha: string | undefined; + const slash = args.lastIndexOf('/'); + if (slash !== -1) { + // Modern syntax: rgb(0 0 0 / 50%), oklch(0.5 0.1 30 / 0.4), color(srgb 0 0 0 / 0.5). + alpha = args.slice(slash + 1).trim(); + } else { + // Legacy comma syntax: rgba(0, 0, 0, 0.5), hsla(0, 0%, 0%, 0.5). + const parts = args.split(','); + if (parts.length === 4) alpha = parts[3]!.trim(); + } + if (alpha === undefined) return 1; + if (alpha === 'none') return 0; + const n = alpha.endsWith('%') ? Number.parseFloat(alpha) / 100 : Number.parseFloat(alpha); + return Number.isFinite(n) ? Math.min(1, Math.max(0, n)) : 1; } export function describeElement(el: Element): string { diff --git a/tests/unit/layout.test.ts b/tests/unit/layout.test.ts new file mode 100644 index 0000000..8ef977d --- /dev/null +++ b/tests/unit/layout.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from 'vitest'; +import { colorAlpha } from '../../src/detection/layout'; + +describe('colorAlpha', () => { + it.each([ + ['rgba(0, 0, 0, 0.6)', 0.6], + ['rgb(0 0 0 / 50%)', 0.5], + ['rgb(0, 0, 0)', 1], + ['transparent', 0], + ['rgba(0, 0, 0, 0)', 0], + ])('reads %s', (color, alpha) => expect(colorAlpha(color)).toBeCloseTo(alpha)); + + // Browsers report computed colors in the space they were written or mixed + // in. Tailwind v4 compiles bg-black/50 to color-mix(in oklab, ...), which + // Chrome reports as oklab(0 0 0 / 0.5). + it.each([ + ['oklab(0 0 0 / 0.5)', 0.5], + ['oklch(0.21 0.034 264.665 / 80%)', 0.8], + ['color(srgb 0 0 0 / 0.4)', 0.4], + ['lab(0 0 0 / 0.3)', 0.3], + ['hsl(0 0% 0% / 0.25)', 0.25], + ['hsla(0, 0%, 0%, 0.25)', 0.25], + ['oklch(0.5 0.1 30)', 1], + ['color(display-p3 1 0 0)', 1], + ])('reads modern color syntax %s', (color, alpha) => + expect(colorAlpha(color)).toBeCloseTo(alpha), + ); +});