From bb90fa7b80cc9ec3710804ff64f9939865b1dbca Mon Sep 17 00:00:00 2001 From: hazre Date: Tue, 14 Apr 2026 20:26:46 +0200 Subject: [PATCH] refactor: convert bg color utility to typescript --- src/app/utils/bgColorImg.d.ts | 3 --- src/app/utils/{bgColorImg.js => bgColorImg.ts} | 7 ++++++- 2 files changed, 6 insertions(+), 4 deletions(-) delete mode 100644 src/app/utils/bgColorImg.d.ts rename src/app/utils/{bgColorImg.js => bgColorImg.ts} (96%) diff --git a/src/app/utils/bgColorImg.d.ts b/src/app/utils/bgColorImg.d.ts deleted file mode 100644 index f38db24f9..000000000 --- a/src/app/utils/bgColorImg.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -declare function bgColorImg(img: HTMLImageElement): string; - -export default bgColorImg; diff --git a/src/app/utils/bgColorImg.js b/src/app/utils/bgColorImg.ts similarity index 96% rename from src/app/utils/bgColorImg.js rename to src/app/utils/bgColorImg.ts index c14326ba8..db7f397f2 100644 --- a/src/app/utils/bgColorImg.js +++ b/src/app/utils/bgColorImg.ts @@ -1,12 +1,17 @@ // Getting a dominant color from an IMG source, // and darkening it a bit afterwards for contrast -export default function bgColorImg(img) { +export default function bgColorImg(img: HTMLImageElement): string { const size = 32; const darken = 0.8; const canvas = document.createElement('canvas'); canvas.width = size; canvas.height = size; const ctx = canvas.getContext('2d', { willReadFrequently: true }); + + if (!ctx) { + return 'rgb(0, 0, 0)'; + } + ctx.drawImage(img, 0, 0, size, size); const px = ctx.getImageData(0, 0, size, size).data;