diff --git a/src/languageFacts/colors.ts b/src/languageFacts/colors.ts index be8cf4f2..6f6fbb0a 100644 --- a/src/languageFacts/colors.ts +++ b/src/languageFacts/colors.ts @@ -311,6 +311,11 @@ function getNumericValue(node: nodes.Node, factor: number, lowerLimit: number = throw new Error(); } +function getAlphaValue(node: nodes.Node): number { + // CSS clamps out-of-range alpha values rather than invalidating the color. + return Math.min(1, Math.max(0, getNumericValue(node, 1, -Infinity, Infinity))); +} + const DEGREES_PER_CIRCLE = 360; // Number of degrees in a full circle const GRAD_TO_DEGREE_FACTOR = 0.9; // Conversion factor: grads to degrees const RADIANS_TO_DEGREES_FACTOR = DEGREES_PER_CIRCLE / 2 / Math.PI; // Conversion factor: radians to degrees @@ -940,7 +945,7 @@ function colorFromColorFunction(values: nodes.Node[]): Color | null { const left = last.getLeft(), right = last.getRight(), operator = last.getOperator(); if (left && right && operator && operator.matches('/')) { values = [values[0], values[1], values[2], left]; - alpha = getNumericValue(right, 1); + alpha = getAlphaValue(right); } } if (values.length !== 4) { @@ -990,7 +995,7 @@ export function getColorValue(node: nodes.Node): Color | null { } try { - const alpha = colorValues.length === 4 ? getNumericValue(colorValues[3], 1) : 1; + const alpha = colorValues.length === 4 ? getAlphaValue(colorValues[3]) : 1; switch (name) { case 'rgb': case 'rgba': { diff --git a/src/test/css/colorAlpha.test.ts b/src/test/css/colorAlpha.test.ts new file mode 100644 index 00000000..ddd90a87 --- /dev/null +++ b/src/test/css/colorAlpha.test.ts @@ -0,0 +1,73 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as assert from 'node:assert'; +import { suite, test } from 'node:test'; +import { getCSSLanguageService, getLESSLanguageService, getSCSSLanguageService, TextDocument } from '../../cssLanguageService.js'; + +const colorFunctions = [ + 'rgb(34 89 234 / ALPHA)', + 'rgb(34, 89, 234, ALPHA)', + 'rgba(34, 89, 234, ALPHA)', + 'hsl(120deg, 100%, 50%, ALPHA)', + 'hsla(240 100% 50% / ALPHA)', + 'hwb(120 0% 0% / ALPHA)', + 'lab(46.41 39.24 33.51 / ALPHA)', + 'lch(46.41 51.60 139.50 / ALPHA)', + 'oklab(62.796% 0.22486 0.12585 / ALPHA)', + 'oklch(62.7955% 0.257683 29.2339 / ALPHA)', + 'color(srgb 0.2 0.4 0.6 / ALPHA)', +]; + +for (const [languageId, createService] of [ + ['css', getCSSLanguageService], + ['less', getLESSLanguageService], + ['scss', getSCSSLanguageService], +] as const) { + suite(`${languageId.toUpperCase()} - Color alpha clamping`, () => { + const service = createService(); + + function findColors(value: string) { + const document = TextDocument.create(`test://test/value.${languageId}`, languageId, 0, `.test { color: ${value}; }`); + const stylesheet = service.parseStylesheet(document); + return { document, colors: service.findDocumentColors(document, stylesheet) }; + } + + for (const template of colorFunctions) { + test(`clamps alpha in ${template}`, () => { + for (const [alpha, expectedAlpha] of [['-0.5', 0], ['-50%', 0], ['1.5', 1], ['150%', 1]] as const) { + const expected = findColors(template.replace('ALPHA', String(expectedAlpha))).colors; + assert.strictEqual(expected.length, 1, 'boundary control'); + const value = template.replace('ALPHA', alpha); + const { document, colors } = findColors(value); + assert.strictEqual(colors.length, 1, value); + assert.strictEqual(colors[0].color.alpha, expectedAlpha, value); + assert.deepStrictEqual(colors[0].color, expected[0].color, value); + assert.strictEqual(document.getText(colors[0].range), value); + } + }); + } + + test('preserves alpha values already in range', () => { + for (const template of colorFunctions) { + for (const [alpha, expectedAlpha] of [['0', 0], ['0.5', 0.5], ['1', 1], ['0%', 0], ['50%', 0.5], ['100%', 1]] as const) { + const value = template.replace('ALPHA', alpha); + const { colors } = findColors(value); + assert.strictEqual(colors.length, 1, value); + assert.strictEqual(colors[0].color.alpha, expectedAlpha, value); + } + } + }); + + test('does not coerce invalid or unresolved alpha values', () => { + for (const template of colorFunctions) { + for (const alpha of ['1px', 'var(--opacity)']) { + const value = template.replace('ALPHA', alpha); + assert.deepStrictEqual(findColors(value).colors, [], value); + } + } + }); + }); +}