peertube-plugin-livechat/client/utils/colors.ts

51 lines
2.0 KiB
TypeScript
Raw Permalink Normal View History

2024-05-23 09:42:14 +00:00
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
//
// SPDX-License-Identifier: AGPL-3.0-only
2021-12-14 14:54:36 +00:00
import { logger } from './logger'
import { AutoColors, areAutoColorsValid } from 'shared/lib/autocolors'
function computeAutoColors (): AutoColors | null {
if (!window.getComputedStyle) {
logger.warn('[AutoColors] getComputedStyle is not available, aborting.')
return null
}
const styles = window.getComputedStyle(document.body)
// Peertube has no CSS variable for the button color...
// Computing by hand.
// Searching for one of these button:
const button = document.querySelector('.publish-button') ?? document.querySelector('.peertube-button-link')
if (!button) {
logger.warn('[AutoColors] Cant find a button, aborting.')
return null
}
const buttonStyles = window.getComputedStyle(button)
const autocolors: AutoColors = {
2022-01-04 04:05:32 +00:00
mainForeground: styles.getPropertyValue('--mainForegroundColor').trim(),
mainBackground: styles.getPropertyValue('--mainBackgroundColor').trim(),
greyForeground: styles.getPropertyValue('--greyForegroundColor').trim(),
greyBackground: styles.getPropertyValue('--greyBackgroundColor').trim(),
menuForeground: styles.getPropertyValue('--menuForegroundColor').trim(),
menuBackground: styles.getPropertyValue('--menuBackgroundColor').trim(),
inputForeground: styles.getPropertyValue('--inputForegroundColor').trim(),
inputBackground: styles.getPropertyValue('--inputBackgroundColor').trim(),
buttonForeground: buttonStyles.color.trim(),
buttonBackground: styles.getPropertyValue('--mainColor').trim(),
link: styles.getPropertyValue('--mainForegroundColor').trim(),
linkHover: styles.getPropertyValue('--mainForegroundColor').trim()
2021-12-14 14:54:36 +00:00
}
const autoColorsTest = areAutoColorsValid(autocolors)
if (autoColorsTest !== true) {
logger.warn('[AutoColors] Computed colors are not valid, dropping. Invalid values: ' + autoColorsTest.join(', '))
return null
}
return autocolors
}
export {
computeAutoColors
}