feat: 缓存歌单封面颜色

This commit is contained in:
qier222 2022-04-12 01:48:14 +08:00
parent 766e866497
commit 1591586735
No known key found for this signature in database
GPG key ID: 9C85007ED905F14D
10 changed files with 506 additions and 396 deletions

View file

@ -1,5 +1,9 @@
import { IpcChannels } from '@/main/IpcChannelsName'
import dayjs from 'dayjs'
import duration from 'dayjs/plugin/duration'
import { APIs } from '@/main/CacheAPIsName'
import { average } from 'color.js'
import { colord } from 'colord'
/**
* @description
@ -70,10 +74,10 @@ export function formatDuration(
dayjs.extend(duration)
let time = dayjs.duration(milliseconds)
let hours = time.hours().toString()
let mins = time.minutes().toString()
let seconds = time.seconds().toString().padStart(2, '0')
const time = dayjs.duration(milliseconds)
const hours = time.hours().toString()
const mins = time.minutes().toString()
const seconds = time.seconds().toString().padStart(2, '0')
if (format === 'hh:mm:ss') {
return hours !== '0'
@ -111,3 +115,43 @@ export function scrollToTop(smooth = false) {
if (!main) return
main.scrollTo({ top: 0, behavior: smooth ? 'smooth' : 'auto' })
}
export async function getCoverColor(coverUrl: string) {
const id = new URL(coverUrl).pathname.split('/').pop()?.split('.')[0]
const colorFromCache = window.ipcRenderer?.sendSync(
IpcChannels.GetApiCacheSync,
{
api: APIs.CoverColor,
query: {
id,
},
}
) as string | undefined
return colorFromCache || calcCoverColor(coverUrl)
}
export async function cacheCoverColor(coverUrl: string, color: string) {
const id = new URL(coverUrl).pathname.split('/').pop()?.split('.')[0]
window.ipcRenderer?.send(IpcChannels.CacheCoverColor, {
api: APIs.CoverColor,
query: {
id,
color,
},
})
}
export async function calcCoverColor(coverUrl: string) {
if (!coverUrl) return
const cover = resizeImage(coverUrl, 'xs')
return average(cover, { amount: 1, format: 'hex', sample: 1 }).then(color => {
let c = colord(color as string)
const hsl = c.toHsl()
if (hsl.s > 50) c = colord({ ...hsl, s: 50 })
if (hsl.l > 50) c = colord({ ...c.toHsl(), l: 50 })
if (hsl.l < 30) c = colord({ ...c.toHsl(), l: 30 })
cacheCoverColor(coverUrl, c.toHex())
return c.toHex()
})
}