mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
const parseHexColor = (hexColor: string) => {
|
||
if (hexColor.startsWith('#')) {
|
||
hexColor = hexColor.substring(1)
|
||
}
|
||
const red = hexColor.substring(0, 2)
|
||
const green = hexColor.substring(2, 4)
|
||
const blue = hexColor.substring(4, 6)
|
||
return {
|
||
red, green, blue
|
||
}
|
||
}
|
||
export const convertHexColorForDialogue = (hexColor: string) => {
|
||
const { red, green, blue } = parseHexColor(hexColor)
|
||
return `\\c&H${blue}${green}${red}&`.toUpperCase()
|
||
}
|
||
export const convertHexColorForStyle = (hexColor: string, opacity = 1) => {
|
||
const { red, green, blue } = parseHexColor(hexColor)
|
||
const hexOpacity = Math.round(255 * (1 - opacity)).toString(16)
|
||
return `&H${hexOpacity}${blue}${green}${red}`.toUpperCase()
|
||
}
|
||
const round = (number: number) => {
|
||
const [integer, decimal = '00'] = String(number).split('.')
|
||
return `${integer.padStart(2, '0')}.${decimal.substr(0, 2).padEnd(2, '0')}`
|
||
}
|
||
const secondsToTime = (seconds: number) => {
|
||
let hours = 0
|
||
let minutes = 0
|
||
while (seconds >= 60) {
|
||
seconds -= 60
|
||
minutes++
|
||
}
|
||
while (minutes >= 60) {
|
||
minutes -= 60
|
||
hours++
|
||
}
|
||
return `${hours}:${String(minutes).padStart(2, '0')}:${round(seconds)}`
|
||
}
|
||
export const convertTimeByDuration = (startTime: number, duration: number) => {
|
||
return [secondsToTime(startTime), secondsToTime(startTime + duration)]
|
||
}
|
||
export const convertTimeByEndTime = (startTime: number, endTime: number) => {
|
||
return [secondsToTime(startTime), secondsToTime(endTime)]
|
||
}
|
||
export const normalizeContent = (content: string) => {
|
||
const map = {
|
||
'{': '{',
|
||
'}': '}',
|
||
'&': '&',
|
||
'<': '<',
|
||
'>': '>',
|
||
'"': '"',
|
||
''': "'",
|
||
'\n': '\\N',
|
||
}
|
||
for (const [key, value] of Object.entries(map)) {
|
||
content = content.replace(new RegExp(key, 'g'), value)
|
||
}
|
||
return content
|
||
}
|
||
export default {
|
||
export: {
|
||
convertHexColorForDialogue,
|
||
convertHexColorForStyle,
|
||
convertTimeByDuration,
|
||
convertTimeByEndTime,
|
||
normalizeContent,
|
||
},
|
||
} |