-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use the dominant color of a user's avatar as the color of his/h…
…er racing bar (#727) * chore: github-url-detection v8 requires node >= 18 * feat: use the dominant color of a user's avatar as the color of his/her racing bar * chore: an insteresting way of using parttern as color * style: linear color is better * chore: adjust grid left * chore: smaller pics also work * refactor: store colorCache in chrome.storage.local * feat: add loading animation and fix storage writing racing
- Loading branch information
Showing
8 changed files
with
1,219 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/pages/ContentScripts/features/repo-activity-racing-bar/AvatarColorStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// @ts-ignore - TS7016: Could not find a declaration file for module 'colorthief'. | ||
import ColorThief from 'colorthief'; | ||
|
||
type LoginId = string; | ||
type Color = string; | ||
type RGB = [number, number, number]; | ||
interface ColorCache { | ||
colors: Color[]; | ||
lastUpdated: number; // timestamp | ||
} | ||
|
||
/** The number determines how many colors are extracted from the image */ | ||
const COLOR_COUNT = 2; | ||
/** The number determines how many pixels are skipped before the next one is sampled. */ | ||
const COLOR_QUALITY = 1; | ||
/** The number determines how long the cache is valid. */ | ||
const CACHE_EXPIRE_TIME = 1000 * 60 * 60 * 24 * 7; | ||
|
||
/** | ||
* A singleton class that stores the avatar colors of users. | ||
*/ | ||
class AvatarColorStore { | ||
private static instance: AvatarColorStore; | ||
private colorThief = new ColorThief(); | ||
|
||
private loadAvatar(loginId: string): Promise<HTMLImageElement> { | ||
return new Promise((resolve, reject) => { | ||
const img = new Image(); | ||
img.crossOrigin = 'anonymous'; | ||
img.onload = () => resolve(img); | ||
img.onerror = reject; | ||
img.src = `https://avatars.githubusercontent.com/${loginId}?s=8&v=4`; | ||
}); | ||
} | ||
|
||
public async getColors(loginId: LoginId): Promise<Color[]> { | ||
// Create a unique key for this user's cache entry. | ||
const cacheKey = `color-cache:${loginId}`; | ||
|
||
const now = new Date().getTime(); | ||
const colorCache: ColorCache = (await chrome.storage.local.get(cacheKey))[ | ||
cacheKey | ||
]; | ||
|
||
// Check if the cache is stale or doesn't exist. | ||
if (!colorCache || now - colorCache.lastUpdated > CACHE_EXPIRE_TIME) { | ||
let colors: Color[]; | ||
// a single white color causes error: https://github.com/lokesh/color-thief/issues/40#issuecomment-802424484 | ||
try { | ||
colors = await this.loadAvatar(loginId) | ||
.then((img) => | ||
this.colorThief.getPalette(img, COLOR_COUNT, COLOR_QUALITY) | ||
) | ||
.then((rgbs) => { | ||
return rgbs.map( | ||
(rgb: RGB) => `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})` | ||
); | ||
}); | ||
} catch (error) { | ||
console.error( | ||
`Cannot extract colors of the avatar of ${loginId}, error info: `, | ||
error | ||
); | ||
colors = Array(COLOR_COUNT).fill('rgb(255, 255, 255)'); | ||
} | ||
|
||
// Store the updated cache entry with the unique key. | ||
await chrome.storage.local.set({ | ||
[cacheKey]: { | ||
colors, | ||
lastUpdated: now, | ||
}, | ||
}); | ||
|
||
return colors; | ||
} | ||
|
||
// Return the cached colors. | ||
return colorCache.colors; | ||
} | ||
|
||
public static getInstance(): AvatarColorStore { | ||
if (!AvatarColorStore.instance) { | ||
AvatarColorStore.instance = new AvatarColorStore(); | ||
} | ||
return AvatarColorStore.instance; | ||
} | ||
} | ||
|
||
export const avatarColorStore = AvatarColorStore.getInstance(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.