| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import hash_literals, bound_methods |
| 4 |
|
| 5 |
from book_list.globals import get_session_data |
| 6 |
from dom import set_css |
| 7 |
from session import all_settings |
| 8 |
|
| 9 |
DARK = '#39322B' |
| 10 |
LIGHT = '#F6F3E9' |
| 11 |
LIGHT_DARKER = '#b6b3a8' |
| 12 |
DT_DARK = '#2d2d2d' |
| 13 |
DT_DARK_DARKER = 'black' |
| 14 |
DT_DARK_LIGHTER = '#777' |
| 15 |
DT_DARK_LIGTHER_CONTRAST = '#1d1d1d' |
| 16 |
DT_LIGHT = '#ddd' |
| 17 |
|
| 18 |
|
| 19 |
def c(light, dark): |
| 20 |
return {'light': light, 'dark': dark or light} |
| 21 |
|
| 22 |
|
| 23 |
def _byte(n): |
| 24 |
if n < 0: |
| 25 |
return 0 |
| 26 |
if n > 255: |
| 27 |
return 255 |
| 28 |
return int(n) |
| 29 |
|
| 30 |
|
| 31 |
def parse_hex_color(s): |
| 32 |
if not s: |
| 33 |
return None |
| 34 |
v = (s + '').strip() |
| 35 |
if v.startsWith('#'): |
| 36 |
v = v[1:] |
| 37 |
if v.length is 3: |
| 38 |
v = v[0] + v[0] + v[1] + v[1] + v[2] + v[2] |
| 39 |
if v.length is not 6: |
| 40 |
return None |
| 41 |
try: |
| 42 |
return [int(v[0:2], 16), int(v[2:4], 16), int(v[4:6], 16)] |
| 43 |
except Exception: |
| 44 |
return None |
| 45 |
|
| 46 |
|
| 47 |
def hex_as_rgba(s, opacity='1'): |
| 48 |
v = parse_hex_color(s) |
| 49 |
return f'rgba({v[0]},{v[1]},{v[2]},{opacity})' |
| 50 |
|
| 51 |
|
| 52 |
def mix_rgb_channel(a, b, t): |
| 53 |
return _byte(Math.round(a * (1 - t) + b * t)) |
| 54 |
|
| 55 |
|
| 56 |
def mix_rgb(a, b, t): |
| 57 |
r0 = mix_rgb_channel(a[0], b[0], t) |
| 58 |
r1 = mix_rgb_channel(a[1], b[1], t) |
| 59 |
r2 = mix_rgb_channel(a[2], b[2], t) |
| 60 |
return [r0, r1, r2] |
| 61 |
|
| 62 |
|
| 63 |
def rgb_to_hex(rgb): |
| 64 |
r = rgb[0] |
| 65 |
g = rgb[1] |
| 66 |
b = rgb[2] |
| 67 |
|
| 68 |
def h(n): |
| 69 |
s = n.toString(16) |
| 70 |
if s.length < 2: |
| 71 |
s = '0' + s |
| 72 |
return s |
| 73 |
|
| 74 |
return '#' + h(r) + h(g) + h(b) |
| 75 |
|
| 76 |
|
| 77 |
def light_mode_primary_tints(primary_rgb): |
| 78 |
W = [255, 255, 255] |
| 79 |
wb = rgb_to_hex(mix_rgb(W, primary_rgb, 0.055)) |
| 80 |
wb2 = rgb_to_hex(mix_rgb(W, primary_rgb, 0.115)) |
| 81 |
tree = rgb_to_hex(mix_rgb(W, primary_rgb, 0.095)) |
| 82 |
dlg = wb |
| 83 |
lh_bg = rgb_to_hex(mix_rgb(W, primary_rgb, 0.168)) |
| 84 |
inp = rgb_to_hex(mix_rgb(W, primary_rgb, 0.068)) |
| 85 |
bar_bg = rgb_to_hex(mix_rgb(W, primary_rgb, 0.052)) |
| 86 |
# Controls: keep contrast but hue-lock to primary. |
| 87 |
btn_base = [36, 38, 40] |
| 88 |
btn_base2 = [52, 54, 58] |
| 89 |
btn_s = rgb_to_hex(mix_rgb(btn_base, primary_rgb, 0.4)) |
| 90 |
btn_e = rgb_to_hex(mix_rgb(btn_base2, primary_rgb, 0.36)) |
| 91 |
return { |
| 92 |
'--calibre-color-window-background': wb, |
| 93 |
'--calibre-color-window-background2': wb2, |
| 94 |
'--calibre-color-tree-highlight-item': tree, |
| 95 |
'--calibre-color-dialog-background': dlg, |
| 96 |
'--calibre-color-list-hover-background': lh_bg, |
| 97 |
'--calibre-color-list-hover-foreground': DARK, |
| 98 |
'--calibre-color-input-background': inp, |
| 99 |
'--calibre-color-bar-background': bar_bg, |
| 100 |
'--calibre-color-bar-foreground': DARK, |
| 101 |
'--calibre-color-button-start': btn_s, |
| 102 |
'--calibre-color-button-end': btn_e, |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
def apply_theme_variables(variables): |
| 107 |
s = document.documentElement.style |
| 108 |
for k in Object.keys(variables): |
| 109 |
s.setProperty(k, variables[k]) |
| 110 |
get_color_as_rgba.cache = {} |
| 111 |
cached_color_to_rgba.cache = {} |
| 112 |
|
| 113 |
|
| 114 |
DEFAULT_COLORS = { |
| 115 |
# General colors |
| 116 |
'window-background': c(LIGHT, DT_DARK), |
| 117 |
'window-background2': c(LIGHT_DARKER, DT_DARK_LIGHTER), |
| 118 |
'window-foreground': c(DARK, DT_LIGHT), |
| 119 |
'window-error-foreground': c('red', '#C40233'), |
| 120 |
'window-hover-foreground': c('red', '#C40233'), |
| 121 |
'link-foreground': c('blue', '#6cb4ee'), |
| 122 |
'faded-text': c('#666', '#7f7f7f'), |
| 123 |
'border': c('rgba(0,0,0,0.12)', 'rgba(255,255,255,0.12)'), |
| 124 |
'border-subtle': c('rgba(0,0,0,0.08)', 'rgba(255,255,255,0.08)'), |
| 125 |
'scrollbar-thumb': c('rgba(0,0,0,0.26)', 'rgba(255,255,255,0.34)'), |
| 126 |
'scrollbar-thumb-hover': c('rgba(0,0,0,0.38)', 'rgba(255,255,255,0.48)'), |
| 127 |
'surface-1': c(hex_as_rgba(LIGHT, '0.08'), hex_as_rgba(DT_DARK, '0.08')), |
| 128 |
'surface-2': c(hex_as_rgba(LIGHT, '0.04'), hex_as_rgba(DT_DARK, '0.04')), |
| 129 |
'surface-3': c(hex_as_rgba(LIGHT, '0.02'), hex_as_rgba(DT_DARK, '0.02')), |
| 130 |
# Top bar specific colors |
| 131 |
'bar-background': c(DARK, DT_DARK_LIGHTER), |
| 132 |
'bar-foreground': c(LIGHT, DT_DARK_LIGTHER_CONTRAST), |
| 133 |
'bar-highlight': c('yellow'), |
| 134 |
'heart': c('#B92111'), |
| 135 |
# Item list colors |
| 136 |
'list-hover-background': c(DARK, DT_DARK_LIGHTER), |
| 137 |
'list-hover-foreground': c(LIGHT, DT_DARK_LIGTHER_CONTRAST), |
| 138 |
# Tree colors |
| 139 |
'tree-highlight-item': c(LIGHT_DARKER, DT_DARK_LIGHTER), |
| 140 |
# Button colors |
| 141 |
'button-start': c(DARK, DT_LIGHT), |
| 142 |
'button-end': c('#49423B', '#666'), |
| 143 |
'button-text': c(LIGHT, DT_DARK), |
| 144 |
# Dialog colors (solid panel — gradients hurt text contrast on some displays) |
| 145 |
'dialog-background': c(LIGHT, DT_DARK), |
| 146 |
'dialog-background-image': c('none', 'none'), |
| 147 |
'dialog-foreground': c(DARK, DT_LIGHT), |
| 148 |
# Native controls |
| 149 |
'input-background': c('field', DT_DARK_DARKER), |
| 150 |
'input-foreground': c('fieldtext', DT_LIGHT), |
| 151 |
'input-focus-outline-color': c('#4D90FE', DT_LIGHT), |
| 152 |
} |
| 153 |
|
| 154 |
DEFAULT_SIZES = { |
| 155 |
'title': '1.4rem', |
| 156 |
'item-list-title': '1.1rem', |
| 157 |
'item-list-subtitle': '0.8rem', |
| 158 |
} |
| 159 |
|
| 160 |
DEFAULT_FONTS = { |
| 161 |
'main': 'sans-serif' |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
def theme_variables(is_dark_theme, primary_color): |
| 166 |
attr = 'dark' if is_dark_theme else 'light' |
| 167 |
default_colors = all_settings.ui_highlight_colors.default |
| 168 |
if not primary_color: |
| 169 |
primary_color = default_colors.dark if is_dark_theme else default_colors.light |
| 170 |
if not primary_color.startswith('#'): |
| 171 |
primary_color = '#' + primary_color |
| 172 |
rgb = parse_hex_color(primary_color) |
| 173 |
if not rgb: |
| 174 |
primary_color = default_colors.dark if is_dark_theme else default_colors.light |
| 175 |
rgb = parse_hex_color(primary_color) |
| 176 |
r, g, b = rgb |
| 177 |
ans = { |
| 178 |
'--calibre-color-primary': primary_color, |
| 179 |
'--calibre-color-primary-light': f'rgba({r},{g},{b},0.18)', |
| 180 |
'--calibre-color-primary-light-2': f'rgba({r},{g},{b},0.28)', |
| 181 |
} |
| 182 |
for k in DEFAULT_COLORS: |
| 183 |
ans['--calibre-color-' + k] = DEFAULT_COLORS[k][attr] |
| 184 |
if not is_dark_theme: |
| 185 |
tints = light_mode_primary_tints(rgb) |
| 186 |
for k in Object.keys(tints): |
| 187 |
ans[k] = tints[k] |
| 188 |
return ans |
| 189 |
|
| 190 |
|
| 191 |
def set_ui_colors(is_dark_theme, primary_color): |
| 192 |
apply_theme_variables(theme_variables(is_dark_theme, primary_color)) |
| 193 |
document.documentElement.style.colorScheme = 'dark' if is_dark_theme else 'light' |
| 194 |
|
| 195 |
|
| 196 |
def browser_in_dark_mode(): |
| 197 |
return window.matchMedia('(prefers-color-scheme: dark)').matches |
| 198 |
|
| 199 |
|
| 200 |
def effective_book_list_is_dark(): |
| 201 |
try: |
| 202 |
sd = get_session_data() |
| 203 |
if not sd: |
| 204 |
return browser_in_dark_mode() |
| 205 |
pref = (sd.get('book_list_color_scheme', 'auto') or 'auto') + '' |
| 206 |
except Exception: |
| 207 |
return browser_in_dark_mode() |
| 208 |
if pref is 'dark': |
| 209 |
return True |
| 210 |
if pref is 'light': |
| 211 |
return False |
| 212 |
return browser_in_dark_mode() |
| 213 |
|
| 214 |
|
| 215 |
def get_saved_color_settings(): |
| 216 |
is_dark = effective_book_list_is_dark() |
| 217 |
try: |
| 218 |
sd = get_session_data() |
| 219 |
except Exception: |
| 220 |
sd = None |
| 221 |
colors = sd.get('ui_highlight_colors') if sd else all_settings.ui_highlight_colors.default |
| 222 |
primary_color = (colors?.dark) if is_dark else (colors?.light) |
| 223 |
if not primary_color: |
| 224 |
default_colors = all_settings.ui_highlight_colors.default |
| 225 |
primary_color = default_colors.dark if is_dark else default_colors.light |
| 226 |
return { |
| 227 |
'primary_color': primary_color, |
| 228 |
'is_dark': is_dark, |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
def apply_book_list_theme(): |
| 233 |
s = get_saved_color_settings() |
| 234 |
set_ui_colors(s.is_dark, s.primary_color) |
| 235 |
try: |
| 236 |
set_css(document.body, background_color=get_color('window-background'), color=get_color('window-foreground')) |
| 237 |
except Exception: |
| 238 |
pass |
| 239 |
|
| 240 |
|
| 241 |
def bind_book_list_color_scheme_listener(): |
| 242 |
try: |
| 243 |
mq = window.matchMedia('(prefers-color-scheme: dark)') |
| 244 |
except Exception: |
| 245 |
return |
| 246 |
|
| 247 |
def on_pref_change(ev): |
| 248 |
try: |
| 249 |
sd = get_session_data() |
| 250 |
if not sd: |
| 251 |
return |
| 252 |
if ((sd.get('book_list_color_scheme', 'auto') or 'auto') + '') is not 'auto': |
| 253 |
return |
| 254 |
except Exception: |
| 255 |
return |
| 256 |
apply_book_list_theme() |
| 257 |
|
| 258 |
if mq.addEventListener: |
| 259 |
mq.addEventListener('change', on_pref_change) |
| 260 |
else: |
| 261 |
mq.addListener(on_pref_change) |
| 262 |
|
| 263 |
|
| 264 |
def color_scheme(): |
| 265 |
return 'dark' if effective_book_list_is_dark() else 'light' |
| 266 |
|
| 267 |
|
| 268 |
def css_for_variables(): |
| 269 |
s = get_saved_color_settings() |
| 270 |
attr = 'dark' if s.is_dark else 'light' |
| 271 |
ans = [] |
| 272 |
variables = theme_variables(s.is_dark, s.primary_color) |
| 273 |
for k in Object.keys(variables): |
| 274 |
ans.push(f'{k}: {variables[k]};') |
| 275 |
return f':root {{ color-scheme: {attr}; ' + ans.join('\n') + '}\n\n' |
| 276 |
|
| 277 |
|
| 278 |
def get_color(name): |
| 279 |
return f'var(--calibre-color-{name})' |
| 280 |
|
| 281 |
|
| 282 |
def color_to_rgba(color): |
| 283 |
# take an arbitrary color spec and return it as [r, g, b, a] |
| 284 |
cvs = document.createElement('canvas') |
| 285 |
cvs.height = 1 |
| 286 |
cvs.width = 1 |
| 287 |
if color.startsWith('var('): |
| 288 |
color = window.getComputedStyle(document.documentElement).getPropertyValue(color[4:-1]) |
| 289 |
ctx = cvs.getContext('2d') |
| 290 |
ctx.fillStyle = color |
| 291 |
ctx.fillRect(0, 0, 1, 1) |
| 292 |
return ctx.getImageData(0, 0, 1, 1).data |
| 293 |
|
| 294 |
|
| 295 |
def cached_color_to_rgba(color): |
| 296 |
cache = cached_color_to_rgba.cache |
| 297 |
if not cache[color]: |
| 298 |
cache[color] = color_to_rgba(color) |
| 299 |
return cache[color] |
| 300 |
cached_color_to_rgba.cache = {} |
| 301 |
|
| 302 |
|
| 303 |
def get_color_as_rgba(name): |
| 304 |
cache = get_color_as_rgba.cache |
| 305 |
if not cache[name]: |
| 306 |
cache[name] = color_to_rgba(get_color(name)) |
| 307 |
return cache[name] |
| 308 |
get_color_as_rgba.cache = {} |
| 309 |
|
| 310 |
|
| 311 |
def get_font_size(name): |
| 312 |
return DEFAULT_SIZES[name] |
| 313 |
|
| 314 |
|
| 315 |
def get_font_family(name): |
| 316 |
name = name or 'main' |
| 317 |
return DEFAULT_FONTS[name] |