mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 17:11:17 -08:00
refactor: convert bg color utility to typescript
This commit is contained in:
2 files changed
+6
-4
No files matched your search
Vendored
-3
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user