From 498daadf3cb4008f84ee5fa947fd580edb0f74eb Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 29 Mar 2026 14:52:06 +0200 Subject: [PATCH] Simplify the `applyOpacity` helper function This function only has a single call-site (if we ignore the unit-tests), where the colors are split into separate parameters. Given that all the color components are modified in the exact same way, it seems easier (and shorter) to pass the colors as-is to `applyOpacity` and have it use `Array.prototype.map()` instead. --- src/display/display_utils.js | 7 ++----- test/unit/display_utils_spec.js | 2 +- web/comment_manager.js | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/display/display_utils.js b/src/display/display_utils.js index 711ecae6c..f3d152326 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -794,13 +794,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) { diff --git a/test/unit/display_utils_spec.js b/test/unit/display_utils_spec.js index 87da7c0ca..28be4d441 100644 --- a/test/unit/display_utils_spec.js +++ b/test/unit/display_utils_spec.js @@ -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]); }); }); diff --git a/web/comment_manager.js b/web/comment_manager.js index 394a01ee4..9625c9fb9 100644 --- a/web/comment_manager.js +++ b/web/comment_manager.js @@ -132,7 +132,7 @@ class CommentManager { return this.#hasForcedColors ? null : findContrastColor( - applyOpacity(...color, opacity ?? 1), + applyOpacity(color, opacity ?? 1), CSSConstants.commentForegroundColor ); }