Merge pull request #21003 from Snuffleupagus/applyOpacity-map

Simplify the `applyOpacity` helper function
This commit is contained in:
Tim van der Meij 2026-03-29 16:06:28 +02:00 committed by GitHub
commit 9026329d3d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 4 additions and 7 deletions

View File

@ -790,13 +790,10 @@ class CSSConstants {
}
}
function applyOpacity(r, g, b, opacity) {
function applyOpacity(color, opacity) {
opacity = MathClamp(opacity ?? 1, 0, 1);
const white = 255 * (1 - opacity);
r = Math.round(r * opacity + white);
g = Math.round(g * opacity + white);
b = Math.round(b * opacity + white);
return [r, g, b];
return color.map(c => Math.round(c * opacity + white));
}
function RGBToHSL(rgb, output) {

View File

@ -323,7 +323,7 @@ describe("display_utils", function () {
ctx.globalAlpha = 0.8;
ctx.fillRect(0, 0, 1, 1);
const [r, g, b] = ctx.getImageData(0, 0, 1, 1).data;
expect(applyOpacity(123, 45, 67, ctx.globalAlpha)).toEqual([r, g, b]);
expect(applyOpacity([123, 45, 67], ctx.globalAlpha)).toEqual([r, g, b]);
});
});

View File

@ -132,7 +132,7 @@ class CommentManager {
return this.#hasForcedColors
? null
: findContrastColor(
applyOpacity(...color, opacity ?? 1),
applyOpacity(color, opacity ?? 1),
CSSConstants.commentForegroundColor
);
}