mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-17 05:38:04 +00:00
feat: 缓存歌单封面颜色
This commit is contained in:
parent
766e866497
commit
1591586735
10 changed files with 506 additions and 396 deletions
|
|
@ -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()
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue