-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fallback when feed icon load error
Signed-off-by: Innei <i@innei.in>
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 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,62 @@ | ||
const getRandomColor = ( | ||
lightness: [number, number], | ||
saturation: [number, number], | ||
hue: number, | ||
) => { | ||
const satAccent = Math.floor( | ||
Math.random() * (saturation[1] - saturation[0] + 1) + saturation[0], | ||
) | ||
const lightAccent = Math.floor( | ||
Math.random() * (lightness[1] - lightness[0] + 1) + lightness[0], | ||
) | ||
|
||
// Generate the background color by increasing the lightness and decreasing the saturation | ||
const satBackground = satAccent > 30 ? satAccent - 30 : 0 | ||
const lightBackground = lightAccent < 80 ? lightAccent + 20 : 100 | ||
|
||
return { | ||
accent: `hsl(${hue}, ${satAccent}%, ${lightAccent}%)`, | ||
background: `hsl(${hue}, ${satBackground}%, ${lightBackground}%)`, | ||
} | ||
} | ||
|
||
export function stringToHue(str: string) { | ||
let hash = 0 | ||
for (let i = 0; i < str.length; i++) { | ||
hash = str.codePointAt(i)! + ((hash << 5) - hash) | ||
} | ||
const hue = hash % 360 | ||
return hue < 0 ? hue + 360 : hue | ||
} | ||
|
||
const memoMap = {} as Record<string, ReturnType<typeof getColorScheme>> | ||
export const getColorScheme = (hue?: number, memo?: boolean) => { | ||
const baseHue = hue ?? Math.floor(Math.random() * 361) | ||
if (baseHue && memo) { | ||
if (memoMap[baseHue]) { | ||
return memoMap[baseHue] | ||
} | ||
const result = getColorScheme(baseHue) | ||
memoMap[baseHue] = result | ||
return result | ||
} | ||
const complementaryHue = (baseHue + 180) % 360 | ||
|
||
// For light theme, we limit the lightness between 40 and 70 to avoid too bright colors for accent | ||
const lightColors = getRandomColor([40, 70], [70, 90], baseHue) | ||
|
||
// For dark theme, we limit the lightness between 20 and 50 to avoid too dark colors for accent | ||
const darkColors = getRandomColor([20, 50], [70, 90], complementaryHue) | ||
|
||
const result = { | ||
light: { | ||
accent: lightColors.accent, | ||
background: lightColors.background, | ||
}, | ||
dark: { | ||
accent: darkColors.accent, | ||
background: darkColors.background, | ||
}, | ||
} | ||
return result | ||
} |