Skip to content

Commit 498daad

Browse files
committed
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.
1 parent 5d2c4ed commit 498daad

File tree

3 files changed

+4
-7
lines changed

3 files changed

+4
-7
lines changed

src/display/display_utils.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -794,13 +794,10 @@ class CSSConstants {
794794
}
795795
}
796796

797-
function applyOpacity(r, g, b, opacity) {
797+
function applyOpacity(color, opacity) {
798798
opacity = MathClamp(opacity ?? 1, 0, 1);
799799
const white = 255 * (1 - opacity);
800-
r = Math.round(r * opacity + white);
801-
g = Math.round(g * opacity + white);
802-
b = Math.round(b * opacity + white);
803-
return [r, g, b];
800+
return color.map(c => Math.round(c * opacity + white));
804801
}
805802

806803
function RGBToHSL(rgb, output) {

test/unit/display_utils_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ describe("display_utils", function () {
323323
ctx.globalAlpha = 0.8;
324324
ctx.fillRect(0, 0, 1, 1);
325325
const [r, g, b] = ctx.getImageData(0, 0, 1, 1).data;
326-
expect(applyOpacity(123, 45, 67, ctx.globalAlpha)).toEqual([r, g, b]);
326+
expect(applyOpacity([123, 45, 67], ctx.globalAlpha)).toEqual([r, g, b]);
327327
});
328328
});
329329

web/comment_manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class CommentManager {
132132
return this.#hasForcedColors
133133
? null
134134
: findContrastColor(
135-
applyOpacity(...color, opacity ?? 1),
135+
applyOpacity(color, opacity ?? 1),
136136
CSSConstants.commentForegroundColor
137137
);
138138
}

0 commit comments

Comments
 (0)