refactor: convert bg color utility to typescript

This commit is contained in:
hazre committed 2026-04-14 23:43:03 +02:00
1 parent 0bc8dae940
commit bb90fa7b80
2 files changed
+6 -4

No files matched your search

-3
View File
@@ -1,3 +0,0 @@
declare function bgColorImg(img: HTMLImageElement): string;
export default bgColorImg;
@@ -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;