Skip to content

Instantly share code, notes, and snippets.

@jdboachie
Created August 15, 2025 17:16
Show Gist options
  • Save jdboachie/a43dcbdff04e928776102eff672abcd6 to your computer and use it in GitHub Desktop.
Save jdboachie/a43dcbdff04e928776102eff672abcd6 to your computer and use it in GitHub Desktop.
from PySide6.QtGui import QGuiApplication, QPalette
from PySide6.QtWidgets import QApplication
def get_theme_mode() -> str:
"""Return 'dark' or 'light' based on the current system/application theme."""
if QApplication.instance() is None:
raise RuntimeError("QApplication instance not found. Initialize it first.")
palette = QGuiApplication.palette()
bg_color = palette.color(QPalette.ColorRole.Window)
brightness = (
bg_color.red() * 0.299 + bg_color.green() * 0.587 + bg_color.blue() * 0.114
)
return "dark" if brightness < 128 else "light"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment