Skip to content

Clamp out-of-range alpha values in color functions - #581

Open
Pradeep Ramola (pradeep-ramola) wants to merge 1 commit into
microsoft:mainfrom
pradeep-ramola:fix/color-alpha-clamping
Open

Pradeep Ramola (pradeep-ramola) wants to merge 1 commit into
microsoft:mainfrom
pradeep-ramola:fix/color-alpha-clamping

Conversation

@pradeep-ramola

Copy link
Copy Markdown
Contributor

Problem

Valid color functions with alpha values outside [0, 1] currently disappear from findDocumentColors. For example, .test { color: rgb(34 89 234 / 150%); } returns no colors instead of an opaque color. Negative alpha values likewise produce no color instead of a transparent one.

CSS Color 4 explicitly requires clamping out-of-range alpha at parsed-value time: https://www.w3.org/TR/css-color-4/#alpha-value

The existing alpha parsing shares getNumericValue's bounded validation and throws on these values, causing color extraction to return null.

Change

Add an alpha-specific helper that reuses the existing numeric/percentage parser without range rejection, then clamps to [0, 1]. Use it for the ordinary color-function path and the separate color() path.

Keep other channel validation, function dispatch, source text/ranges, and unsupported-expression handling unchanged. This is independent of #579 and based directly on current main.

Tests

Added 39 public language-service tests across CSS, LESS, and SCSS:

  • Below-zero and above-one alpha values expressed as numbers and percentages.
  • Legacy comma and modern slash syntax, including all ten currently supported color-function names.
  • Exact color equivalence to the clamped boundary and preservation of the original source range.
  • In-range alpha values remain unchanged.
  • Invalid units and unresolved var() alpha values are not coerced into colors.

Before the production fix: 33 failed, 6 controls passed.

Verification on Windows after a clean npm ci:

  • npm run compile: TypeScript compilation and 7 lint checks passed.
  • Explicitly enumerated all 31 compiled test files and ran node --enable-source-maps --test: 463 passed, no failures/skips.
  • git diff --check: passed.
  • Supplemental headless Chromium 148 verification: 20 checks across five representative syntaxes; computed colors match the clamped-boundary spelling and canvas pixel alpha is 0/255 as appropriate.

The test files were enumerated explicitly to avoid the single-quoted npm glob selecting no tests in this Windows shell. Full VS Code decorator UI integration was not run.

AI assistance was used to investigate, implement, test, and draft this contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant