Hugo-theme-stack/assets/ts/color.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-08-22 07:20:08 -04:00
interface colorScheme {
key: string, /// Regenerate color scheme when the image is changed
2020-08-22 07:20:08 -04:00
DarkMuted: {
hex: string,
rgb: Number[],
bodyTextColor: string
},
Vibrant: {
hex: string,
rgb: Number[],
bodyTextColor: string
}
}
let colorsCache: { [key: string]: colorScheme } = {};
if (localStorage.hasOwnProperty('StackColorsCache')) {
2020-08-22 07:20:08 -04:00
try {
colorsCache = JSON.parse(localStorage.getItem('StackColorsCache'));
2020-08-22 07:20:08 -04:00
}
catch (e) {
colorsCache = {};
}
}
async function getColor(id: string, key: string, imageURL: string) {
if (!id || !colorsCache.hasOwnProperty(id) || colorsCache[id].key !== key) {
2020-08-22 07:20:08 -04:00
const palette = await Vibrant.from(imageURL).getPalette();
colorsCache[id] = {
key: key,
2020-08-22 07:20:08 -04:00
Vibrant: {
hex: palette.Vibrant.hex,
rgb: palette.Vibrant.rgb,
bodyTextColor: palette.Vibrant.bodyTextColor
},
DarkMuted: {
hex: palette.DarkMuted.hex,
rgb: palette.DarkMuted.rgb,
bodyTextColor: palette.DarkMuted.bodyTextColor
}
}
localStorage.setItem('StackColorsCache', JSON.stringify(colorsCache));
2020-08-22 07:20:08 -04:00
}
return colorsCache[id];
2020-08-22 07:20:08 -04:00
}
export {
getColor
}