/
themes.service.ts
187 lines (160 loc) · 8.17 KB
/
themes.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { Inject, Injectable } from '@angular/core'
import { Subject, Observable } from 'rxjs'
import * as Color from 'color'
import { ConfigService } from '../services/config.service'
import { Theme } from '../api/theme'
import { NewTheme } from '../theme'
@Injectable({ providedIn: 'root' })
export class ThemesService {
get themeChanged$ (): Observable<Theme> { return this.themeChanged }
private themeChanged = new Subject<Theme>()
private styleElement: HTMLElement|null = null
private rootElementStyleBackup = ''
/** @hidden */
private constructor (
private config: ConfigService,
private standardTheme: NewTheme,
@Inject(Theme) private themes: Theme[],
) {
this.rootElementStyleBackup = document.documentElement.style.cssText
this.applyTheme(standardTheme)
config.ready$.toPromise().then(() => {
this.applyCurrentTheme()
this.applyThemeVariables()
config.changed$.subscribe(() => {
this.applyCurrentTheme()
this.applyThemeVariables()
})
})
}
private applyThemeVariables () {
if (!this.findCurrentTheme().followsColorScheme) {
document.documentElement.style.cssText = this.rootElementStyleBackup
}
const theme = this.config.store.terminal.colorScheme
const isDark = Color(theme.background).luminosity() < Color(theme.foreground).luminosity()
function more (some, factor) {
if (isDark) {
return Color(some).darken(factor)
}
return Color(some).lighten(factor)
}
function less (some, factor) {
if (!isDark) {
return Color(some).darken(factor)
}
return Color(some).lighten(factor)
}
let background = Color(theme.background)
if (this.config.store?.appearance.vibrancy) {
background = background.fade(0.6)
}
// const background = theme.background
const backgroundMore = more(background.string(), 0.25).string()
// const backgroundMore =more(theme.background, 0.25).string()
const accentIndex = 4
const vars: Record<string, string> = {}
const contrastPairs: string[][] = []
if (this.findCurrentTheme().followsColorScheme) {
vars['--bs-body-bg'] = background.string()
vars['--bs-body-color'] = theme.foreground
vars['--bs-black'] = theme.colors[0]
vars['--bs-red'] = theme.colors[1]
vars['--bs-green'] = theme.colors[2]
vars['--bs-yellow'] = theme.colors[3]
vars['--bs-blue'] = theme.colors[4]
vars['--bs-purple'] = theme.colors[5]
vars['--bs-cyan'] = theme.colors[6]
vars['--bs-gray'] = theme.colors[7]
vars['--bs-gray-dark'] = theme.colors[8]
// vars['--bs-red'] = theme.colors[9]
// vars['--bs-green'] = theme.colors[10]
// vars['--bs-yellow'] = theme.colors[11]
// vars['--bs-blue'] = theme.colors[12]
// vars['--bs-purple'] = theme.colors[13]
// vars['--bs-cyan'] = theme.colors[14]
contrastPairs.push(['--bs-body-bg', '--bs-body-color'])
vars['--theme-fg-more-2'] = more(theme.foreground, 0.5).string()
vars['--theme-fg-more'] = more(theme.foreground, 0.25).string()
vars['--theme-fg'] = theme.foreground
vars['--theme-fg-less'] = less(theme.foreground, 0.25).string()
vars['--theme-fg-less-2'] = less(theme.foreground, 0.5).string()
vars['--theme-bg-less-2'] = less(theme.background, 0.5).string()
vars['--theme-bg-less'] = less(theme.background, 0.25).string()
vars['--theme-bg'] = theme.background
vars['--theme-bg-more'] = backgroundMore
vars['--theme-bg-more-2'] = more(backgroundMore, 0.25).string()
contrastPairs.push(['--theme-bg', '--theme-fg'])
contrastPairs.push(['--theme-bg-less', '--theme-fg-less'])
contrastPairs.push(['--theme-bg-less-2', '--theme-fg-less-2'])
contrastPairs.push(['--theme-bg-more', '--theme-fg-more'])
contrastPairs.push(['--theme-bg-more-2', '--theme-fg-more-2'])
const themeColors = {
primary: theme.colors[accentIndex],
secondary: theme.colors[8],
tertiary: theme.colors[8],
warning: theme.colors[3],
danger: theme.colors[1],
success: theme.colors[2],
info: theme.colors[4],
dark: more(theme.background, 0.5).string(),
light: more(theme.foreground, 0.5).string(),
link: theme.colors[8], // for .btn-link
}
for (const [key, color] of Object.entries(themeColors)) {
vars[`--bs-${key}-bg`] = more(color, 0.5).string()
vars[`--bs-${key}-color`] = less(color, 0.5).string()
vars[`--bs-${key}`] = color
vars[`--bs-${key}-rgb`] = Color(color).rgb().array().join(', ')
vars[`--theme-${key}-more-2`] = more(color, 1).string()
vars[`--theme-${key}-more`] = more(color, 0.5).string()
vars[`--theme-${key}`] = color
vars[`--theme-${key}-less`] = less(color, 0.25).string()
vars[`--theme-${key}-less-2`] = less(color, 0.75).string()
vars[`--theme-${key}-fg`] = more(color, 1).string()
contrastPairs.push([`--theme-${key}`, `--theme-${key}-fg`])
}
const switchBackground = less(theme.colors[accentIndex], 0.25).string()
vars['--bs-form-switch-bg'] = `url("data:image/svg+xml,%3csvg xmlns=%27http://www.w3.org/2000/svg%27 viewBox=%27-4 -4 8 8%27%3e%3ccircle r=%273%27 fill=%27${switchBackground}%27/%3e%3c/svg%3e")`
}
vars['--spaciness'] = this.config.store.appearance.spaciness
for (const [bg, fg] of contrastPairs) {
const colorBg = Color(vars[bg]).hsl()
const colorFg = Color(vars[fg]).hsl()
const bgContrast = colorBg.contrast(colorFg)
const isLightBG = colorBg.luminosity() > colorFg.luminosity()
if (bgContrast < this.config.store.terminal.minimumContrastRatio) {
const targetLuminosityDarkFG = (colorBg.luminosity() + 0.05) / this.config.store.terminal.minimumContrastRatio - 0.05
const targetLuminosityLightFG = (colorBg.luminosity() + 0.05) * this.config.store.terminal.minimumContrastRatio - 0.05
let candidateLuminosities = isLightBG ? [targetLuminosityDarkFG, targetLuminosityLightFG] : [targetLuminosityLightFG, targetLuminosityDarkFG]
candidateLuminosities = candidateLuminosities.map(x => Math.max(0, Math.min(1, x)))
const targetLuminosity = candidateLuminosities.reduce((a, b) => Math.abs(b - colorBg.luminosity()) < Math.abs(a - colorBg.luminosity()) ? a : b, colorFg.color[2] / 100)
colorFg.color[2] = targetLuminosity * 100
vars[fg] = colorFg
}
}
for (const [key, value] of Object.entries(vars)) {
document.documentElement.style.setProperty(key, value)
}
document.body.classList.toggle('no-animations', !this.config.store.accessibility.animations)
}
findTheme (name: string): Theme|null {
return this.config.enabledServices(this.themes).find(x => x.name === name) ?? null
}
findCurrentTheme (): Theme {
return this.findTheme(this.config.store.appearance.theme) ?? this.standardTheme
}
applyTheme (theme: Theme): void {
if (!this.styleElement) {
this.styleElement = document.createElement('style')
this.styleElement.setAttribute('id', 'theme')
document.querySelector('head')!.appendChild(this.styleElement)
}
this.styleElement.textContent = theme.css
document.querySelector('style#custom-css')!.innerHTML = this.config.store?.appearance?.css
this.themeChanged.next(theme)
}
private applyCurrentTheme (): void {
this.applyTheme(this.findCurrentTheme())
}
}