Skip to content

Commit 9026329

Browse files
Merge pull request #21003 from Snuffleupagus/applyOpacity-map
Simplify the `applyOpacity` helper function
2 parents 80d0d73 + 498daad commit 9026329

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
@@ -790,13 +790,10 @@ class CSSConstants {
790790
}
791791
}
792792

793-
function applyOpacity(r, g, b, opacity) {
793+
function applyOpacity(color, opacity) {
794794
opacity = MathClamp(opacity ?? 1, 0, 1);
795795
const white = 255 * (1 - opacity);
796-
r = Math.round(r * opacity + white);
797-
g = Math.round(g * opacity + white);
798-
b = Math.round(b * opacity + white);
799-
return [r, g, b];
796+
return color.map(c => Math.round(c * opacity + white));
800797
}
801798

802799
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)