-
Notifications
You must be signed in to change notification settings - Fork 0
/
dark_mode_support.py
44 lines (33 loc) · 1.26 KB
/
dark_mode_support.py
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
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Sam Schott (ss2151@cam.ac.uk)
(c) Sam Schott; This work is licensed under a Creative Commons
Attribution-NonCommercial-NoDerivs 2.0 UK: England & Wales License.
"""
from PyQt5 import QtWidgets, QtGui
THEME_DARK = "dark"
THEME_LIGHT = "light"
LINE_COLOR_DARK = (70, 70, 70)
LINE_COLOR_LIGHT = (213, 213, 213)
def rgb_to_luminance(r, g, b, base=256):
"""
Calculates luminance of a color, on a scale from 0 to 1, meaning that 1 is the
highest luminance. r, g, b arguments values should be in 0..256 limits, or base
argument should define the upper limit otherwise.
"""
return (0.2126*r + 0.7152*g + 0.0722*b)/base
def windowTheme():
"""
Returns one of gui.utils.THEME_LIGHT or gui.utils.THEME_DARK, corresponding to
current user's UI theme.
"""
# getting color of a pixel on a top bar, and identifying best-fitting color
# theme based on its luminance
w = QtWidgets.QWidget()
bg_color = w.palette().color(QtGui.QPalette.Background)
bg_color_rgb = [bg_color.red(), bg_color.green(), bg_color.blue()]
luminance = rgb_to_luminance(*bg_color_rgb)
return THEME_LIGHT if luminance >= 0.4 else THEME_DARK
def isDarkWindow():
return windowTheme() == THEME_DARK