22# SPDX-License-Identifier: GPL-3.0-only
33
44
5- from PIL import Image
5+ from PIL import Image , ImageQt
66from PySide6 .QtCore import QByteArray , QRectF , QSize , Qt
7- from PySide6 .QtGui import QColor , QGuiApplication , QPainter , QPixmap
7+ from PySide6 .QtGui import QColor , QGuiApplication , QImage , QPainter , QPixmap
88from PySide6 .QtSvg import QSvgRenderer
99
1010# TODO: Consolidate the built-in QT theme values with the values
1111# here, in enums.py, and in palette.py.
12- _THEME_DARK_FG : str = "#FFFFFF77"
13- _THEME_LIGHT_FG : str = "#000000DD"
14- _THEME_DARK_BG : str = "#000000DD"
15- _THEME_LIGHT_BG : str = "#FFFFFF55"
12+ _THEME_DARK_FG = QColor (255 , 255 , 255 , 120 )
13+ _THEME_LIGHT_FG = QColor (0 , 0 , 0 , 220 )
1614
1715
1816def theme_foreground_color () -> QColor :
1917 """Return a translucent foreground color for icons and hint text in the current theme."""
2018 if QGuiApplication .styleHints ().colorScheme () is Qt .ColorScheme .Dark :
21- return QColor (255 , 255 , 255 , 0x77 )
22- return QColor (0 , 0 , 0 , 0x77 )
19+ return QColor (255 , 255 , 255 , 120 )
20+ return QColor (0 , 0 , 0 , 120 )
2321
2422
2523def svg_to_pixmap (
@@ -43,58 +41,33 @@ def svg_to_pixmap(
4341
4442def auto_theme_overlay (
4543 image : Image .Image , inverse : bool = False , use_alpha : bool = True
46- ) -> Image . Image :
44+ ) -> QPixmap :
4745 """Overlay the current foreground theme color onto an image.
4846
4947 Args:
5048 image (Image): The PIL Image object to apply an overlay to.
5149 inverse (bool): Option to invert the overlay color relative to the current theme.
52- use_alpha (bool): Option to retain the base image 's alpha value when applying the overlay .
50+ use_alpha (bool): Option to use the theme 's translucent foreground color .
5351 """
54- dark_fg : str = _THEME_DARK_FG [:- 2 ] if not use_alpha else _THEME_DARK_FG
55- light_fg : str = _THEME_LIGHT_FG [:- 2 ] if not use_alpha else _THEME_LIGHT_FG
56-
57- overlay_color = (
58- dark_fg if QGuiApplication .styleHints ().colorScheme () is Qt .ColorScheme .Dark else light_fg
59- )
52+ is_dark = QGuiApplication .styleHints ().colorScheme () is Qt .ColorScheme .Dark
6053 if inverse :
61- overlay_color = light_fg if overlay_color == dark_fg else dark_fg
62-
63- im = Image .new (mode = "RGBA" , size = image .size , color = overlay_color )
64- return _apply_overlay (image , im )
65-
66-
67- def light_overlay (image : Image .Image , use_alpha : bool = True ) -> Image .Image :
68- """Overlay the light theme foreground color onto an image.
69-
70- Args:
71- image (Image): The PIL Image object to apply an overlay to.
72- use_alpha (bool): Option to retain the base image's alpha value when applying the overlay.
73- """
74- overlay_color : str = _THEME_DARK_FG [:- 2 ] if not use_alpha else _THEME_DARK_FG
75- im = Image .new (mode = "RGBA" , size = image .size , color = overlay_color )
76- return _apply_overlay (image , im )
54+ is_dark = not is_dark
55+ color = QColor (_THEME_DARK_FG if is_dark else _THEME_LIGHT_FG )
56+ if not use_alpha :
57+ color .setAlpha (255 )
58+ return apply_overlay (image , color )
7759
7860
79- def dark_overlay (image : Image .Image , use_alpha : bool = True ) -> Image . Image :
80- """Overlay the dark theme foreground color onto an image.
61+ def apply_overlay (image : Image .Image , color : QColor ) -> QPixmap :
62+ """Apply a color overlay on top of an image using the image's alpha channel as a mask .
8163
8264 Args:
8365 image (Image): The PIL Image object to apply an overlay to.
84- use_alpha (bool ): Option to retain the base image's alpha value when applying the overlay.
66+ color (QColor ): The color of the overlay.
8567 """
86- overlay_color : str = _THEME_LIGHT_FG [:- 2 ] if not use_alpha else _THEME_LIGHT_FG
87- im = Image .new (mode = "RGBA" , size = image .size , color = overlay_color )
88- return _apply_overlay (image , im )
89-
90-
91- def _apply_overlay (image : Image .Image , overlay : Image .Image ) -> Image .Image :
92- """Apply an overlay on top of an image using the image's alpha channel as a mask.
93-
94- Args:
95- image (Image): The PIL Image object to apply an overlay to.
96- overlay (Image): The PIL Image object to act as the overlay contents.
97- """
98- im : Image .Image = Image .new (mode = "RGBA" , size = image .size , color = "#00000000" )
99- im .paste (overlay , (0 , 0 ), mask = image )
100- return im
68+ overlaid = ImageQt .ImageQt (image ).convertToFormat (QImage .Format .Format_ARGB32_Premultiplied )
69+ painter = QPainter (overlaid )
70+ painter .setCompositionMode (QPainter .CompositionMode .CompositionMode_SourceIn )
71+ painter .fillRect (overlaid .rect (), color )
72+ painter .end ()
73+ return QPixmap .fromImage (overlaid )
0 commit comments