Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/languageFacts/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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': {
Expand Down
73 changes: 73 additions & 0 deletions src/test/css/colorAlpha.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
});
});
}